Two-Phase Locking (2PL), Timestamp Ordering & Write-Ahead Logging
“2PL variants, Wait-Die vs Wound-Wait, and Write-Ahead Logging crash recovery.”
1.From Theory to Engine Implementation
§ 8.1.Testing precedence graphs after the fact is impossible in real database servers: an engine cannot wait for a day of transactions to run and then check if a cycle occurred. Instead, database engines use concurrency protocols (like 2PL) that guarantee serializability proactively.
Concurrency control protocols ensure that interleaved executions are guaranteed to produce conflict serializable schedules on the fly. Meanwhile, the Crash Recovery Subsystem guarantees that if the server loses power during a write operation, the database can reconstruct committed states and roll back half-finished transactions.
2.Two-Phase Locking (2PL) Protocol & Its Variants
§ 8.2.The 2PL Rule: A transaction must acquire all its locks before releasing any lock. It consists of two phases:
1. Growing Phase: Locks may be acquired, but NO locks can be released.
2. Lock Point: The exact moment when the transaction acquires its final lock.
3. Shrinking Phase: Locks may be released, but NO new locks can be acquired.
The 4 Major Variants of 2PL:
• Basic 2PL: Follows the 2 phases. Guarantees conflict serializability, but can suffer from deadlocks and cascading aborts.
• Conservative 2PL (Static 2PL): Transaction locks ALL needed items before execution begins. If any item is unavailable, it waits without locking anything. Guarantees Deadlock-Free execution, but reduces concurrency.
• Strict 2PL: Transaction holds all its Exclusive (X) write locks until after it commits or aborts. Guarantees Cascadelessness and Strict Recoverability! (Does NOT prevent deadlock).
• Rigorous 2PL: Transaction holds ALL locks (Shared and Exclusive) until after commit. Guarantees that the serialization order matches the exact commit order.
3. Comparison Matrix of 2PL Variants
A quick summary of the guarantees and tradeoffs across 2PL protocols.
| Feature / Aspect | Protocol Variant | Guarantees & Characteristics |
|---|---|---|
| Basic 2PL | Standard Growing & Shrinking phases. | Guarantees Conflict Serializability; Vulnerable to Deadlocks & Cascading Aborts. |
| Conservative 2PL | Pre-claims all locks prior to execution start. | Guarantees Conflict Serializability + Deadlock Freedom; Poor concurrency. |
| Strict 2PL | Holds Exclusive (Write) locks until commit/abort. | Guarantees Conflict Serializability + Cascadeless / Strict Recoverability; Deadlocks possible. |
| Rigorous 2PL | Holds both Shared (Read) and Exclusive (Write) locks until commit. | Serial order matches commit order; Most commonly implemented in commercial RDBMS. |
✎ Takeaway: Strict 2PL and Rigorous 2PL are the industry standards because preventing cascading aborts is mandatory for enterprise reliability.
4.Deadlock Prevention: Wait-Die vs Wound-Wait
§ 8.4.Assign each transaction T_i a unique timestamp TS(T_i) based on start time. Older transactions have smaller timestamps (TS(T_old) < TS(T_young)).
1. Wait-Die Scheme (Non-preemptive):
When T_i requests a lock held by T_j:
• If T_i is older (TS(T_i) < TS(T_j)): T_i is allowed to WAIT.
• If T_i is younger (TS(T_i) > TS(T_j)): T_i is killed (DIES) and restarts with its original timestamp.
Mnemonic: The old wait; the young die.
2. Wound-Wait Scheme (Preemptive):
When T_i requests a lock held by T_j:
• If T_i is older (TS(T_i) < TS(T_j)): T_i preempts (WOUNDS) T_j, forcing T_j to abort and release its lock.
• If T_i is younger (TS(T_i) > TS(T_j)): T_i is allowed to WAIT.
Mnemonic: The old wound the young; the young wait.
Result: Both schemes guarantee Deadlock Freedom because wait edges can only flow in one direction in the timestamp timeline!
5.Crash Recovery & Write-Ahead Logging (WAL)
§ 8.5.The Write-Ahead Logging (WAL) Protocol:
1. Before any data page is written to disk, its corresponding log record (containing old value and new value) must be flushed to persistent disk log storage.
2. A transaction cannot be acknowledged as committed until its
Checkpointing & The Undo/Redo Algorithm:
To avoid scanning the log back to the beginning of time upon reboot, the DBMS periodically writes a Checkpoint:
1. Flushes all dirty buffer pool pages to disk.
2. Writes a
Recovery on Restart:
• REDO List: Transactions that wrote
• UNDO List: Transactions that started (
6. Locking & Recovery Quiz
Test your understanding of 2PL, deadlock schemes, and WAL recovery.
Which of the following properties is guaranteed by the Strict Two-Phase Locking (Strict 2PL) protocol?
7. 1-Minute Protocols & Recovery Revision Card
“Strict 2PL holds write locks until commit (cascadeless). Conservative 2PL pre-claims all locks (deadlock-free). WAL flushes log before dirty pages.”
- •Wait-Die: Old waits, Young dies (non-preemptive).
- •Wound-Wait: Old wounds young, Young waits (preemptive).
- •Committed before crash? REDO. Uncommitted before crash? UNDO.
- •Strict 2PL guarantees Cascadelessness and Strict Recoverability, but NOT Deadlock freedom.
- •Deadlocks can still occur in Strict 2PL and Rigorous 2PL!
- •Redo requires the log's AFTER-image; Undo requires the log's BEFORE-image.
Primary Academic References & Exam Sources
Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.
The definitive research paper on Write-Ahead Logging and repeating history during crash recovery.
The bible of transaction systems engineering by Turing Award winner Jim Gray.
Curated for clarity, rigorous numerical solving, and semester excellence:
GATE-focused numerical problem-solving with clear worked examples.
Fast conceptual coverage with step-by-step GATE numericals.
Systematic textbook-style explanations ideal for first-time learners.
Deep theoretical rigor on normalization and transaction serializability.