Skip to content
BENIBIX Learn

Write-Ahead Log (WAL)

Development🗃️ DatabasesRelational Databases

Also known as WAL, Write-Ahead Logging, Redo Log, Transaction Log

A log the database writes each change to first. Then a crash loses nothing.

Think of it like

Think of it like a diary you write in first. If you get called away, it says what you meant to do.

Example

PostgreSQL writes every change to its WAL before its tables. After a power cut, it replays the WAL and loses no saved order.

Why it matters

It is how a database keeps saved changes safe through a crash. Copies of the log also feed backups and replicas.

Definition

A write-ahead log is a file where a database writes each change before doing it. The change goes to the end of the log first, which is fast. Only then does the database say the change is saved. Later, it copies the change into its main data files. If the machine crashes, the database reads the log and finishes the work.
The change goes to the log first, so a crash can be replayed.

Common mistake

Putting the log on a slow or full disk. Every save waits on the log, so the whole database slows.

Key takeaways

  1. 1
    Log first. Write the change to the log first.
  2. 2
    Then the data. Main files are updated later.
  3. 3
    Replay on crash. The log finishes unfinished work.