SQLite WAL mode: concurrent readers, one local writer
Enable SQLite WAL mode so readers keep seeing a stable snapshot while one writer appends changes; verify with PRAGMA and watch for long transactions.
23 Jul 2025, 21:35 UTC

The problem: readers and writers taking turns
Your application database works fine until a background report starts reading while the app writes. With SQLite’s default DELETE journal mode, writers and readers can take turns blocking each other. The practical takeaway: for a local SQLite database with mixed read/write traffic, enabling Write-Ahead Logging (WAL) lets one writer and many readers run at the same time, but it is not a free pass—long-running transactions and network storage can turn it into a maintenance problem.
What WAL actually changes
WAL stands for Write-Ahead Logging. Instead of overwriting pages in the main database file, SQLite appends changes to a separate -wal file. Readers operate on a stable snapshot of the database as it existed when their read transaction began, so they do not block the writer and the writer does not block them. A -shm shared-memory file acts as an index for readers and writers. The default journal mode is DELETE, so you must explicitly choose WAL if you want this concurrency model.
| Behavior | DELETE journal (default) | WAL mode |
|---|---|---|
| Writers and readers | Can block each other | One writer, many concurrent readers |
| Where changes go | Main database file, with rollback journal | Separate -wal file |
| Extra files | Temporary rollback journal | -wal and -shm |
| Best for | Read-only or single-user local databases | Local apps with mixed read/write traffic |
Enable WAL and verify it
Run these in a terminal against your application database. Replace /path/to/app.db with the actual path. You need write permission on the database file and its directory, because SQLite creates the -wal and -shm files next to it.
sqlite3 /path/to/app.db "PRAGMA journal_mode;"
Expect delete on a database that has never been switched. Then enable WAL:
sqlite3 /path/to/app.db "PRAGMA journal_mode=WAL;"
Expect wal. The setting persists in the database file header, so later connections normally open in WAL mode without repeating the pragma. Confirm the sidecar files exist:
ls -la /path/to/app.db*
You should see app.db-wal and app.db-shm alongside the main file.
Changing journal mode can fail if another connection holds a lock or the database file is read-only. If it fails, close other connections or check file permissions before retrying.
One open read transaction can make -wal grow forever
Checkpointing copies committed changes from the -wal file back into the main database. SQLite supports PASSIVE, FULL, RESTART, and TRUNCATE checkpoint modes through PRAGMA wal_checkpoint. A long-running read transaction holds a stable snapshot and prevents checkpoint advancement. While that transaction stays open, every committed write remains in -wal, so the file can grow without bound.
Use this diagnostic to confirm the behavior:
- Session A:
sqlite3 /path/to/app.db "BEGIN; SELECT count(*) FROM orders;"Leave the transaction open. - Session B:
sqlite3 /path/to/app.db "INSERT INTO orders (total) VALUES (99);"The write commits. - Session A still sees the pre-insert count until it runs
COMMIT;orROLLBACK;.
If Session A stays open for minutes or hours while Session B keeps writing, app.db-wal keeps growing. Run PRAGMA wal_checkpoint(TRUNCATE); to copy WAL contents back to the main database and truncate the -wal file to zero bytes. If the checkpoint cannot make progress, an open read transaction is the likely cause. Checkpointing requires write access and may briefly contend with writers.
When WAL is the wrong choice
- Network filesystems: Do not use WAL on NFS, SMB, or other network drives where shared memory and byte-range locking are inconsistent. Database corruption is possible.
- Write-heavy workloads: Only one writer can commit at a time, so writes still serialize even though readers are not blocked.
- Read-only databases: No concurrency benefit.
Practical check before you ship it
- Run
PRAGMA journal_mode;and confirm it returnswal. - Confirm
app.db-walandapp.db-shmexist next to the database. - Run a read transaction in one connection and a write in another; confirm the reader sees the old snapshot until it commits or rolls back.
- Run
PRAGMA wal_checkpoint(TRUNCATE);and confirm the -wal file shrinks while committed rows remain readable.
If you need to revert, run PRAGMA journal_mode=DELETE; after checkpointing and closing active transactions. This removes WAL semantics. Delete the sidecar files only after a clean checkpoint and with no connections open.
WAL mode is a solid default for local application databases with mixed traffic, but it trades simplicity for concurrency. Treat the -wal file as something to monitor, not ignore.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.