How does a DBMS recover from a crash? Explain log-based recovery and checkpoints.
HardConceptTransactions & ACID
Answer
A DBMS records every change in a log before applying it to the data on disk, a rule called write-ahead logging (WAL). After a crash, it reads the log to redo committed work and undo uncommitted work.
- Each log record holds the transaction ID, the item and its old and new values, alongside
<T start>,<T commit>and<T abort>records. - Redo: transactions that have a commit record are replayed, so committed changes are not lost (durability).
- Undo: transactions without a commit record are rolled back using the old values (atomicity).
- A checkpoint periodically flushes changed pages to disk and writes a checkpoint record, so recovery only scans the log from the last checkpoint. Production systems refine this, for example with the ARIES algorithm.
Example:
<T1 start> <T1, A, 1000, 500> <T1 commit>
<checkpoint>
<T2 start> <T2, B, 200, 700>
<T3 start> <T3, C, 50, 80> <T3 commit>
--- crash ---
Recovery starts at the checkpoint: redo T3 (C = 80) and undo T2 (B back to 200). T1 committed before the checkpoint, so it needs no work.