# atomicity about REPLACE INTO and INSERT...ON DUPLICATE KEY UPDATE

**URL:** <https://forums.percona.com/t/atomicity-about-replace-into-and-insert-on-duplicate-key-update/4789>\
**Category:** Other MySQL® Questions\
**Created:** [March 17, 2016, 1:04am UTC](https://forums.percona.com/t/atomicity-about-replace-into-and-insert-on-duplicate-key-update/4789 "2016-03-17T01:04:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy\_xie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/andy_xie/32/1112_2.png) [@andy\_xie](https://forums.percona.com/u/andy_xie)\
**Post date:** [March 17, 2016, 1:04am UTC](https://forums.percona.com/t/atomicity-about-replace-into-and-insert-on-duplicate-key-update/4789/1 "2016-03-17T01:04:21Z")

</div>

Here is an example:  
TABLE:  
[INDENT] CREATE TABLE TICKET (  
id bigint(20) unsigned NOT NULL AUTO\_INCREMENT,  
stub char(1) NOT NULL DEFAULT ‘’,  
PRIMARY KEY (id),  
UNIQUE KEY idx\_ticket\_stub (stub)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8  
[/INDENT]  
[INDENT] INSERT INTO demo(stub) VALUES(‘a’) ON DUPLICATE KEY UPDATE id = id + 1  
[/INDENT]

---

<div class="post-metadata">

**Author:** ![andy\_xie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/andy_xie/32/1112_2.png) [@andy\_xie](https://forums.percona.com/u/andy_xie)\
**Post date:** [March 17, 2016, 2:50am UTC](https://forums.percona.com/t/atomicity-about-replace-into-and-insert-on-duplicate-key-update/4789/2 "2016-03-17T02:50:32Z")

</div>

I think it is just like “REPLACE INTO” about atomicity. I run a golang demo to simulate concurrent execution when using “REPLACE INTO” and “INSERT … INTO ON DUPLICATE KEY UPDATE”. All of those will sometimes output error info about “exec error Error 1213: Deadlock found when trying to get lock; try restarting transaction”. I think this means that both “REPLACE INTO” and “INSERT … INTO ON DUPLICATE KEY UPDATE” use lock or transaction internally. 🙂
