On this page
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
- > Recommended Startup `PRAGMA` Checklist
- > `BEGIN IMMEDIATE` for Concurrent Writers
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.
Recommended Startup PRAGMA Checklist
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
- tuts/databases/design_patterns/transactions_acid_and_isolation Linked here; links to this page; shared tags: #databases
- tuts/databases/sqlite3 Linked here; links to this page; shared tags: #databases
- tuts/databases/sqlite3/architecture_and_type_affinity Linked here; links to this page; shared tags: #databases
- tuts/databases/sqlite3/json_fts5_and_extensions Linked here; links to this page; shared tags: #databases
- tuts/databases/sqlite3/python_sqlite3_patterns Linked here; links to this page; shared tags: #databases
- tuts/databases/postgresql/indexes_and_explain_analyze shared tags: #databases, #performance
- tuts/databases shared tags: #databases
- tuts/databases/design_patterns shared tags: #databases
- tuts/databases/design_patterns/indexing_and_storage_engines shared tags: #databases
- tuts/databases/design_patterns/normalization_and_data_modeling shared tags: #databases