Diagnosing 400ms tail spikes from SQLite WAL checkpoints
SQLite in WAL mode handles concurrent readers remarkably well, which makes it tempting to forget about checkpointing altogether. On a small metadata service doing ~60 writes/sec, our median write latency stayed under 2 ms while p99.9 jumped past 400 ms every fifteen seconds.
What caused the stalls
By default, wal_autocheckpoint triggers at 1,000 pages (~4 MB). Whichever writer commits the 1,000th page ends up running a synchronous PASSIVE checkpoint inline before its transaction returns, flushing dirty frames to the main database file and calling fsync.
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA wal_autocheckpoint = 0;
Moving checkpoints off the request path
Disabling automatic inline checkpoints and running a dedicated background ticker every 5 seconds with PRAGMA wal_checkpoint(PASSIVE) removed the latency spikes immediately. To prevent unbounded WAL growth if a long-lived reader pins an old snapshot, we also alert whenever the -wal file exceeds 64 MB.
After the change, p99 write latency dropped from 410 ms to 4.8 ms with no change to hardware or schema.