What the message looks like
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transactionCommon causes
- Two transactions update the same rows or tables in opposite order, for example one locks row A then B, the other B then A.
- Long transactions hold locks for a long time, because the app does other work or waits for input between statements.
- Missing indexes on the
WHEREorJOINcolumns of anUPDATEorDELETE, so InnoDB locks many more rows than needed. SELECT ... FOR UPDATEon ranges inREPEATABLE READtakes gap locks, and concurrent inserts into the same gap block each other.- Foreign keys and triggers lock additional rows in other tables, so a simple statement touches more than it seems.
Check
- Report of the latest deadlock from InnoDB
mysql -e "SHOW ENGINE INNODB STATUS\G" | sed -n '/LATEST DETECTED DEADLOCK/,/WE ROLL BACK/p' - Are all deadlocks written to the error log?
mysql -e "SHOW VARIABLES LIKE 'innodb_print_all_deadlocks'" - Current isolation level
mysql -e "SHOW VARIABLES LIKE '%isolation%'" - Open transactions and locked rows
mysql -e "SELECT trx_id, trx_state, trx_started, trx_rows_locked, trx_query FROM information_schema.innodb_trx"
Fix
- Treat 1213 (SQLSTATE 40001) as retryable: repeat the whole transaction, not just the failed statement, a few times with a short random pause. Frameworks often have a setting for it.
- Access tables and rows in the same order in every code path, for example always update by ascending id.
- Keep transactions short. Do no network calls or other slow work between
BEGINandCOMMIT, and commit as early as possible. - Add indexes on the columns used by
UPDATE,DELETEand locking reads, so that fewer rows get locked. - To see every deadlock and not only the latest one, set
innodb_print_all_deadlocks = ON. It can be changed at runtime withSET GLOBAL. The details then go to the error log. READ COMMITTEDreduces gap locking and can lower the number of deadlocks. With binary logging it requiresbinlog_format=ROW, so test it before changing the isolation level.
Typical scenario
An application books stock movements. Two workers handle two orders at the same time that contain the same articles A and B, but in a different order. One updates A then B, the other B then A, and one of them gets ERROR 1213. The fix is to sort the lines by article id before updating, and to restart the transaction on 1213.
FAQ
Is a deadlock a bug in MySQL?
No. It is a normal result of concurrent transactions and InnoDB resolves it on purpose by rolling back one of them. It cannot be avoided completely, it can only be made rare, so the application has to be able to retry.
What is the difference to error 1205, lock wait timeout?
A deadlock is a cycle that InnoDB detects at once and resolves. With 1205 a transaction merely waited longer than innodb_lock_wait_timeout (50 seconds by default) for a lock that another transaction holds. That is a long-lock problem, not a cycle.
Where can I see which queries caused the deadlock?
In the LATEST DETECTED DEADLOCK section of SHOW ENGINE INNODB STATUS. It shows both transactions, their queries, the locks held and awaited, and at the end which one was rolled back.
How many deadlocks are still acceptable?
Occasional ones that the retry absorbs without consequences are normal under load. If they happen all the time or reach users as errors, it is worth analysing order, indexes and transaction length.
Does changing innodb_lock_wait_timeout help?
No, it only affects waiting for locks (error 1205). A deadlock is detected at once and is independent of it.