AlgoViz

Deadlocks and lock ordering

medium 4 min

A waits for a resource held by B while B waits for A.

Video lesson · Chapter 1 of 5

Two transfers stop in the middle

0:00 / 3:25

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

Transfer XTransfer YAccount 9Account 4needsheld byneedsheld byX needs 9. Y needs 4. Neither can release.
X holds account 4 and needs 9. Y holds 9 and needs 4. Follow the arrows around the closed wait cycle.

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.

Step 1 of 3

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.

Two names on the night roster
The night-shift rosterAdaon dutyOn call2 peopleBoon dutyTwo people cover the shift

Lock the shared shift before reading or retry the serializable transaction.

Sources