Skip to content
The WAL — Log Before You Write
step 1/3

Reading — step 1 of 3

Write-Ahead Logging Fundamentals

~2 min readWrite-Ahead Log

The WAL — Log Before You Write

The Write-Ahead Log (WAL) is one of the most important concepts in database engineering. It solves a critical problem: what happens if the system crashes in the middle of writing data?

The Problem

Updating a page on disk isn't atomic. If the system crashes mid-write:

  • The page might be half-written (torn write)
  • Related pages might be inconsistent
  • The database is now corrupted

The Solution: Log First, Write Later

The WAL rule is simple: before modifying any database page, write a log record describing the change to a sequential log file.

1. Write log record: "Page 5: change byte 100 from 'A' to 'B'"
2. Only THEN modify Page 5 in the buffer pool
3. Eventually write the dirty page to the database file

If the system crashes:

  • After step 1, before step 2: replay the log → apply the change
  • After step 2, before step 3: replay the log → change is already applied (idempotent)
  • After step 3: everything is fine

Log Record Format

Each WAL record contains:

+-------+--------+--------+----------+----------+--------+
|  LSN  | Txn ID |  Type  | Page ID  | Old Data | New Data|
+-------+--------+--------+----------+----------+--------+
  • LSN (Log Sequence Number): monotonically increasing ID
  • Txn ID: which transaction made this change
  • Type: INSERT, UPDATE, DELETE, COMMIT, ABORT
  • Page ID: which page was modified
  • Old/New Data: before and after images (for UNDO/REDO)

Sequential I/O Advantage

The WAL is append-only — new records are always written to the end. This means:

Random I/O (database pages): Disk head moves to different locations
Sequential I/O (WAL):        Disk head moves forward continuously

Sequential writes are 10-100x faster than random writes on spinning disks. Even on SSDs, sequential writes are faster due to write amplification reduction.

WAL in Practice

DatabaseWAL NameLocation
PostgreSQLpg_walpg_wal/ directory
SQLiteWAL modedatabase-wal file
MySQL/InnoDBredo logib_logfile0/1

PostgreSQL's WAL is the backbone of replication — the WAL is streamed to replicas for continuous backup.

Your Task

Implement a WAL that logs all write operations (INSERT, UPDATE, DELETE) before they're applied. Add WAL ON, WAL DUMP, and WAL REPLAY commands.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…