Section 8.12

Write-ahead logging

Definition

Write-ahead logging (WAL) is a technique for making updates to stored data survive crashes. Before a change is made to the data in its permanent location, a record describing the change is appended to a log, a separate storage area that is only ever added to at its end, and the record is forced to non-volatile storage. A transaction §8.7 commits when its commit record §8.21 is durable in the log. After a crash, recovery redoes committed changes missing from the data and undoes uncommitted changes that reached it.

The idea

A database must change 3 pages to commit one transaction, and the pages lie in different places on the disk. Writing them in place takes 3 separate writes, and a crash between two of them leaves a mixture of old and new pages. Waiting until all 3 pages are written before reporting the commit also makes every commit slow.

Write-ahead logging first writes a small description of the change in one place, at the end of the log. Appending to one sequential area is fast, and once the description is durable the change cannot be lost: if a crash comes before the pages are updated, recovery rebuilds them from the log. The permanent locations can be brought up to date later, in bulk. A ship’s logbook is a fair picture: the officer writes the course change in the book before turning the wheel, so that after any interruption the book says what was intended. The picture stops at the word before. On a ship the order is a habit; in storage it must be enforced by waiting until the log write is permanent, because storage devices may reorder writes.

How it works

Each log record carries a log sequence number (LSN), its position in the log, which grows with every record. Records come in three kinds.

Which kinds are needed depends on two choices that Theo Härder and Andreas Reuter named in 1983 [514]. Steal: may a page that holds uncommitted changes be written to its permanent location, for example to free memory? If so, recovery may have to undo, and undo records are needed. Force: must every page a transaction changed be written to its permanent location before the commit is reported? If not, recovery may have to redo, and redo records are needed. Steal with no force gives the system the most freedom and needs both kinds. ARIES (Algorithms §14.1 for Recovery and Isolation Exploiting Semantics), the recovery method that C. Mohan and colleagues published in 1992 [516], is built for that combination.

Three ordering rules make the log sufficient.

  1. Write-ahead rule. The undo record of a change must be durable before the changed page overwrites the old page in its permanent location. Otherwise a crash could leave an uncommitted change in the data with no record of the old value.

  2. Commit rule. Every redo record of a transaction, and then its commit record, must be durable before the commit is reported. Otherwise a reported commit could vanish. The moment the commit record becomes durable is the commit point.

  3. Truncation rule. A log record may be discarded only when it can no longer be needed: the change it describes is durable in its permanent location, and its transaction has finished.

Durable means confirmed by a flush, an operation such as fsync §15.22 on Unix-like systems that returns only after earlier writes are stored permanently. A write call that has returned may still leave the data in a cache. A checkpoint brings the permanent locations up to date and records in the log how far back recovery must start, so that older records can be discarded and recovery stays short.

Recovery after a crash has three steps.

  1. Read the log forward from the point the last checkpoint recorded, and stop at the first record that is incomplete or fails its checksum §4.32; that is the end of the log.

  2. Redo: apply the changes of every committed transaction. Each page stores the LSN of the last change applied to it, and a change is applied only if its LSN is larger, so applying the log twice gives the same result as applying it once.

  3. Undo: reverse the changes of every transaction that has no commit record, newest change first.

ARIES redoes every logged change, committed or not, before it undoes the uncommitted ones, a principle it calls repeating history.

Worked example

This example uses a redo-only log: uncommitted changes never reach the permanent locations, so only redo records are needed. SQLite, a widely used database library that keeps a database in one file, follows this design in its WAL mode, although it logs whole pages rather than single values. Accounts A and B hold 50 and 20 in their permanent locations. Transaction TT moves 10 from A to B. The log receives:

LSN 1: TT sets A to 40
LSN 2: TT sets B to 30
LSN 3: TT commits

The log is flushed, and only then is the commit reported. Later, a checkpoint writes 40 and 30 to the permanent locations and flushes them; after that the three records may be discarded.

Crash cuts.

The records hold values (set A to 40), not differences (subtract 10 from A). Replaying a difference twice would give 50−10−10=3050 - 10 - 10 = 30 for A, which is wrong; replaying a value twice gives 40 both times. Recovery may itself be interrupted and run again, so this idempotency §8.31, the property that repeating an action has the same effect as doing it once, is required. In a design that allows steal, the permanent location of A might receive 40 before TT commits; the old value, 50, would then have to be durable in the log before that write, so that recovery after a crash before LSN 3 could put A back to 50.

Stated precisely

Write durable(e)\mathrm{durable}(e) for the moment at which the record or page write ee is stored permanently. For every transaction TT, every log record rr and every page pp, write-ahead logging requires:

  1. if rr is the undo record of a change to pp, then durable(r)\mathrm{durable}(r) comes before the write of pp to its permanent location begins;

  2. if rr is a redo record of TT, then the commit record of TT can be found valid after a crash only if rr is durable and intact, and the commit record of TT is durable before the system reports that TT committed;

  3. rr is discarded only after the change it describes is durable in its permanent location and TT has committed, or has been rolled back durably.

Rule 2 is enforced either by a flush between the other records and the commit record, or by a checksum that covers all earlier records, so that a commit record cannot pass the check if any earlier record is missing.

Given these rules, recovery that redoes the changes of committed transactions and undoes the changes of the others produces a state that contains exactly the committed transactions. The guarantee rests on four assumptions.

  1. A completed flush means the data survives power loss; the device does not acknowledge writes that are still in a volatile cache.

  2. The end of the log can be recognized: every record carries a length and a checksum, and records left from an earlier use of the same log space cannot pass as new ones.

  3. Redo and undo are idempotent, so a crash during recovery is followed by a correct second recovery.

  4. The log itself is not lost or corrupted after a flush.

Limits and common mistakes

A log is not automatically a write-ahead log. A diagnostic log that records events after they happen, or a log that is not flushed before the data changes, gives recovery nothing to rely on.

A write call that has returned is not durable. The data may still sit in an operating-system cache or in the drive’s own cache. Only a completed flush counts, and a device that acknowledges a flush early breaks the guarantee.

Every change is written twice, once to the log and once to its permanent location, which is a form of write amplification §8.18. The log write is sequential and fast, but on flash memory §6.6, which wears out after a limited number of erase cycles, the extra writing costs lifetime.

The log grows until a checkpoint lets old records be discarded, and recovery time grows with the length of log after the last checkpoint. In SQLite’s WAL mode, a long-running reader can stop a checkpoint from finishing, and the log file then keeps growing.

Write-ahead logging supplies atomicity §8.3 and durability §8.2. It does not supply isolation between concurrent transactions. It does not show who wrote a record, unless records carry a message authentication code §5.7, a tag that only a holder of a secret key can compute. It cannot undo effects outside the storage, such as a message already sent.

Recovery may itself be interrupted, so it must be safe to run again from the start. That is why redo uses values or LSN checks, and why ARIES also logs the undo actions it performs, in records it calls compensation log records.

Questions answered

What ordering makes the log sufficient for recovery?

The log is sufficient for recovery when every log record that recovery will need is durably stored before the event that creates the need. In write-ahead logging (WAL), changes to stored data are first described in an append-only log §8.13, so that recovery after a crash can redo committed changes and undo uncommitted ones. Three ordering rules make the log sufficient.

  1. The undo record holding the old value of a page must be durable before that page is overwritten in its permanent location.

  2. Every redo record of a transaction, and then its commit record, must be durable before the commit is reported to anyone, and the commit record must be unable to appear valid unless all earlier records of the transaction are intact.

  3. A record may be discarded only after the change it describes is durable in its permanent location and its transaction has finished.

Durable means confirmed by a flush that waits until the data is stored permanently. Issuing the log write first is not enough, because caches and storage devices can reorder writes. In a redo-only design, where uncommitted changes never reach the permanent locations, rule 1 never applies, and the order is: redo records, commit record, flush, report the commit, update the permanent locations, flush, discard the records.

Related entries: Write ordering (§8.20), Crash consistency (§8.4), Exactly-once effect (§8.32), Append-only log (§8.13), Idempotency (§8.31), Shadow paging (§8.9). Further reading: [179, 514, 516].