SQL Write-Ahead Logging and Crash Recovery
A SQL write-ahead log (WAL) is an append-only sequence of records describing database changes. The database writes the relevant log records to durable storage before it writes the changed data pages, so recovery can repeat committed changes after a crash.
The problem WAL solves
A database usually keeps frequently accessed data pages in memory. A page is a fixed-size block containing rows, indexes, or other structures. Changing a row normally changes the in-memory page first; writing that page to storage can happen later.
That delay is useful for performance, but it creates a failure window. Consider a transfer:
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
The database may have changed both pages in memory without yet writing either page to disk. If the machine loses power, those changes disappear. If it wrote only the first page, the stored database might show money removed from one account but not added to the other.
Writing every changed page immediately would reduce some of this risk, but it would be slow and would not by itself make multi-page transactions atomic. Storage writes can be reordered, interrupted, or only partly completed. The database needs a durable record of what was intended, separate from the pages being updated.
That record is the WAL. It turns many random page writes into a mostly sequential append, and it gives recovery something reliable to process after a crash.
What a redo record contains
A WAL is not normally a copy of the SQL statement. It contains records in a format designed for recovery. A record might identify a page and describe how to apply a change to that page—for example, inserting an index entry or setting a row field to a new value. Such a record is called a redo record because recovery can redo, or replay, the operation.
Records commonly also carry a transaction identifier and a log sequence number (LSN). An LSN is an ordered position in the log. A data page stores the LSN of the latest log record whose change it includes, allowing recovery to avoid applying the same change twice.
The exact format is database-specific. Some systems use physical descriptions such as “write these bytes at this offset.” Others use more logical operations, or a mixture. The important property is that the record contains enough information to reconstruct the change without rerunning application code or issuing the original SQL.
The write-ahead rule
The central rule is simple:
A data page cannot be written to durable storage until the WAL records describing its changes are durable there first.
This is the “write-ahead” part. A page in memory may contain changes whose log records have merely been copied into the operating system’s buffers, but the database must flush those log records before flushing the page. A flush, often implemented using an operating-system or storage durability operation such as fsync, asks the storage stack to make buffered bytes survive a crash.
The database can therefore use a steal policy: it may write a dirty page—that is, a changed page not yet associated with a committed transaction—to storage before the transaction commits. WAL makes that safe because the change is recoverable, and a recovery process can remove or hide an incomplete transaction according to that database’s design.
It can also use a no-force policy: committing a transaction does not require flushing every page it changed. Instead, commit requires the relevant WAL, including a commit record, to be durable. The data pages can be written later in batches.
What happens during a transaction
For a simplified transaction, the sequence is:
- SQL statements change pages in the buffer cache.
- The database creates redo records describing those changes and appends them to the WAL.
- Before any changed page is flushed, the corresponding WAL records are flushed.
- On
COMMIT, the database appends a commit record and makes the required portion of the WAL durable. - The transaction is reported as committed.
- The changed data pages may be flushed later.
Step 4 is the durability boundary. If the server tells the client that COMMIT succeeded, the durable log must contain enough information to establish that fact after a restart. A configuration that acknowledges commits before the log reaches durable storage can improve latency, but it weakens the durability guarantee and may lose acknowledged transactions in a power failure.
Recovery after a crash
After an unclean shutdown, the database reads the WAL from a known recovery point, usually associated with a checkpoint. A checkpoint is a record of recovery progress; it limits how far back normal recovery needs to scan, although it does not replace the WAL.
Recovery generally proceeds in two conceptual stages:
- Analysis determines which transactions were committed, still in progress, or otherwise incomplete when the crash occurred.
- Redo scans the log and reapplies changes that might be missing from data pages.
Suppose a transaction’s commit record reached durable storage, but one of its modified pages did not. Recovery finds the transaction’s redo records and applies them to the page. The committed transaction is restored even though its original page write never finished.
If the page had already been written, its stored LSN tells recovery that the change is present, so replay can skip it. Replaying the same WAL after a second crash is consequently designed to be safe; recovery is intended to be repeatable rather than dependent on knowing exactly which page writes completed.
A transaction with no durable commit record is not treated as committed. Some databases undo its changes during recovery. Others can leave the changes on pages but mark the transaction invisible to later queries, then clean them up. The details differ, but the rule is the same: a client-visible commit requires a durable commit record, not merely a changed data page.
Where you may have seen WAL
Operational messages such as “replaying WAL,” “waiting for WAL flush,” or “recovery starting from checkpoint” refer to these steps. Replication systems also commonly ship WAL records to another server, where they are replayed to keep a standby current. In both cases, the log is valuable because it describes durable database changes in order—not because it is a backup of SQL text.
WAL therefore separates two jobs: the log provides a durable history of changes, while data pages provide an efficient current representation. Writing the history first is what lets a database acknowledge a committed transaction without forcing every affected page to disk immediately, and still reconstruct that transaction after a crash.