DEADLOCKS IN THE DATABASE

Two UPDATEs, opposite order, and the detector picks a victim

L215 MINDATABASES
PREDICT

Two transactions each update two rows, taking a row lock on each until COMMIT. T1 holds row 1 and waits for row 2. T2 holds row 2 and waits for row 1. What happens next?

An UPDATE takes an exclusive lock on the row it changes and holds it until COMMIT. Two transfers between the same accounts in opposite directions each lock their first row, then wait for the other’s. That is the lock-order deadlock from threads, with rows as the locks.

The database watches for it. After a short wait (PostgreSQL’s deadlock_timeout is 1 s; InnoDB checks at once) a detector looks for a cycle in the wait-for graph and aborts one transaction, the victim. The other proceeds. Your application gets an error and must retry. A wait with no cycle is not a deadlock, and the detector leaves it alone.

▶ SIMULATION
T1: transfer 10 from 1 to 2
holds row locks: —
waits for: —
idle
accounts (row locks)
id 1: balance 100 · unlocked
id 2: balance 100 · unlocked
WAIT-FOR GRAPH · clock 0 ms
(no edges)
T2: transfer 25 from 2 to 1
holds row locks: —
waits for: —
idle
✓ no cycle in the wait-for graph✓ no transaction waits longer than 2000 ms for a row lock
SCHEDULE
01[T1]T1: UPDATE accounts SET balance = balance - 10 WHERE id = 1
02[T2]T2: UPDATE accounts SET balance = balance - 25 WHERE id = 2
03[T1]T1: UPDATE accounts SET balance = balance + 10 WHERE id = 2 → row 2 held by T2, WAIT
04[T2]T2: UPDATE accounts SET balance = balance + 25 WHERE id = 1 → row 1 held by T1, WAIT
05[DB]clock +500 ms
06[DB]clock +500 ms → deadlock_timeout: detector runs, picks a victim
07[T1]T1: COMMIT
08[T2]T2: app catches the error and RETRIES the transaction
09[T2]T2: UPDATE accounts SET balance = balance - 25 WHERE id = 2
10[T2]T2: UPDATE accounts SET balance = balance + 25 WHERE id = 1
11[T2]T2: COMMIT
Start of deadlock scenario. 11 steps.
-- T1: transfer 10 from 1 to 2         -- T2: transfer 25 from 2 to 1
BEGIN;                                 BEGIN;
UPDATE accounts SET balance = balance - 10 WHERE id = 1;
                                       UPDATE accounts SET balance = balance - 25 WHERE id = 2;
UPDATE accounts SET balance = balance + 10 WHERE id = 2;   -- waits for T2
                                       UPDATE accounts SET balance = balance + 25 WHERE id = 1;   -- waits for T1
-- ERROR 40P01 deadlock detected (one of them, after deadlock_timeout)

Watch the WAIT-FOR GRAPH. In DEADLOCK the second wait closes the cycle and the strip turns red until the detector aborts T2. In SLOW, NOT DEADLOCKED there is only one edge: the detector runs, finds nothing, and T2 waits past the 2000 ms budget anyway. Both fixes run the same opposite-direction transfers with no cycle and no abort.

BREAK IT NOT YET

T1 updates row 1 then row 2. T2 updates row 2 then row 1. Interleave their statements so each one is waiting for a row the other holds.

GOAL: a cycle appears in the wait-for graph

FIX IT NOT YET

Two transactions transfer money between the same two rows in opposite directions at the same time. Rewrite transfer so it can never deadlock. The grader interleaves them at every lock and checks balances.

Mental model:

a row lock is a mutex the database takes for you and holds until COMMIT. Lock order is still your job: touch rows in one global order and the cycle cannot form. The detector is a safety net, not a fix; every transaction that can deadlock needs a retry around it. And a long wait with no cycle is a different bug: a transaction that holds its locks too long.

IN PRODUCTION

Transfers, inventory moves, any job that updates several rows of the same table from different code paths. Symptoms: PostgreSQL ERROR: deadlock detected (SQLSTATE 40P01), MySQL error 1213, and a SHOW ENGINE INNODB STATUS section titled LATEST DETECTED DEADLOCK.

IN INTERVIEWS

“Two transactions deadlock in production. How do you find and fix it?” Read the deadlock report for the two statements and the rows, explain the opposite-order cycle, fix it with a consistent key order or SELECT … ORDER BY id FOR UPDATE, and add a bounded retry for the error you can never fully rule out. Contrast it with a plain lock wait, which the detector will not touch.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: CAPSTONE: THE SLOW QUERY →