Recovery Mechanisms and Durability
You'll learn how databases use logs and checkpoints to ensure committed data survives system crashes.
The Problem
You've learned that durability guarantees committed changes persist even after crashes. But how does a database actually achieve this? When your application commits a transaction, the data might still be in memory buffers, not yet written to disk. If the power fails, everything in memory vanishes.
Write-Ahead Logging to the Rescue
Databases rely on transaction logs (which you learned about earlier) combined with checkpoints to recover data after crashes.
Here's how it works:
- Before any data page is written to disk, the database first writes a log record describing the change (write-ahead logging)
- When you
COMMIT, the log entry is forced to disk immediately—much faster than writing entire data pages - Data pages themselves are written to disk lazily, in the background
The Checkpoint Process:
Periodically, the database creates a checkpoint—a snapshot moment where it flushes all dirty (modified) data pages to disk and records which transactions were active. This creates a recovery starting point.
After a Crash
When the database restarts:
- Start from the last checkpoint
- Redo all committed transactions found in the log after that checkpoint
- Undo any incomplete transactions that didn't commit
Think of it like a bookmark (checkpoint) in a journal (log): you don't need to replay the entire history, just from your last bookmark forward.
Key Takeaway: Databases achieve durability through write-ahead logging and checkpoints—the log ensures committed work survives crashes, while checkpoints minimize recovery time by creating regular "save points" in the database's history.