Lecture: Lecture 18 - Reliability Management and Recovery
Related: Reliability Manager Log Records Undo Redo and Checkpoints · Buffer Manager and Page Replacement · Warm Restart Problem Solving and Cold Restart
Why log-writing rules are needed (Slide 17)
During normal execution, the Buffer Manager evicts dirty pages to disk whenever it needs buffer space, while transactions call commit as soon as their work finishes. If database pages reach disk—or commits return success to clients—before the corresponding log records reach stable memory, a sudden crash leaves the DBMS unable to restore Atomicity or Durability.
Two ordering rules govern log writes relative to database disk writes and commits: Write-Ahead Log (WAL) and Commit Precedence (Slides 17–21).
Rule 1: Write-Ahead Log (WAL) (Slide 18)
- Rule: The Before State (
BS) of data in a log record must be written in stable memory before the modified database data is written on disk. - Mechanism: Before the Buffer Manager flushes a dirty page containing object
Oto disk, the Reliability Manager ensures the log record holdingO’sBSis already forced to stable memory. - Why it matters: If transaction
Tupdatesxfrom10to99, the Buffer Manager writes99to disk, and the system crashes beforeTcommits, recovery must undoxback to10. WithoutBS = 10safely in the log prior to the disk write, the old value10would have been overwritten on disk and lost forever. - Guarantee supported: Enables UNDO operations on data already written on disk (Atomicity).
Rule 2: Commit Precedence (Slide 19)
- Rule: The After State (
AS) of data in a log record must be written in stable memory before the transaction commits. - Mechanism: Before
C(T)takes effect and the transaction is acknowledged as committed, allASvalues produced byT(along withC(T)) must be safely in stable memory. - Why it matters: If
Tcommits (x = 99) and power fails before the Buffer Manager writesx = 99from RAM to disk, recovery must redox = 99. WithoutAS = 99in stable memory before commit, the committed update would be lost. - Guarantee supported: Enables REDO operations for transactions that already committed but were not yet written on disk (Durability).
Practical rules and synchronous vs. asynchronous writes (Slides 20–21)
Because real systems write BS and AS together in the same U(T, O, BS, AS) log record (Slide 20), WAL and Commit Precedence simplify to two operational rules:
- WAL: The log record must be written to stable memory before the modified record is written in the database on disk.
- Commit precedence: The log records of a transaction must be written to stable memory before commit.
This directly dictates which log writes must block (synchronous / force) versus which can proceed in the background (asynchronous) (Slide 21):
| Event | Log write mode | Why |
|---|---|---|
| Before data modifications are written on disk | Synchronous (force) | Enforces WAL so BS is guaranteed to be on stable memory before disk is overwritten. |
On Commit (C(T)) | Synchronous (force) | Enforces Commit Precedence and guarantees C(T) survives a crash immediately after commit. |
On Abort / Rollback (A(T)) | Asynchronous | Does not need to block. See the borderline rule below! |
Why Abort can be asynchronous while Commit must be synchronous (Slide 22)
The commit record C(T) on the log is the exact borderline between UNDO and REDO (Slide 22):
- If
C(T)is not written in the log when a failure occurs → the transaction is undone. - If
C(T)is written in the log when a failure occurs → the transaction is redone.
Because of that borderline:
- If
Taborts (A(T)) and crashes beforeA(T)reaches stable memory,C(T)is absent from the log—so recovery undoesTanyway. An asynchronous write forA(T)cannot break correctness. - Conversely, if
Tcommits and crashes beforeC(T)reaches stable memory, recovery sees noC(T)and undoes a committed transaction, violating Durability. ThereforeC(T)must be forced synchronously.
Three protocols for writing the log and the database (Slides 23–25)
Depending on when database disk writes (W(x), W(y)) are allowed relative to C(T), a DBMS falls into one of three protocols (Slides 23–25):
1. All database disk writes BEFORE commit (Slide 23)
Log: B(T) U(T,x,BSx,ASx) U(T,y,BSy,ASy) C(T)
▲ ▲ ▲ ▲
───────┼──────────┼───────┼─────────────┼───────┼─────────┼────────► Time
▼ ▼
DB: W(x) W(y)- Behavior: Every modified page is forced to disk before
C(T)is written. - Recovery needed: UNDO only (does not require REDO of committed transactions, because once
C(T)is in the log, all writes are already on disk). - Tradeoff: Slow commits (every commit waits for random disk writes). Connects to the Force buffer policy in Buffer Manager and Page Replacement.
2. All database disk writes AFTER commit (Slide 24)
Log: B(T) U(T,x,BSx,ASx) U(T,y,BSy,ASy) C(T)
▲ ▲ ▲ ▲
───────┼──────────┼────────────────┼────────────┼───────┼───────┼──► Time
▼ ▼
DB: W(x) W(y)- Behavior: Dirty pages stay pinned in RAM and are written to disk only after
C(T)is logged. - Recovery needed: REDO only (does not require UNDO of uncommitted transactions, because uncommitted writes never touch disk).
- Tradeoff: Long or large transactions can exhaust RAM buffer space because dirty pages cannot be evicted early. Connects to the No-Steal buffer policy in Buffer Manager and Page Replacement.
3. Mixed approach: writes BEFORE and AFTER commit (Slide 25)
Log: B(T) U(T,x,BSx,ASx) U(T,y,BSy,ASy) C(T)
▲ ▲ ▲ ▲
───────┼──────────┼───────┼─────────────┼────────────┼───────┼──────► Time
▼ ▼
DB: W(x) W(y)- Behavior: Some pages (
W(x)) may be evicted to disk beforeC(T), while others (W(y)) stay in RAM and are written afterC(T). - Recovery needed: Both UNDO and REDO operations.
- Tradeoff: Adopted in real systems (matches Steal / No-Force). It gives the Buffer Manager full freedom to evict pages at any time while keeping commits fast (commits only wait for a sequential log
force, not scattered data-page writes).
Protocol comparison
| Protocol | DB writes vs. C(T) | Buffer policy parallel | Recovery required | Main runtime tradeoff |
|---|---|---|---|---|
| Before commit (Slide 23) | All before C(T) | Force (with Steal) | UNDO only (No REDO) | High commit latency |
| After commit (Slide 24) | All after C(T) | No-Steal (with No-Force) | REDO only (No UNDO) | Buffer memory pressure |
| Mixed (Slide 25) | Both before & after | Steal / No-Force | UNDO + REDO | Best performance; full recovery logic |
Cost of writing the log and optimizations (Slide 26)
Using robust log protocols to guarantee ACID properties is expensive: the cost of writing the log is comparable to the cost of updating the database itself (Slide 26). Real DBMSs optimize log writing without weakening guarantees through:
- Compact log format (minimizing bytes written per record),
- Parallelism (overlapping log I/O across devices or threads), and
- Group commit (batching commits from multiple concurrent transactions into a single synchronous log
force).
Common confusions
- Pairing the wrong rule with Undo/Redo: Remember WAL protects Before State → UNDO (Atomicity), while Commit Precedence protects After State → REDO (Durability).
- Thinking
W(x)on disk afterC(T)breaks REDO-only recovery: Under Protocol 2, if a crash happens betweenW(x)andW(y), recovery still runs REDO on the whole transactionT. Re-applyingW(x)to disk is harmless because REDO is idempotent, and re-applyingW(y)restores the lost RAM update. - Confusing “no UNDO” with “no WAL”: Even in real systems using the Mixed protocol, WAL must always hold for any page written before commit.
Retrieval questions with answers
1. Why is it safe to write the log asynchronously for an Abort/Rollback (A(T)), whereas Commit (C(T)) must be written synchronously?
C(T) is the borderline in the log: any transaction without C(T) in the log is undone upon failure. If a crash happens before an asynchronous A(T) hits stable memory, recovery sees no C(T) and undoes T anyway. By contrast, if C(T) were asynchronous and lost in a crash after acknowledging commit, recovery would wrongly undo a committed transaction and violate Durability.
2. Imagine a DBMS uses Protocol 2 (all DB writes AFTER commit), and a crash happens right after W(x) reaches disk (C(T) is in the log, W(x) is on disk, W(y) was still in RAM). Does recovery need UNDO, REDO, or both, and why does W(x) already being on disk not cause a problem?
Recovery needs REDO only (no UNDO, because nothing is ever written before commit). Redoing T re-applies both W(x) and W(y) using their logged After States (AS). Re-writing W(x) is completely safe because REDO is idempotent, leaving x at its exact AS while restoring W(y).
3. Why do real commercial DBMSs adopt the Mixed protocol even though it requires both UNDO and REDO?
It lets the Buffer Manager replace dirty pages freely when RAM fills up (avoiding Protocol 2’s buffer exhaustion) while letting commits finish with a single fast sequential log force instead of waiting for scattered database page writes (avoiding Protocol 1’s commit bottleneck).