OakType / Runtime Notes

WAL checkpoint starvation: why the write rate plateaus and then falls

Domain StorageSQLite 3.40+Revised 2026-07-30

In WAL mode, writers append to the log and readers see a consistent snapshot. The catch is that the log cannot be truncated while a reader still needs an older frame — so a single long-lived reader can pin the log and turn a fast append path into a growing file.

The observable signature

Write throughput is flat, then declines in steps that coincide with checkpoint attempts. -wal file size grows monotonically past its configured limit. If you plot commits per second and WAL size on the same axis, the two curves are mirror images.

-- what the pager thinks is happening
PRAGMA wal_checkpoint(PASSIVE);
-- returns (busy, log_frames, checkpointed_frames)

-- the settings that bound the log
PRAGMA journal_size_limit = 67108864;
PRAGMA wal_autocheckpoint = 4000;

busy=1 from a passive checkpoint means a reader held a snapshot and the checkpointer could not complete. That single return value answers the question faster than any amount of filesystem-level tracing.

Why a reader can pin the log

A read transaction pins the snapshot at the frame it first observed. The checkpointer may copy frames back into the database file up to that point, but it cannot recycle frames beyond it. If an application opens a long read transaction on a connection that is rarely used — an analytics query, a lazily-consumed cursor, an ORM session kept open — the log grows for as long as that snapshot lives.

Common cause. A connection pool where one connection holds an implicit read transaction open between requests. The fix is usually PRAGMA read_uncommitted = 0 plus an explicit COMMIT at end of request, not a bigger WAL.

Mitigations that work

  1. Bound read transactions. Every reader that is not a single statement should commit on a timer.
  2. Set journal_size_limit so the file is truncated after a successful checkpoint instead of being reused at whatever size it reached.
  3. Increase wal_autocheckpoint only if the checkpoint itself is causing latency spikes; raising it does not help starvation.
  4. Run wal_checkpoint(TRUNCATE) during a maintenance window when no readers are active.
PRAGMA synchronous = NORMAL;   -- safe with WAL on a journalled fs
PRAGMA journal_size_limit = 67108864;
PRAGMA wal_autocheckpoint = 4000;
PRAGMA busy_timeout = 5000;

synchronous=NORMAL is the setting that makes WAL mode worthwhile: it fsyncs the log at checkpoint boundaries rather than on every commit. On a filesystem that orders writes correctly it survives process crashes; only an OS-level crash can lose the last few commits, which for most application data is an acceptable trade.