On this page
  1. WAL Mode & PRAGMA Tuning
  2. Contents
  3. Why Enable WAL Mode
  4. Recommended Startup PRAGMA Checklist
  5. BEGIN IMMEDIATE for Concurrent Writers

WAL Mode & PRAGMA Tuning

Prev: Architecture & Type Affinity | Up: sqlite3 | Next: JSON, FTS5 & Generated Indexes
Rel: Transactions, ACID & Isolation Levels, Python sqlite3 Patterns
Ref: SQLite — Write-Ahead Logging
Tags: #public #databases #sqlite3 #performance


Contents


Why Enable WAL Mode

In SQLite's default rollback-journal mode, writers block readers. Switching to WAL (Write-Ahead Logging) allows multiple concurrent readers alongside one writer with significantly faster commits.


Run these per connection on open (note foreign_keys and busy_timeout are per-connection, while journal_mode = WAL persists in the database header):

PRAGMA journal_mode = WAL;        -- Concurrent reads during writes
PRAGMA synchronous = NORMAL;      -- Fast and crash-safe in WAL mode
PRAGMA foreign_keys = ON;         -- Disabled by default in SQLite!
PRAGMA busy_timeout = 5000;       -- Wait up to 5s on lock instead of failing immediately
PRAGMA cache_size = -20000;       -- ~20 MiB page cache (negative = KiB)
PRAGMA temp_store = MEMORY;       -- Keep temp B-Trees in RAM

BEGIN IMMEDIATE for Concurrent Writers

When a transaction will read and then write, start it with BEGIN IMMEDIATE instead of deferred BEGIN so SQLite acquires the write lock upfront (honoring busy_timeout) rather than failing mid-transaction with SQLITE_BUSY.

Linked from

Related notes