Skip to content

Troubleshooting guide

MySQL "Deadlock found": how to diagnose and prevent it

Two transactions each hold something the other wants. InnoDB picks one, rolls it back, and lets the other finish. Some are unavoidable; the design goal is making them rare and safe.

3 min read10 sectionsUpdated

On this page

Key points

  • The whole transaction is rolled back, so retry it entirely.
  • SHOW ENGINE INNODB STATUS explains the most recent deadlock.
  • Inconsistent access order is the classic cause.
  • Gap locks catch people out at REPEATABLE READ.

What it means

InnoDB's deadlock detector finds the cycle immediately and rolls back the transaction that is cheapest to undo. Unlike a lock wait timeout, nothing waits for long, and the transaction that survives continues normally.

Common causes

  • Rows updated in different orders by different code paths.
  • Gap and next-key locks under the default REPEATABLE READ, especially INSERT into ranges that others are locking.
  • Missing indexes, which widen the locked rows.
  • Big transactions touching many rows.

Diagnose it

The status output has a LATEST DETECTED DEADLOCK section with both transactions, locks held and locks waited on.

bash
-- Most recent deadlock
SHOW ENGINE INNODB STATUS\G

-- Record every deadlock in the error log
SET GLOBAL innodb_print_all_deadlocks = ON;

-- How often it happens
SELECT NAME, COUNT FROM information_schema.INNODB_METRICS WHERE NAME = 'lock_deadlocks';

Fixes

Retry the transaction on error 1213, with a little jitter, and make it safe to repeat. Access rows in a consistent order, such as sorted by primary key, in every code path. Add indexes, so fewer rows are locked. Keep transactions small. Consider READ COMMITTED, which reduces gap locking, after checking that your application does not depend on repeatable reads.

Worked example

Transaction A updates account 1, then account 2. Transaction B updates account 2, then account 1. If each completes its first update, they now wait for each other. Sorting the two IDs before updating means both transactions lock account 1 first, so one simply waits for the other and no cycle can form. The same principle covers bulk updates: an UPDATE ... WHERE id IN (...) processes rows in the order the plan produces, so ORDER BY id can help. Applying a fixed lock order across tables as well is simple to document and to review.

Catch it automatically

A low, steady deadlock rate handled by retries is normal. Alert when it rises, and record the transactions involved. APM shows which endpoints retry, while log management keeps the deadlock details searchable.

Frequently asked questions

Are deadlocks a bug?

Occasional ones are expected in concurrent systems. Frequent ones point to a design problem.

Which transaction is rolled back?

InnoDB usually chooses the one with the fewest row changes.

Can I turn off deadlock detection?

Yes with innodb_deadlock_detect, but only for very high-concurrency cases with a short lock wait timeout.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.