Boosting SQLite read concurrency with Write‑Ahead Logging
Eliminate “database is locked” errors in read‑heavy SQLite apps by enabling Write‑Ahead Logging, verify the setting, and manage its trade‑offs.
31 Oct 2025, 04:03 UTC

Problem: “database is locked” errors in read‑heavy SQLite apps
When SQLite uses its default rollback‑journal mode, a writer obtains an exclusive lock on the main database file. While that lock is held, any reader that issues a query receives the error SQLITE_BUSY or the message “database is locked”. This blocks many read‑only workloads.
Thesis: Switch to Write‑Ahead Logging (WAL) to keep readers unblocked
WAL changes the write path so that a writer appends changes to a separate ‑wal file instead of modifying the original .db file. Readers continue to see the last committed snapshot, allowing multiple readers to run concurrently with a single writer.
How WAL works
In the default mode SQLite creates a temporary journal file, copies the original database into it, then applies changes. This requires an exclusive lock that blocks readers. In WAL mode the writer creates or appends to a ‑wal file and a ‑shm (shared‑memory) segment. The main database file stays unchanged until a checkpoint merges the WAL frames back into it. Because readers only need the main file, they can run while a writer is active.
Verification steps
- Open the database with the SQLite CLI or an application that has write permission on the file and its directory.
- Execute the configuration pragma:
The call returns the stringPRAGMA journal_mode=WAL;walif the mode change is accepted. Required permission: write access to the database file. - Confirm the mode is active:
It should returnPRAGMA journal_mode;wal. Expected check: noSQLITE_BUSYwhen a reader queries while a writer holds a transaction. - Test concurrency by opening two connections. On
conn1start a transaction and insert a row. While the transaction is open, onconn2run a simpleSELECT count(*) FROM mytable;. In WAL mode the SELECT succeeds; in journal‑mode it may raiseSQLITE_BUSY. - Inspect the directory for
‑waland‑shmfiles after writes. Run
to merge frames back into the main database and verify that thePRAGMA wal_checkpoint(PASSIVE);‑walfile shrinks.
Worked example
- Open the database:
Ensure the process can write tosqlite3_open('example.db')example.dband its containing folder. - Set WAL mode:
The command returnsPRAGMA journal_mode=WAL;walon success. - Begin a write transaction on one connection:
then insert a row:BEGIN;INSERT INTO logs (msg) VALUES ('test'); - While the transaction is open, open a second connection and query:
The query should return without error, showing that readers are not blocked.SELECT count(*) FROM logs; - Commit the transaction:
The inserted row becomes visible to all connections.COMMIT;
Trade‑offs and limitations
- WAL requires a shared‑memory segment (
‑shm) that must reside on a filesystem supporting POSIX file locking; network shares such as NFS often fail. - The
‑walfile can grow unbounded if checkpoints are delayed; long‑running read‑only connections may prevent automatic checkpoints. - Checkpoint operations briefly lock the database, so a checkpoint cannot be completely lock‑free.
Actionable closing
- Set the pragma
PRAGMA journal_mode=WAL;at database creation or via an open connection. - Verify with
PRAGMA journal_mode;that the result iswal. - Monitor the
‑shmand‑walfiles; run
periodically or rely on the defaultPRAGMA wal_checkpoint(PASSIVE);wal_autocheckpointinterval. - Ensure your backup strategy copies both the main
.dbfile and its accompanying‑waland‑shmfiles, or run a checkpoint before copying.
This blog explains enabling SQLite WAL for improved read concurrency, includes PRAGMA usage, verification steps, and trade‑offs.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.