In this lesson, you will learn how to prevent cyclic deadlocks in distributed databases by enforcing strict priority rules based on transaction timestamps.
When multiple distributed transactions request locks on the same resource, a deadlock can occur if each transaction waits for another to release a lock. To avoid this without the overhead of cycle detection algorithms, we assign each transaction a unique, monotonically increasing timestamp at its start. These timestamps determine the "age" of a transaction, which dictates how conflicts are resolved.
The Wait-Die Scheme (Non-preemptive)
In this approach, we prioritize older transactions (smaller timestamps). If a younger transaction () requests a lock held by an older transaction ():
- is allowed to wait.
- If an older transaction requests a lock held by a younger one, the older transaction "dies" (it is aborted and restarted with its original timestamp).
The Wound-Wait Scheme (Preemptive)
This approach is more aggressive and generally results in fewer restarts. If a younger transaction holds a lock requested by an older transaction:
- The older transaction "wounds" the younger one (forcing it to abort).
- If a younger transaction requests a lock held by an older one, the younger transaction must wait.
Practical Comparison
Think of it as a boardroom hierarchy. In Wait-Die, the junior person must wait for the senior, but if the senior arrives, the junior leaves immediately. In Wound-Wait, the senior person can force the junior person to stop working and vacate the room so the senior can finish the task.
-- Conceptual logic for a lock manager:
IF (T_request.timestamp < T_holder.timestamp) {
// T_request is older
WOUND(T_holder); // In Wound-Wait
} ELSE {
// T_request is younger
WAIT(); // In Wound-Wait
}
Because these rules are based purely on timestamps—which are globally unique and ordered—they guarantee that a wait-for cycle can never form. By forcing one transaction to die or wait based on its age, we break the circular chain of dependencies required for a deadlock.
Key Takeaway: Wait-Die and Wound-Wait prevent deadlocks by using transaction timestamps to force a strict ordering, ensuring that circular waiting is logically impossible.