Deadlock avoidance in databases is essential to ensure that transactions can proceed without getting stuck indefinitely, waiting for resources held by each other. While deadlock detection and resolution (e.g., using wait-for graphs and choosing a victim to roll back) is commonly used, deadlock avoidance focuses on preventing the system from entering an unsafe state in the first place.
Here are some common methods for deadlock avoidance in databases:
1. Resource Ordering (Conservative Approach)
- Idea: Require all transactions to lock resources in a predefined global order.
- How it avoids deadlock: Prevents the circular wait condition, one of the four necessary conditions for deadlock.
- Example: If resources are numbered R1, R2, ..., Rn, a transaction must request R3 only after acquiring R1 and R2.
- Pros/Cons: Simple and effective, but may be restrictive and reduce concurrency.
2. Wait-Die Scheme
- Type: Non-preemptive technique based on transaction timestamps.
- Rules:
- If a transaction
T1 requests a resource held by T2:
- If
T1 is older than T2, T1 waits.
- If
T1 is younger than T2, T1 is rolled back (dies).
- How it avoids deadlock: Older transactions have priority; younger ones are rolled back before cycles form.
- Pros/Cons: Avoids deadlocks, but may cause unnecessary rollbacks.
3. Wound-Wait Scheme
- Type: Preemptive technique also based on transaction timestamps.
- Rules:
- If
T1 requests a resource held by T2:
- If
T1 is older than T2, T1 forces T2 to roll back (wounds T2) and takes the resource.
- If
T1 is younger, it waits.
- How it avoids deadlock: Ensures only one version of conflicting transactions survives, avoiding cycles.
- Pros/Cons: Reduces rollbacks compared to Wait-Die but still has overhead.
4. Timeout-Based Methods
- Idea: Set a maximum time a transaction can wait for a resource.
- How it avoids deadlock: If a transaction waits longer than the timeout, it is rolled back to break potential cycles.
- Pros/Cons: Simple to implement, but choosing an appropriate timeout is tricky—too short leads to false positives, too long delays detection.
5. No Waiting (Immediate Restart)
- Idea: A transaction either gets all required locks immediately or restarts later.
- How it avoids deadlock: Eliminates waiting entirely, so no possibility of circular waits.
- Pros/Cons: Very simple logic but can lead to high contention and frequent restarts under heavy load.
6. Graph-Based Locking Protocols
- Idea: Use tree or graph structures to define a partial order of locking.
- Example: In hierarchical databases, lock child nodes only after locking parent nodes.
- How it avoids deadlock: Enforces a directionality in locking, reducing chances of cycles.
7. Two-Phase Locking with Bounded Concurrency
- Idea: Combine 2PL (Two-Phase Locking) with limits on concurrent transactions or lock counts.
- How it avoids deadlock: By limiting how many transactions can hold locks at once, the system avoids complex dependency chains.
- Pros/Cons: Can help avoid some classes of deadlocks but not all.
Summary Table
| Method | Deadlock Avoidance Mechanism | Pros | Cons |
|---|
| Resource Ordering | Prevents circular wait | Simple, effective | Restrictive |
| Wait-Die | Prioritizes older transactions | No cycles | Frequent rollbacks |
| Wound-Wait | Preemptive rollback | Fewer rollbacks | Complex logic |
| Timeout | Breaks cycle after delay | Easy to implement | Risk of false positives |
| Immediate Restart | No waiting | Deadlock-free | High contention |
| Graph-Based Locking | Structured lock acquisition | Directional locking | Limited to certain data models |
| Bounded 2PL | Limits concurrency | Controlled locking | May not eliminate all cycles |
Conclusion
While deadlock avoidance strategies like resource ordering and wound-wait are effective in theory, most real-world database systems use a combination of deadlock detection and resolution (like periodic cycle checking in wait-for graphs) because strict avoidance can limit performance and scalability.
However, understanding these avoidance techniques is crucial for designing highly reliable and predictable transaction systems.