Lesson 2 of 9 · 27m
WAL Mode, Checkpoints, and Durability Knobs
Learn what WAL actually changes, how checkpoints interact with readers, and which durability settings belong in production defaults.
WAL mode is the setting that separates "I tried SQLite once" from "I operate SQLite deliberately." In rollback-journal mode, writers take stronger locks and readers can be blocked. In write-ahead logging mode, new pages are appended to the WAL and readers can keep reading a consistent snapshot while a writer works. It is the right default for many applications — but only if you understand what it does not solve.
Enable WAL correctly
WAL is persistent database state, not just a connection flag:
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA busy_timeout = 5000;journal_mode=WAL changes the database file's mode. synchronous controls how aggressively SQLite asks the operating system to flush writes. busy_timeout tells the connection to wait briefly when another process holds a lock instead of instantly returning SQLITE_BUSY.
NORMAL is often appropriate in WAL mode because WAL permits durability without the most aggressive synchronous behavior at every commit. FULL is stronger and slower. OFF is sometimes used for rebuildable caches; using it for valuable user data is operational negligence dressed up as performance tuning.
Readers and writers under WAL
The practical concurrency contract is simple:
many readers: yes
one writer at a time: yes
multiple simultaneous writers: noReaders do not block the writer in normal WAL operation, and the writer does not block readers. Writers still serialize. If your application opens fifty threads and all fifty want to write, SQLite does not turn that into parallel writes. It turns it into a queue — or a busy-error storm if you forgot timeouts.
Checkpoints move WAL content into the database
The WAL is not an optional accessory. Until a checkpoint copies pages back into the main database file, committed data can live in database.sqlite-wal. That is normal. Do not copy only the .sqlite file and call it a backup.
SQLite checkpoints automatically, usually when the WAL reaches about one thousand pages. You can influence behavior:
PRAGMA wal_autocheckpoint = 1000;
PRAGMA wal_checkpoint(TRUNCATE);A blocking checkpoint can be delayed by long-lived readers because checkpointing cannot overwrite pages readers still need. A database with a mysteriously growing WAL often has a leaked transaction, not a broken database engine.
Crash recovery is designed, not magical
After a crash, SQLite uses the WAL frames and checksums to recover committed state. That is robust engineering, but it assumes correct local filesystem semantics. NFS, SMB, some distributed filesystems, and unusual mount options can invalidate assumptions around locking or flush ordering. SQLite supports some network filesystem configurations in narrow cases, but the safe default is local disk.
Field notes
- Set
busy_timeouton every connection. Retry policy does not help if the driver never waits. - WAL does not create parallel writers; it improves reader/writer coexistence.
- Long transactions make WAL files grow and checkpoints stall.
- Copying a live database by copying only the main file can lose recent commits or capture inconsistent state.
synchronous=NORMALin WAL mode is a common production choice, but verify what your driver exposes and test power-loss assumptions.- Treat
database.sqlite-walas live data until checkpointed, not as temporary garbage.
Was this lesson helpful?