← Lessons

quiz vs the machine

Gold1400

Databases

Undo and Redo Logs

Two logs let a database both replay committed work and roll back unfinished work.

5 min read · core · beat Gold to climb

Two Jobs, Two Logs

Crash recovery needs two opposite abilities: redo any committed change that did not reach the data files, and undo any uncommitted change that did. Most engines keep separate information for each, often called the redo log and the undo log.

Redo

The redo log records, before a page change is applied, enough information to reproduce that change. This is write ahead logging: the log entry hits durable storage before the dirty page. After a crash, the engine replays redo records to bring data files up to the last committed state.

Undo

The undo log records the prior version of each modified row. It serves two purposes.

  • Rollback of a transaction that aborts uses undo to restore old values.
  • Multiversion reads let other transactions see the previous version while a row is being changed.

Working Together

Recovery first replays redo to redo all logged changes, then uses undo to roll back transactions that never committed, restoring a consistent state.

Key idea

Redo replays committed changes that had not yet reached disk, while undo reverses uncommitted ones and supplies old row versions, and recovery applies them in that order.

Check yourself

Answer to earn rating on the learn ladder.

1. What is the purpose of the redo log?

2. Beyond rollback, what does the undo log enable?

3. In what order does recovery apply the logs?