Skip to content
Contents/CH 23
Transactions & Recovery/Advanced · 16 min

Recovery: Logs & Checkpoints

The log is the database's memory of what happened. After a crash, redo and undo use that memory to rebuild a consistent state.

In plain English

The log is a diary of changes. If the power goes out, replay the diary.

The one idea

Write-ahead logging records the before and after image before the data page reaches disk; recovery replays committed work and undoes uncommitted work.

A crash can happen between any two writes. The log is the only durable record of what the database intended to do.

By the end

  1. 01Explain write-ahead logging.
  2. 02Distinguish redo from undo.
  3. 03Know what a checkpoint does.
  4. 04Trace recovery after a crash.
01

Write-ahead logging

Before a data page is written to disk, the corresponding log record must be on stable storage. This is the write-ahead rule. The log records the transaction ID, page ID, old value, and new value. If a crash occurs, the log is the authoritative record of what was intended.

chapter-23.db
log:
<T1, begin>
<T1, update, page 5, old=100, new=200>
<T1, commit>
02

Redo and undo

Redo reapplies committed changes that may not have reached the data files. Undo reverses uncommitted changes that may have reached the data files. Recovery scans the log, builds the set of committed and uncommitted transactions, and applies the appropriate pass. The exact algorithm varies, but the principle is the same.

03

Checkpoints

A checkpoint flushes dirty pages and writes a checkpoint record. Recovery can start from the last checkpoint instead of the beginning of the log. A fuzzy checkpoint allows normal work to continue while pages are flushed, which keeps the system available.

Worked example3 steps

One update and one crash

T1 changes A from 500 to 400 and commits. T2 changes B from 300 to 250 but has not committed when the system crashes.

01

Write the log records before the data pages.

The log contains <T1, A, 500, 400> and <T2, B, 300, 250>.

02

T1's commit record reaches disk; T2's does not.

The log identifies T1 as a winner and T2 as a loser.

03

Run recovery.

Redo T1's change to A; undo T2's change to B.

ConclusionThe recovery manager does not guess. It reads the log, builds the winner and loser sets, and applies the correct action.

The trade-off

No free correctness.

Gain

Crash safety without copying the whole database.

Cost

Every write pays log I/O; checkpoints and log truncation add operational complexity.

Verdict

WAL is the default recovery mechanism in serious transactional systems.

Recap

What to keep.

Takeaways

  • +Write-ahead logging writes the log before the data page.
  • +Redo reapplies committed changes; undo reverses uncommitted changes.
  • +A checkpoint bounds the amount of log recovery must scan.
  • +The log must be on stable storage before commit is acknowledged.
  • +Recovery restores a consistent state after a crash.

Traps

  • !Writing data pages before the log record.
  • !Assuming a checkpoint means no recovery is needed.
  • !Forgetting that undo must handle changes already written to disk.

Focus

  • →State the write-ahead rule.
  • →Explain redo and undo with a small log.
  • →Describe the purpose of a checkpoint.

Check yourself

Before you turn the page.

0 / 2 answered
01

What is the write-ahead rule?

02

What does redo do?

DB//COSMOS

A field manual for database systems: model the domain, normalize the facts, query the relations, and protect the result.

Edition 2026 · 32 chaptersTopic catalogInterview prep