← back to databases

Recovery

Wikipedia · wpWrite-ahead logging · CC BY-SA 4.0

Recovery restores the database to a consistent state after a crash. The key mechanism is write-ahead logging (WAL): before any data page is modified on disk, the change is first recorded in a log. On crash recovery, the log replays committed changes (redo) and undoes uncommitted ones (undo).

WAL (Write-Ahead Log) LSN Txn Op Before After 001 T1 W(A) 100 80 002 T1 W(B) 50 70 003 T1 COMMIT - - 004 T2 W(A) 80 60 005 *** CRASH *** Redo T1 (committed), undo T2 (not committed).

Write-ahead logging (WAL)

The WAL protocol: (1) before modifying a data page, write the log record to stable storage. (2) Before committing, flush all log records for that transaction. This guarantees that if the database crashes, the log contains enough information to reconstruct the committed state and undo uncommitted changes.

Scheme

Checkpoints

A checkpoint flushes all dirty pages to disk and records which transactions are active. On recovery, the system only needs to scan the log from the last checkpoint, not from the beginning. This bounds recovery time.

Python

ARIES algorithm

wpARIES (Algorithms for Recovery and Isolation Exploiting Semantics) is the industry-standard recovery algorithm. Three phases: (1) Analysis: scan the log to determine which transactions were active at crash time. (2) Redo: replay all logged actions from the last checkpoint. (3) Undo: reverse actions of uncommitted transactions, working backward. ARIES uses LSNs (Log Sequence Numbers) to avoid redundant work.

Scheme
Neighbors

Foundations (Wikipedia)