Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Fix SQLite concurrency bottlenecks and SQLITE_BUSY database locks using Write-Ahead Logging. Learn PRAGMA journal_mode=WAL, checkpointing, and tuning.
Quick answer
SQLite Write-Ahead Logging (WAL) replaces the traditional rollback journal with an append-only WAL file. Enabling WAL mode allows concurrent readers to query the database simultaneously without blocking writers, and permits writers to commit transactions without blocking active readers.
FAQs
Does PRAGMA journal_mode = WAL need to be run on every new database connection?
No. The journal mode is written into the database header and persists across connections and application restarts.
Why are there .db-wal and .db-shm files next to my SQLite database?
The -wal file contains un-checkpointed transaction writes. The -shm file is a shared-memory index that allows concurrent readers to locate pages in the WAL file rapidly.
Is WAL mode safe against power failures and crashes?
Yes. In WAL mode with synchronous = NORMAL, SQLite guarantees transactional consistency and crash durability without file corruption.
Can multiple writers write at the exact same time in SQLite WAL mode?
No. SQLite allows unlimited concurrent readers alongside a single active writer, but multiple concurrent writes still queue sequentially.
Build fast text search directly in PostgreSQL without external search engines. Master to_tsvector, to_tsquery, relevance ranking with ts_rank, and GIN indexes.
Learn how to cache complex analytical queries with PostgreSQL materialized views. Master REFRESH MATERIALIZED VIEW CONCURRENTLY, indexing, and scheduling.
SQLite is widely deployed across mobile apps, embedded devices, local development environments, and edge servers. However, developers frequently encounter SQLITE_BUSY: database is locked errors when multiple application threads attempt to read and write to the database concurrently.
By default, SQLite operates in rollback journal mode. In rollback mode, a write transaction acquires an exclusive lock on the entire database file, blocking all read queries until the write completes. Switching SQLite to Write-Ahead Logging (WAL) mode dramatically improves concurrency by allowing readers and writers to operate simultaneously.
How the rollback journal causes database locking
Under default rollback journal mode (PRAGMA journal_mode = DELETE), SQLite preserves transaction safety by copying original unmodified disk pages into a separate journal file before modifying the main database file. Review our guide on fixing SQLite database is locked errors for error patterns.
While a writer holds the lock and modifies the database file, no other process can read. If a read query starts, writers must wait. On multi-threaded web servers or desktop applications, this mutually exclusive locking quickly causes query timeouts and lock contention.
How Write-Ahead Logging (WAL) works in SQLite
When you enable WAL mode, SQLite reverses the write process:
Original database untouched: Write operations append new changes to a separate database.db-wal file instead of writing directly to the main database file.
Readers scan snapshots: Read queries read original pages from the main database file, combined with the latest committed versions in the WAL file.
Non-blocking concurrency: A writer appending to the WAL file does not interfere with readers scanning historical pages. Multiple readers operate concurrently with an active writer.
Enabling WAL mode in SQLite
-- Check current journal mode (defaults to 'delete')PRAGMA journal_mode;-- Enable Write-Ahead Logging (persists across connections!)PRAGMA journal_mode = WAL;-- Verify mode change (returns 'wal')-- SQLite creates two additional companion files:-- 1. database.db-wal (the write-ahead log)-- 2. database.db-shm (shared-memory index for fast WAL reading)
As detailed in the SQLite WAL documentation, the WAL journal mode setting is persistent. Once set on a database file, all future connections automatically use WAL mode until explicitly changed back.
Understanding the checkpoint process
As transactions commit, the -wal file grows. To prevent the WAL file from growing indefinitely and slowing down read performance, SQLite periodically transfers committed pages from the WAL file back into the main database file. This transfer is called a checkpoint.
By default, SQLite automatically triggers a passive checkpoint whenever the WAL file reaches 1,000 pages (approximately 4 megabytes). You can inspect or manually trigger checkpoints using PRAGMA wal_checkpoint.
Managing SQLite checkpoints
-- Check checkpoint status:-- Returns (busy, log_pages, checkpointed_pages)PRAGMA wal_checkpoint(PASSIVE);-- Force all WAL pages into main database file (waits for readers to clear)PRAGMA wal_checkpoint(TRUNCATE);
Critical PRAGMAs for high-performance SQLite deployments
To maximize concurrency and prevent SQLITE_BUSY errors in multi-threaded environments, configure these companion PRAGMAs on every connection:
Production SQLite connection configuration
-- 1. Enable WAL mode (database-level setting)PRAGMA journal_mode = WAL;-- 2. Set busy timeout to 5000 milliseconds (waits instead of throwing SQLITE_BUSY immediately!)PRAGMA busy_timeout = 5000;-- 3. Set synchronous to NORMAL (completely safe in WAL mode, much faster writes)PRAGMA synchronous = NORMAL;-- 4. Store temporary tables in memoryPRAGMA temp_store = MEMORY;
According to SQLite PRAGMA journal_mode reference, setting synchronous = NORMAL in WAL mode avoids frequent disk syncs on every commit while still guaranteeing zero database corruption during an application crash.
Limitations of WAL mode
While WAL mode is vastly superior for local and server workloads, note these constraints:
Network filesystems: WAL mode requires shared-memory primitives and will fail or corrupt data on networked filesystems like NFS or SMB.
Companion files: You must keep the .db-wal and .db-shm files alongside the main .db file; deleting them while the database is open causes corruption.
Single writer limit: WAL mode allows concurrent readers alongside one writer, but does not permit multiple writers to write simultaneously.
Actionable checklist for configuring SQLite WAL mode
Enable WAL mode: run PRAGMA journal_mode = WAL on initial database creation.
Configure busy_timeout: always set PRAGMA busy_timeout = 5000 in application connection pool initialization.
Set synchronous = NORMAL: improve write throughput safely on SSDs by setting synchronous to NORMAL.
Avoid network shares: ensure SQLite database files reside on local POSIX or NTFS filesystems, never NFS or CIFS.
Monitor WAL file size: verify that automated checkpointing keeps the .db-wal file under a few megabytes.
Checkpoint before backups: run PRAGMA wal_checkpoint(TRUNCATE) before copying the raw database file for backups.
Wrapping up
Enabling Write-Ahead Logging is the most impactful configuration change you can make to a SQLite database. By decoupling readers from writers and eliminating full-file database locks, WAL mode provides high concurrency for modern application workloads. When drafting schema scripts and SQLite queries, format your code with sqlfmt or explore our pricing tiers for automated formatting.