SQLite WAL Mode and Busy Timeouts in Small Go Services
Practical pragma settings, connection pool sizing, and checkpoint behaviour for embedded SQLite databases.
Why default journal mode stalls concurrent readers
When a single-binary service handles HTTP requests alongside a background ticker, SQLite in rollback journal mode quickly triggers SQLITE_BUSY errors as soon as a write overlaps with a read.
Switching to Write-Ahead Logging lets readers proceed concurrently with a single writer, provided the connection DSN enforces busy_timeout and foreign_keys on every opened connection.
db.SetMaxOpenConns(1) // serialize writes in-process
_, _ = db.Exec("PRAGMA journal_mode=WAL;")
_, _ = db.Exec("PRAGMA synchronous=NORMAL;")
_, _ = db.Exec("PRAGMA busy_timeout=5000;")
Separating read and write pools
In workloads where 95% of queries are lookups, keeping a dedicated read-only *sql.DB with SetMaxOpenConns(4) and a single-connection writer pool eliminates lock contention inside Go's database/sql.
Periodically running PRAGMA wal_checkpoint(TRUNCATE) during low-traffic windows keeps the -wal file bounded without stalling foreground latency.