SQLite Write-Ahead Logging and Concurrency Explained
SQLite write-ahead logging (WAL) is a journaling mode for applications that need reliable local storage while reads and writes overlap. It is especially relevant to developers building mobile, desktop, and local-first software: WAL can keep readers from waiting on a writer, but it does not turn a SQLite file into a multi-writer database server. This guide explains how WAL commits work, why checkpointing matters, and how to enable it without overlooking its deployment limits.
What Is SQLite Write-Ahead Logging?
SQLite is an embedded SQL database library: an application links to or bundles the engine and accesses a database file directly, rather than sending each query to a separate database server. The SQLite project describes the engine and its interfaces; its write-ahead logging documentation explains WAL as an alternative to SQLite’s rollback-journal mode.
In rollback-journal mode, SQLite saves old page contents before overwriting the database. In WAL mode, it leaves the original database pages in place and appends changed pages to a separate log. A transaction commits when SQLite writes a commit record to that log. The main database is updated later by a process called a checkpoint.
This ordering lets readers continue using a stable view while committed changes accumulate in the WAL. It improves overlap between reads and writes, not between independent write transactions: a WAL database still has one writer at a time.
The Problem WAL Solves
A database transaction must appear complete or not happen at all, even if an application crashes partway through an update. A journal provides the information needed to recover that consistent state. SQLite’s database file format documentation describes its database pages and the on-disk WAL format.
With the traditional rollback journal, SQLite records the original pages, updates the database file, then removes or invalidates the journal on commit. This is straightforward and reliable, but readers can be held up when a writer needs to modify the database file. WAL changes the write path: the writer appends new page versions to another file, so existing readers can continue reading the database pages they already rely on.
The trade-off is that the database’s live state can span multiple files. A WAL file is not merely disposable scratch space while a database is open; it can contain committed transactions not yet copied into the main file. Applications that copy, rename, or back up a live database must use SQLite’s supported backup mechanisms or otherwise account for the database, WAL, and shared-memory state together.
How WAL Works
A WAL is a sequence of frames. A frame contains a database page and metadata identifying that page; a commit marker indicates that the transaction’s changes are complete. SQLite appends frames instead of immediately replacing the corresponding pages in the main database.
Each read transaction establishes an end mark: the last committed point in the WAL that the reader is allowed to see. When it needs a page, SQLite looks for the newest version of that page before the reader’s end mark. If none exists, it reads the page from the main database file. Because a reader keeps the same end mark for its transaction, it sees a consistent snapshot even if a writer appends a later commit.
To avoid scanning the whole log for each page, SQLite maintains a WAL index in shared memory. This index helps locate page versions quickly. The shared-memory requirement is also a deployment constraint: all processes using the WAL database must be on the same host. WAL is not suitable for coordinating access to a database on a network filesystem between different machines.
A checkpoint copies eligible page versions from the WAL into the main database. It can run while readers are active, but it cannot overwrite pages needed by a reader’s older snapshot. A long-running read transaction can therefore hold back checkpoint progress and allow the WAL to grow. SQLite normally runs automatic checkpoints around a default threshold of 1,000 pages; applications can also inspect or schedule checkpoints explicitly.
| Behavior | Rollback-journal mode | WAL mode |
|---|---|---|
| Where writes go first | Original pages are saved to a journal; database pages are updated | Changed pages are appended to a separate WAL file |
| Commit point | Journal protocol completes | A commit record is appended to the WAL |
| Reader and writer overlap | A write may block readers while database pages are changed | Readers can continue from their snapshots while a writer appends |
| Simultaneous writers | One writer at a time | One writer at a time |
| Extra files during activity | Rollback journal | WAL and shared-memory index files |
| Work needed after commits | Journal cleanup or reuse | Checkpointing transfers pages into the database |
| Multi-host network filesystem | Depends on filesystem locking behavior | Not supported because WAL needs shared memory on one host |
WAL and rollback journaling are two ways to preserve transaction integrity, not a choice between safe and unsafe storage. WAL is often useful when a local application mixes reads with writes. Rollback mode may be simpler for workloads that rarely write, need transactions across multiple attached databases as a set, or depend on a deployment where WAL’s shared-memory requirements cannot be met.
Key Concepts and Concurrency Limits
Readers use snapshots. A reader’s end mark does not move during its transaction. This gives it a consistent view, but a transaction that remains open for a long time can prevent a checkpoint from advancing. Keep read transactions bounded and avoid holding cursors or connections open unnecessarily.
There is one writer. WAL allows readers and a writer to make progress at the same time, but separate write transactions are serialized. If an application sends writes from multiple threads or processes, they can queue or receive SQLITE_BUSY when a lock cannot be obtained promptly. Use short transactions, configure a reasonable busy timeout, and retry only operations that are safe to retry. WAL does not provide unlimited write parallelism.
Checkpointing is different from committing. A transaction is committed when its commit record reaches the WAL; it does not need to wait for every changed page to be copied into the main file. Checkpointing reclaims or reuses log space over time. A large WAL may increase read work and consume disk space, while aggressive checkpoints can add I/O and contend with active readers.
Durability depends on settings and storage. SQLite’s synchronous setting controls when commits and checkpoints synchronize data to persistent storage. Relaxing synchronization can improve throughput but may lose recently committed transactions after power loss or a hard reboot. Keep the default unless the application’s durability requirements and failure model justify a deliberate change. Filesystem, device, and operating-system behavior also matter.
SQLite coordinates locks through its virtual filesystem (VFS) layer, which adapts file operations to the host platform. On POSIX systems, the Open Group specification for fcntl() defines advisory record-lock operations; the exact locking implementation depends on SQLite’s VFS and platform. This is one reason correct filesystem locking matters and why a database file should not be casually shared across machines.
For comparison, PostgreSQL WAL and checkpointing cover a server database’s recovery log; the shared name does not mean the two systems have the same architecture or concurrency model.
Real-World Uses
WAL is a good fit for a desktop application where the UI reads frequently while background work saves state, or for a phone app that stores records locally and synchronizes them later. It can also help a local service or development tool that runs several readers alongside a modest write stream. Those deployments benefit from SQLite’s compact, in-process design without requiring a database server.
For local-first software, the local database is only one part of the design: synchronization and conflict resolution between devices still need their own rules. A local-first architecture explains those broader trade-offs. WAL only coordinates access to one database on one host; it does not replicate data, resolve conflicts, or provide a shared multi-host write service.
Avoid WAL when separate machines need to open and write the same database over a network share, when many independent writers need to make progress concurrently, or when a system requires server-managed access control and remote connections. In those cases, use a database server or a synchronization service that matches the workload rather than treating a shared file as a server.
Getting Started with WAL
Python’s standard sqlite3 module can enable WAL without a separate package. Save this as wal_demo.py and run it with python wal_demo.py:
import sqlite3
connection = sqlite3.connect("app.db", timeout=5.0)
try:
mode = connection.execute("PRAGMA journal_mode=WAL").fetchone()[0]
print(f"Journal mode: {mode}")
with connection:
connection.execute(
"CREATE TABLE IF NOT EXISTS notes "
"(id INTEGER PRIMARY KEY, body TEXT NOT NULL)"
)
connection.execute(
"INSERT OR REPLACE INTO notes (id, body) VALUES (?, ?)",
(1, "WAL keeps readers moving during writes"),
)
print(connection.execute("SELECT body FROM notes").fetchone()[0])
finally:
connection.close()
connection = sqlite3.connect("app.db", timeout=5.0)
try:
checkpoint = connection.execute(
"PRAGMA wal_checkpoint(PASSIVE)"
).fetchone()
print(f"Checkpoint status (busy, log pages, copied pages): {checkpoint}")
finally:
connection.close()
The first pragma returns the journal mode SQLite actually selected; check that it says wal rather than assuming the change succeeded. The sample commits its write before requesting a passive checkpoint. The checkpoint result reports whether it was blocked and the number of log and copied pages. PASSIVE does not wait for readers, which makes it useful for observation and routine progress without forcing a disruptive checkpoint.
The equivalent SQLite command-line checks are:
sqlite3 app.db "PRAGMA journal_mode;"
sqlite3 app.db "PRAGMA wal_checkpoint(PASSIVE);"
Watch for a growing WAL file, long-lived readers, repeated SQLITE_BUSY errors, or disk-space pressure. Diagnose active transactions before repeatedly forcing checkpoints; if a connection keeps a snapshot open, the underlying condition must be addressed. For a live backup, use SQLite’s backup API instead of copying only the main database file.
Common Misconceptions
“WAL lets multiple writers update SQLite at the same time.” It allows readers to overlap a writer, but SQLite still serializes write transactions. If write contention is a core workload characteristic, WAL alone will not solve it.
“The WAL file can be deleted after a commit.” A commit may still exist only in the WAL until checkpointed. Removing or copying only part of the live database state can lose committed data or produce an inconsistent backup.
“WAL makes SQLite safe on a network share.” It does the opposite of adding network coordination: the WAL index depends on shared memory available to processes on the same host. Use a server database or a supported replication design for multi-host access.
Related Articles
- Learn how local-first software stores and synchronizes device data.
- Compare embedded analytical workloads in the DuckDB architecture explainer.
- Understand PostgreSQL WAL, checkpoints, and crash recovery.
- Review database backup and recovery strategies before designing a live-copy process.
Changelog and Last Updated
- Changelog: Initial publication.
- Last updated: Reviewed against the SQLite WAL, file-format, and locking documentation and the POSIX file-control specification.

