Enable SQLite Write-Ahead Logging for Concurrent Reads and Writes
Switch SQLite to WAL mode so readers and writers no longer block each other, with steps to enable, verify, and revert it safely.
08 Nov 2025, 16:21 UTC

The problem: default SQLite mode serializes readers and writers
In SQLite's default rollback journal mode, a write transaction acquires an exclusive lock that prevents readers from seeing the new data until the transaction commits, and active readers can prevent a writer from committing. In a multi-process application—such as several web workers or background jobs opening the same database file—this serialization becomes a bottleneck. The useful takeaway is that Write-Ahead Logging (WAL) mode lets readers operate on a consistent snapshot while a writer commits, and lets the writer proceed without waiting for readers, at the cost of requiring a local filesystem and two sidecar files.
How WAL mode works
WAL stands for Write-Ahead Logging. Instead of modifying the main database file directly, SQLite appends changes to a separate write-ahead log file (the -wal file) and uses a shared-memory file (the -shm file) to coordinate readers and writers. A checkpoint operation copies committed changes from the -wal file back into the main database file. Readers see a snapshot taken when their transaction starts, so a long-running read transaction can delay checkpointing and cause the -wal file to grow.
Prerequisites
- SQLite version 3.7.0 or newer; all application instances must use a WAL-capable library.
- The database file must reside on a local filesystem that supports shared memory and reliable byte-range locking, such as ext4, XFS, NTFS, or APFS. Do not use WAL on NFS, SMB, or other network filesystems.
- Your process needs write permission on the database file and on the directory containing it, because SQLite creates
-waland-shmfiles next to the main database file. - Close or account for any long-running read transactions before enabling WAL on a busy production database.
Enable WAL mode
1. Check the current journal mode
Run this in the sqlite3 CLI or from your application's SQL interface:
sqlite3 /path/to/your-database.sqlite "PRAGMA journal_mode;"
Where to run: any terminal with access to the database file. Required permissions: read access to the database file. Expected result: delete for the default rollback journal mode, or wal if WAL is already enabled.
2. Switch the database to WAL
sqlite3 /path/to/your-database.sqlite "PRAGMA journal_mode=WAL;"
Where to run: terminal or application startup script. Required permissions: write access to the database file and write/create access to its directory. Expected result: the statement returns wal. This setting persists in the database file header until you change it again, so you do not need to run it on every connection.
Risk: running this on a network filesystem can lead to silent corruption or lock failures because WAL depends on shared memory and byte-range locking that network filesystems often do not provide reliably.
3. Confirm the sidecar files exist
ls -la /path/to/your-database.sqlite*
Expected checks: alongside your-database.sqlite, the directory should list your-database.sqlite-wal and your-database.sqlite-shm while connections are open. If these files are missing after enabling WAL, the filesystem may not support the required locking, or the directory is not writable.
Tune automatic checkpointing (optional)
SQLite automatically checkpoints the -wal file when it reaches 1,000 pages by default. You can adjust this threshold:
sqlite3 /path/to/your-database.sqlite "PRAGMA wal_autocheckpoint = 2000;"
A higher value means fewer checkpoints but a potentially larger -wal file; a lower value means more frequent main-file writes. This pragma is a hint, not a hard guarantee, and the default of 1,000 pages is suitable for most workloads.
Verify concurrent read and write access
To confirm the behavior without relying on claimed results, open two independent sqlite3 sessions against the same WAL-mode database:
- Session A:
BEGIN; SELECT * FROM large_table;and leave the transaction open. - Session B:
INSERT INTO ...;followed byCOMMIT;. - Session C: open a new read-only connection and query the same table.
In WAL mode, Session C should see the pre-commit snapshot, and Session B should not be blocked by Session A's open read transaction. However, Session A's long-running read can prevent the -wal file from being checkpointed, so close read transactions promptly in production.
Do not manually delete -wal or -shm files while any process has the database open. Crash recovery is automatic on the next valid open.
Limitations
- SQLite still allows only one writer at a time; WAL removes reader/writer blocking, not writer/writer contention.
- Long-running read transactions can block checkpoints and allow unbounded
-walgrowth, which slows crash recovery. - Older SQLite clients may refuse to open a WAL-mode database. Verify every deployment uses a WAL-capable version.
- The
-waland-shmfiles must remain with the main database file; moving or renaming the database without them breaks the WAL state.
Practical check after enabling WAL
sqlite3 /path/to/your-database.sqlite "PRAGMA journal_mode; PRAGMA wal_checkpoint(TRUNCATE);"
Expected checks: the first pragma returns wal. The checkpoint pragma returns one row with three columns—busy, log, and checkpointed—and afterward the -wal file should be small or near zero if no transactions are active.
Revert to rollback journal mode
Because enabling WAL changes the database file header, you can revert it when WAL is unsuitable:
sqlite3 /path/to/your-database.sqlite "PRAGMA wal_checkpoint(TRUNCATE); PRAGMA journal_mode=DELETE;"
Where to run: terminal with write access to the database file and directory. Expected result: the final pragma returns delete. Afterward, the -wal and -shm files may be removed automatically once no connections remain open. Do not delete them manually while connections are active.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.