arthrop0d

Brief articles on varied topics.

SQL Write-Ahead Logs and Crash Recovery

sqldatabases

A write-ahead log (WAL) is an append-only record of database changes that the database saves to durable storage before it writes the corresponding table and index pages. After a crash, the database uses that log to reconstruct committed changes and remove incomplete ones.

The problem the log solves

A database rarely updates a row by rewriting the entire table immediately. It changes an in-memory copy of a fixed-size data page, a block containing rows or index entries, and writes that page to storage later. A modified page that has not yet been written is called a dirty page.

That delay is useful: many changes can be grouped into fewer writes, and a transaction does not have to wait for every affected page. But it creates a failure window. Suppose a transaction transfers $100:

BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

If the process or machine fails after only the first page reaches storage, the database may contain money deducted from one account but not credited to the other. If neither page reached storage, the committed transfer appears to have vanished.

Writing every changed page immediately would avoid some of this uncertainty, but would be slow and still would not make a multi-page transaction atomic by itself. The WAL provides a smaller, ordered record of the work that can be made durable efficiently.

The write-ahead rule

For each change, the database creates a log record describing enough information to reproduce or reverse it. The record might identify a page and byte range, store a new value, store an old value, or describe a higher-level operation. The exact format differs between systems.

The central rule is:

A dirty data page must not reach durable storage until the log records needed to recover that page have reached durable storage first.

A database therefore normally follows this sequence:

  1. Change a page in memory and create a WAL record.
  2. Put the WAL record in the log buffer.
  3. Flush the log to durable storage before flushing the changed data page.
  4. Eventually flush the data page, perhaps much later.

“Durable” means that the database has asked the storage system to preserve the bytes across a process failure or restart, usually using an operating-system flush operation and storage hardware with appropriate guarantees. A successful write to a memory buffer is not necessarily durable.

A transaction's commit record is especially important. To acknowledge COMMIT safely, the database generally makes the transaction's log records, including its commit marker, durable first. It can then recover the transaction even if its changed pages were still only in memory when the crash happened.

This is why WAL is called write-ahead: the log write gets ahead of the data-page write, not necessarily ahead of the SQL statement. The statement may finish in memory while both writes are still waiting to be flushed.

What recovery does after a crash

On restart, the database finds a known position in the log, often supplied by a checkpoint, a record of recovery progress and relevant database state. It scans forward from there and determines which transactions were complete and which were active when the crash occurred.

Recovery commonly has two conceptual jobs: redo and undo.

Redo: restore durable committed work

Redo reapplies log records to data pages. This is needed even for committed transactions: their pages may not have been flushed before the crash. If a log record says that a page should contain a new value, recovery ensures that the page reflects that value.

Recovery must avoid applying a record twice incorrectly. Databases therefore attach an increasing log sequence number (LSN) to log records. A page stores the LSN of the latest log record already applied to it. During redo, the database compares the record's LSN with the page's LSN and applies the record only when the page is older.

This makes recovery safe when some writes reached storage before the crash and others did not. It can replay the same range of log records without blindly duplicating operations.

Undo: remove incomplete work

A transaction that has no durable commit marker is considered incomplete, even if some of its changed pages reached storage. Recovery must undo those changes so that the transaction appears never to have happened.

It follows the transaction's log records backward, restoring old values or applying compensating operations. The resulting undo records are themselves logged, so a second crash during recovery can resume safely. The incomplete transaction is then marked aborted or rolled back.

The exact ordering varies by database design. Some systems describe recovery as analysis, redo, and undo; others use different terminology or integrate rollback with normal transaction processing. The invariant is the same: committed changes survive, and uncommitted changes do not.

Why the log can be smaller than the database

The WAL is not normally a second copy of every table. It records changes in sequence, often as compact page-level or operation-level records. A database can flush the log sequentially and flush data pages in batches, which is usually cheaper than forcing random page writes for every transaction.

Once recovery no longer needs old log records—for example, because a checkpoint and later page flushes make them unnecessary—the database can recycle or remove them. Replication, backups, or delayed replicas may extend that retention period.

You have probably encountered WAL indirectly: a setting controlling synchronous commits, a full disk reported for a transaction log, a recovery message saying that log replay is starting, or a database that spends time replaying records after an unclean shutdown. Those are all expressions of the same trade-off: keep an ordered durable history of changes, and postpone or batch the more expensive data-page writes.

← All articles