Skip to content

WAL Internals: The Database Mechanism That Makes Crashes Survivable

Every database promises durability. Power failures happen, kernels panic, and storage fails at inconvenient times. Write-Ahead Logging is the reason modern databases can make durability guarantees while still delivering high performance — from PostgreSQL replication to Kafka commit logs.

Published
Reading time
6 min
Topics
database, system-design
Cover illustration for "WAL Internals: The Database Mechanism That Makes Crashes Survivable"

Every database promises durability.

You run a transaction.

UPDATE accounts
SET balance = balance - 100
WHERE id = 42;

COMMIT;

The database responds successfully.

At that moment, a contract exists between the database and the application:

This change will not disappear.

The problem is that hardware doesn’t care about contracts.

Power failures happen. Kernels panic. Processes crash. Machines reboot unexpectedly. Storage devices fail at inconvenient times.

Without a mechanism to bridge the gap between volatile memory and durable storage, every committed transaction would be a gamble.

Write-Ahead Logging (WAL) is the reason modern databases can make durability guarantees while still delivering high performance.

And once you understand WAL deeply, many other systems suddenly become easier to understand - from PostgreSQL replication and Kafka commit logs to distributed consensus systems and cloud-native databases.

The Fundamental Problem

Consider the simplest possible database update.

A row is modified.

The corresponding page in memory changes.

Eventually that page is written back to disk.

At first glance, this sounds reasonable.

The problem appears when failure occurs between those steps.

Memory Updated
      ↓
Power Failure
      ↓
Disk Never Updated

The transaction was acknowledged.

The data is gone.

For a banking system, payment processor, inventory platform, or trading system, this is unacceptable.

The database needs a way to guarantee recovery even if the machine crashes immediately after a commit.

This is exactly why WAL exists.

The Core Idea: Log First, Data Later

Most engineers initially assume databases write data files directly.

In reality, modern databases rarely work this way.

Instead, they follow a simple rule:

Never write the actual data page before recording the change in the log.

The sequence becomes:

Transaction
      ↓
Generate WAL Record
      ↓
Persist WAL
      ↓
Acknowledge COMMIT
      ↓
Write Data Pages Later

That ordering is everything.

Because once the log is safely stored, the database can reconstruct the missing changes even if the data files were never updated.

The log becomes the source of truth during recovery.

Why WAL Is Faster Than Writing Data Directly

Durability is only half the story.

The surprising part is that WAL also improves performance.

Consider a busy database receiving thousands of writes per second.

Without WAL, every update might require writing random pages scattered throughout storage:

Page 17
Page 420
Page 8921
Page 53
Page 1042

Random I/O is expensive.

Storage devices perform best when writing sequentially.

A WAL file is essentially an append-only stream:

Record 1
Record 2
Record 3
Record 4
...

The database continuously appends new entries.

No seeking.

No page lookups.

No random writes.

Just sequential persistence.

This allows databases to provide durability while minimizing write amplification and storage overhead.

It’s one of the rare engineering patterns that improves both reliability and performance simultaneously.

What Actually Gets Written Into WAL?

A common misconception is that WAL stores copies of entire tables.

It doesn’t.

Most systems store compact change records.

A simplified WAL entry might look like:

LSN: 1001
UPDATE
Page: 42
Offset: 128
Old Value: 1000
New Value: 900

Each operation generates a corresponding log record.

These records are appended continuously to WAL segments.

When recovery occurs, the database replays these operations to reconstruct missing changes.

Think of it as recording instructions rather than storing full snapshots.

The Importance of LSNs

Every WAL record receives a unique Log Sequence Number (LSN).

An LSN acts as a globally ordered position within the log stream.

LSN 1000
LSN 1001
LSN 1002
LSN 1003

Database pages also track the most recent LSN applied to them.

This allows recovery systems to answer an important question:

Has this change already been applied?

If a page contains LSN 1002 and the WAL contains records up to 1005, recovery knows exactly which operations still need replaying.

This is one of the key mechanisms that makes crash recovery deterministic.

Crash Recovery in Practice

Imagine the following sequence:

Transaction Begins
Update Account A
Update Account B
Crash

The system restarts.

Recovery generally follows three phases:

1. Analysis

Determine:

  • Which transactions were active
  • Which pages were dirty
  • Where recovery should begin

2. Redo

Replay committed operations from WAL.

This ensures all durable transactions are reflected in the database.

3. Undo

Rollback incomplete transactions that never committed.

The result is a consistent database state as if the crash never happened.

This approach, heavily influenced by the ARIES recovery model, remains foundational across modern database engines.

Why Checkpoints Exist

Imagine replaying months of WAL every time a database restarts.

Recovery would take hours.

Possibly days.

To avoid this, databases create checkpoints.

A checkpoint effectively says:

Everything up to this position is safely stored in the data files.

Now recovery only needs to process newer WAL records.

Instead of replaying years of history, the database replays a much smaller window of changes.

Checkpoint design becomes an important tradeoff:

  • Frequent checkpoints reduce recovery time
  • Frequent checkpoints increase I/O pressure
  • Infrequent checkpoints reduce overhead
  • Infrequent checkpoints increase restart time

Production databases continuously balance these competing concerns.

WAL Is Also the Foundation of Replication

One of the most elegant aspects of WAL is that the same mechanism used for crash recovery can also power replication.

Instead of sending full database pages across the network, the primary database streams WAL records.

Primary
    ↓
WAL Stream
    ↓
Replica
    ↓
Replay Changes

This is how PostgreSQL streaming replication works.

The replica simply behaves like a continuously recovering database.

As new WAL records arrive, they are replayed locally.

The architecture is remarkably efficient because the database is already generating the log for durability.

Replication becomes a byproduct of an existing mechanism rather than a separate subsystem.

Production Realities Engineers Eventually Encounter

Theoretical explanations often stop at recovery.

Production systems reveal additional challenges.

WAL Devices Become Bottlenecks

Commit latency frequently depends on WAL flush performance.

Not CPU.

Not query execution.

Not storage throughput.

The bottleneck becomes:

fsync()

If the WAL device slows down, transaction latency increases immediately.

This is why high-performance deployments often place WAL on dedicated storage.

Checkpoint Storms

Large checkpoints can trigger bursts of disk activity.

Symptoms include:

  • Sudden latency spikes
  • Increased I/O wait
  • Throughput degradation
  • Replica lag

Many production incidents trace back to aggressive checkpoint behavior rather than query execution itself.

Replication Lag

Generating WAL is usually faster than replaying it.

Under heavy workloads:

Primary WAL Generation
>
Replica WAL Replay

The backlog grows.

Recovery Point Objectives begin drifting.

Storage consumption increases.

Eventually operators must choose between scaling replicas, reducing write volume, or accepting higher lag.

Beyond Databases

One of the most useful mental shifts for senior engineers is recognizing that WAL is not really a database feature.

It’s a systems pattern.

The pattern is:

Record Intent
      ↓
Persist Intent
      ↓
Apply Change
      ↓
Recover By Replay

You can see this pattern everywhere:

  • PostgreSQL
  • MySQL
  • Kafka
  • Raft logs
  • Distributed storage systems
  • Filesystem journals
  • Cloud database replication systems

Different implementations.

Same fundamental idea.

A durable, ordered log becomes the foundation for recovery, replication, consistency, and fault tolerance.

Final Thoughts

Write-Ahead Logging is one of those technologies that quietly powers nearly every large-scale system while remaining invisible to most application developers.

It solves a deceptively difficult problem:

How do you guarantee durability without destroying performance?

The answer is elegantly simple:

Don’t write the data first.

Write the history of the change first.

Everything else - crash recovery, replication, checkpoints, group commits, distributed logs, and consensus systems - builds on top of that idea.

Once you start viewing systems through the lens of log-first architecture, you’ll notice the same pattern appearing everywhere from databases and filesystems to distributed consensus and event streaming platforms.

And that’s when WAL stops being a database feature and starts becoming a fundamental systems design principle.

Reactions

Comments

Leave a comment