DATABASE MANAGEMENT SYSTEMS/Student Engineering Notebook
Page 08·notes
CONCURRENCY PROTOCOLS, DEADLOCKS & CRASH RECOVERY—~24 MIN READ

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

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

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 / AspectProtocol VariantGuarantees & Characteristics
Basic 2PLStandard Growing & Shrinking phases.Guarantees Conflict Serializability; Vulnerable to Deadlocks & Cascading Aborts.
Conservative 2PLPre-claims all locks prior to execution start.Guarantees Conflict Serializability + Deadlock Freedom; Poor concurrency.
Strict 2PLHolds Exclusive (Write) locks until commit/abort.Guarantees Conflict Serializability + Cascadeless / Strict Recoverability; Deadlocks possible.
Rigorous 2PLHolds 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

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)

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 log record has been safely flushed to disk.

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 log entry and flushes the log.

Recovery on Restart:

• REDO List: Transactions that wrote into the log before crash. Their updates are reapplied to disk to guarantee Durability.

• UNDO List: Transactions that started () but never committed before the crash. Their partial writes are rolled back using the log's before-image to guarantee Atomicity.

STUDENT PRACTICE · CONCEPT DRILL

6. Locking & Recovery Quiz

Test your understanding of 2PL, deadlock schemes, and WAL recovery.

Problem 1 · FOUNDATION

Which of the following properties is guaranteed by the Strict Two-Phase Locking (Strict 2PL) protocol?

RAPID REVIEW · NOTEBOOK SUMMARY

7. 1-Minute Protocols & Recovery Revision Card

core principle:

“Strict 2PL holds write locks until commit (cascadeless). Conservative 2PL pre-claims all locks (deadlock-free). WAL flushes log before dirty pages.”

must-remember principles:
  • •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.
critical exam pitfalls:
  • •Deadlocks can still occur in Strict 2PL and Rigorous 2PL!
  • •Redo requires the log's AFTER-image; Undo requires the log's BEFORE-image.
SOURCE TRANSPARENCY & CITATIONS

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.

Primary Academic Texts
ARIES: A Transaction Recovery Method Supporting Fine-Granularity Locking and Partial Rollbacks↗by C. Mohan, Don Haderle, Bruce Lindsay, Hamid Pirahesh, Peter Schwarz (ACM TODS 1992) · The landmark industry recovery algorithm used in IBM DB2, SQL Server, and SQLite

The definitive research paper on Write-Ahead Logging and repeating history during crash recovery.

ACADEMIC STANDARD
Transaction Processing: Concepts and Techniques↗by Jim Gray, Andreas Reuter (Morgan Kaufmann) · Chapters 7 & 10: Concurrency Control and Recovery Management

The bible of transaction systems engineering by Turing Award winner Jim Gray.

ACADEMIC STANDARD
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Knowledge Gate · DBMS for GATE & Semester↗Instructor: Sanchit Jain · Covers: ER diagrams, normalization, SQL, transactions, concurrency control

GATE-focused numerical problem-solving with clear worked examples.

Open YouTube↗
Gate Smashers · DBMS Full Course↗Instructor: Varun Singla · Covers: Relational algebra, SQL queries, B/B+ trees, file organization

Fast conceptual coverage with step-by-step GATE numericals.

Open YouTube↗
Neso Academy · DBMS Complete Playlist↗Instructor: Neso Academy · Covers: Relational model, SQL, normalization theory, ACID properties

Systematic textbook-style explanations ideal for first-time learners.

Open YouTube↗
Ravindrababu Ravula · DBMS Lectures↗Instructor: Ravindrababu Ravula · Covers: Functional dependencies, BCNF, 3NF decomposition, transaction schedules

Deep theoretical rigor on normalization and transaction serializability.

Open YouTube↗