Deadlocks and lock ordering
medium 4 minA waits for a resource held by B while B waits for A.
Video lesson · Chapter 1 of 5
Two transfers stop in the middle
Deadlocks and lock order
1:01Reel
Theory
Read or listen first. About 1 min.
What it is
A wait graph describes who holds a resource and who needs it next. Trace a path from a waiting transfer to the holder of its next account lock.
Two transfers, two accounts
Why it exists
A common acquisition order eliminates cycles caused by inconsistent ordering of those locks. A transaction that loses a deadlock still needs a safe retry path.
Words to know
- Wait cycle
- A chain of participants in which every participant waits for another in the same chain.
How it works
Step through the idea, or press Play.
Lock opposite ends. One transaction holds account 4; the other holds 9.
Understand it
Worked example
Deadlocks and lock ordering in practice
Transfer X locks accounts 4 then 9. Transfer Y locks 9 then 4. If both instead acquire the lower account ID first, this two-account cycle cannot form.
Related case
When two correct updates break a rule
Two technicians are on call. Each may leave only if another remains. Two transactions see two technicians and each takes a different person off duty.
Lock the shared shift before reading or retry the serializable transaction.