Sub topic 11
Deadlock: wait-for graphs, wait-die and wound-wait
Assume you know rows, locks, transactions, and directed graphs. A deadlock occurs when transactions wait for one another in a cycle, so none can continue.
How a wait-for graph detects deadlock
A wait-for graph has one node per active transaction. Add an edge Ti → Tj when Ti is waiting for a lock held by Tj.
For example, suppose:
T1holds a lock on row A and requests row B.T2holds a lock on row B and requests row A.
The graph contains T1 → T2 and T2 → T1. This cycle proves that the transactions are deadlocked.
A database can build this graph from its lock table and periodically search for cycles. A cycle means that the system must recover. A graph without a cycle can still contain waiting transactions, but those waits may eventually finish.
Recovery after detection
The database usually chooses one transaction as a victim and rolls it back. Rolling back releases the victim's locks, allowing other transactions to proceed. The victim may then restart.
Victim selection can consider:
- How much work the transaction has performed.
- How many locks it holds.
- How much rollback would cost.
- Whether it has already been rolled back several times.
Repeatedly choosing the same transaction can cause starvation, where that transaction rarely completes. Systems can increase a transaction's priority after each rollback.
Prevention with timestamps
Detection allows deadlocks to form before fixing them. Prevention rules reject or delay lock requests so a cycle cannot form.
Give every transaction a timestamp when it starts. A smaller timestamp means the transaction is older.
Wait-die
With wait-die:
- An older requester may wait for a younger lock holder.
- A younger requester aborts, or “dies,” when it requests a lock held by an older transaction.
This rule is non-preemptive because the requester never forces the holder to release its lock.
Wound-wait
With wound-wait:
- An older requester aborts, or “wounds,” a younger lock holder.
- A younger requester waits for an older lock holder.
This rule is preemptive because the older transaction can force the younger one to roll back.
| Rule | Older requester | Younger requester |
|---|---|---|
| Wait-die | Waits | Aborts |
| Wound-wait | Aborts holder | Waits |
def wait_die(requester_ts, holder_ts):
"""Return the action for a lock request."""
return "wait" if requester_ts < holder_ts else "abort requester"
def wound_wait(requester_ts, holder_ts):
"""Return the action for a lock request."""
return "abort holder" if requester_ts < holder_ts else "wait"
print(wait_die(5, 9)) # wait
print(wait_die(9, 5)) # abort requester
print(wound_wait(5, 9)) # abort holder
print(wound_wait(9, 5)) # waitBoth schemes use fixed timestamps. If a rolled-back transaction keeps its original timestamp when it restarts, it becomes older than newer transactions and is less likely to starve.
Exercise
T1 has timestamp 3 and holds a lock. T2 has timestamp 8 and requests that lock. What happens under wait-die and wound-wait?
Show answer
Under wait-die, T2 aborts because it is younger. Under wound-wait, T2 waits because it is younger than T1.