Skip to content

Lesson 5 of 9 · 26m

WAL Mode: The Feature That Changed SQLite Production Use

How write-ahead logging decouples readers and writers, what the -wal and -shm files are, checkpointing behavior, and the operational cases where WAL is the wrong choice.

Write-ahead logging is the reason modern SQLite can serve serious read-heavy production workloads. It does not create multi-writer concurrency. It changes where writes land and what readers look at, so readers and one writer can coexist without constantly blocking each other.

The mechanism in one picture

In rollback-journal mode, commit means copying old pages aside, overwriting the database file, then deleting or invalidating the journal. In WAL mode, commit means appending new versions of pages to db-wal and recording a commit frame:

readers -> stable snapshot in main db + older WAL frames
writer  -> appends new page versions to db-wal
checkpoint -> moves WAL pages back into db file when safe

A reader in WAL mode sees a snapshot: the database state as of the WAL frame when its read transaction began. New commits do not invalidate that view. This is why WAL readers are unusually consistent for reporting queries and UI screens that issue several SELECTs.

Enable and inspect it

PRAGMA journal_mode = WAL;
PRAGMA wal_checkpoint;

Typical deployment settings:

PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA busy_timeout = 5000;

journal_mode persists in the database file. busy_timeout does not. The WAL file can grow between checkpoints because readers may pin old frames and because write volume may outpace checkpointing.

Checkpointing is routine maintenance, not an error

SQLite auto-checkpoints after a threshold, traditionally about 1000 pages. Manual checkpoint modes matter operationally:

PRAGMA wal_checkpoint(PASSIVE);   -- checkpoint what can be done without blocking
PRAGMA wal_checkpoint(TRUNCATE);  -- wait, then truncate the WAL file
PRAGMA wal_checkpoint(RESTART);   -- restart sequence after checkpoint

A passive checkpoint that cannot finish usually means a long-lived reader is pinning old frames. A constantly huge -wal file is often a connection leak or a reporting query held open for minutes, not proof that WAL is broken.

The limitations are architectural

WAL requires shared-memory coordination through the -shm file for processes on the same machine. That is why WAL is unsuitable or unreliable on many network filesystems. NFS, SMB, and assorted distributed filesystems have imperfect lock semantics, and SQLite depends on correct locking more than it depends on fast writes.

Also, WAL does not make multiple simultaneous writers possible. It allows many readers plus one writer. Writers still serialize. If the write path is inherently multi-writer and high-volume, a client/server database is the honest answer.

Field notes

  • Never delete -wal or -shm while any process may have the database open; you can destroy committed transactions.
  • Backups must account for WAL state; SQLite backup APIs handle this correctly, raw file copies often do not.
  • WAL is excellent for local services, mobile apps, desktop apps, edge nodes, and embedded control planes.
  • For read-only immutable files, PRAGMA query_only and immutable open flags can eliminate locking overhead, but only when immutability is genuinely guaranteed.

Was this lesson helpful?

Sign in to rate