Course contentsShow
← Computer Science
Lesson 1664 of 2,87238. Database Transactions and IndexingFree lesson

Recovery Mechanisms and Durability

Explore how databases use logs and checkpoints to recover committed data after crashes.

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:

  1. Before any data page is written to disk, the database first writes a log record describing the change (write-ahead logging)
  2. When you COMMIT, the log entry is forced to disk immediately—much faster than writing entire data pages
  3. 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:

  1. Start from the last checkpoint
  2. Redo all committed transactions found in the log after that checkpoint
  3. 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.