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
Diagnose it
The status output has a LATEST DETECTED DEADLOCK section with both transactions, locks held and locks waited on.
-- 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.
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.
Related reading
- 410 Gone: what it means and when to use it: 410 Gone explained: how it differs from 404, how search engines treat it, when to remove pages with 410, and how to fix a 410 you did not expect.
- 404 Not Found: how to find and fix broken pages: Find and fix 404 errors: broken internal links, moved pages, case-sensitive paths and soft 404s, with commands and advice on what actually affects SEO.
- CreateContainerConfigError in Kubernetes: how to fix it: Fix Kubernetes CreateContainerConfigError: missing Secrets and ConfigMaps, wrong keys and namespaces, with kubectl commands that show exactly what is absent.
- MySQL: Too many connections: Fix MySQL error 1040 Too many connections: check max_connections and current threads, find sleeping or leaked connections, size application pools.
- WordPress "Error establishing a database connection": fix: Fix WordPress Error establishing a database connection: check wp-config credentials, MySQL status, connection limits and damaged tables with safe commands.
- HTTP status codes explained, with fixes for the common errors: HTTP status codes explained by class, with what each common code means and links to step-by-step fixes for 403, 429, 500, 502, 503, 504 and Cloudflare 52x.
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.