"How do you diagnose and prevent MySQL deadlocks?"
Deadlock troubleshooting is a classic "have you actually run this in production" question. It separates candidates who've survived a 3am page from those who've only read documentation. Interviewers look for a methodical diagnostic approach and real prevention strategies, not textbook definitions.
Define deadlock at the InnoDB level: two or more transactions each hold a row/gap lock and wait for another lock held by the other transaction, forming a cycle. InnoDB detects the cycle and rolls back the "lighter" transaction.
Diagnostic command: SHOW ENGINE INNODB STATUS outputs a LATEST DETECTED DEADLOCK section showing the two transactions, their executing SQL, which locks each holds, and which lock each is waiting for.
Walk through the reading: identify the two SQL statements, the affected rows, and crucially which lock type — record lock, gap lock, or next-key lock. Most non-obvious deadlocks involve gap locks under RR isolation.
Cover common causes: conflicting index access orders (tx1 updates A then B, tx2 updates B then A); insert-intention vs. gap lock conflicts in RR; missing indexes causing whole-table lock escalation; long transactions holding locks across many statements.
Prevention strategies: keep transactions short and touch rows in the same order across code paths; ensure hot queries use proper indexes (no full table scan under lock); consider lowering isolation to RC when you don't need gap locks; set innodb_lock_wait_timeout and retry on deadlock in application code.
An InnoDB deadlock is a cycle where each transaction holds a lock the other needs. InnoDB detects the cycle with its wait-for graph and rolls back the transaction with fewer modified rows. The first thing I do when a deadlock fires in production is run SHOW ENGINE INNODB STATUS and look at the LATEST DETECTED DEADLOCK section. It shows both transactions, the SQL each was running, the lock each holds and the lock each is waiting for, including the exact page, heap number, and whether it's a record lock, gap lock, or next-key lock. Reading that output carefully almost always reveals the cause. Common patterns: two transactions updating the same pair of rows in opposite orders — tx1 updates A then B, tx2 updates B then A. Fix: enforce a consistent ordering in code, e.g., always sort the target IDs ascending before updating. Another pattern: INSERT conflicting with gap locks under Repeatable Read. Transaction A does SELECT ... FOR UPDATE WHERE col > 100 which takes a gap lock on (100, +∞), transaction B tries INSERT with col = 150 and blocks on the gap lock, then B holds a lock A needs. Fix: use a narrower predicate that doesn't take a large gap lock, or lower isolation to RC (which has no gap locks for most operations), or use SELECT without FOR UPDATE. A third pattern is a missing index — a DELETE or UPDATE with no suitable index scans the whole table under lock, turning a small operation into a table-wide lock and deadlocking with anything else touching the table. Always ensure UPDATEs/DELETEs use an index. Prevention beyond these: keep transactions short — ideally a transaction holds locks for tens of milliseconds, not seconds; never do network I/O (external API calls, queue publishes) inside a DB transaction; set innodb_lock_wait_timeout (default 50s, I usually set 3–5s to fail fast); and in application code, wrap writes in a retry loop catching deadlock errors because occasional deadlocks are unavoidable in high-concurrency systems. For chronic deadlock hotspots, the real fix is often a schema or ordering change, not more retries.
Always mention SHOW ENGINE INNODB STATUS — it's the single most important diagnostic tool and shows you've touched real production.
Call out gap locks specifically — they cause the non-obvious deadlocks that trip up most candidates.
Saying "we retry on deadlock in application code" is honest and strong — pretending deadlocks can be fully prevented is a red flag.
In a live interview, if you blank on the status command, HireMe AI can surface SHOW ENGINE INNODB STATUS immediately.
Yes, but fewer — RC drops most gap locks, so gap-related deadlocks mostly disappear. Row-on-row ordering conflicts still happen.
Usually decrease it. Long waits pile up threads and connections; failing fast + app-level retry is healthier.
Enable innodb_print_all_deadlocks in my.cnf to log every deadlock to the error log, then pipe the error log into your monitoring system.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free