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.