For single-node web services, internal admin portals, release catalogs, and desktop/mobile applications, SQLite eliminates network hop latency and external database administration. Most teams hit trouble only because SQLite ships in legacy rollback-journal mode (DELETE), where any writer locks out concurrent readers.
1. Essential Connection Pragmas on Startup
Enable Write-Ahead Logging (WAL) once per database file and configure per-connection timeouts on every new handle:
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA busy_timeout = 5000;
PRAGMA foreign_keys = ON;
PRAGMA cache_size = -16000; -- ~16 MB negative KiB page cache
WALmode: Readers proceed concurrently from the snapshot while a single writer appends committed frames to the-walfile.busy_timeout = 5000: Instructs SQLite to wait cleanly up to 5 seconds instead of throwing immediateSQLITE_BUSYerrors when two HTTP requests commit milliseconds apart.
2. Never Back Up a Live WAL Database with Plain cp
Copying only app.db while omitting app.db-wal leaves recent committed transactions behind and risks torn page reads. Always use SQLite's online backup API or VACUUM INTO:
# Safe online snapshot while traffic continues running
sqlite3 /var/lib/app/app.db "VACUUM INTO '/var/backups/app-snapshot-20261123.db';"
VACUUM INTO writes a clean, compacted, standalone database file atomically without blocking ongoing readers.
3. Managing WAL Growth & Periodic Checkpointing
Under steady continuous read traffic, a long-running read transaction can prevent SQLite's automatic checkpoint (PRAGMA wal_autocheckpoint = 1000) from resetting the -wal file back to zero, occasionally causing multi-hundred-megabyte WAL growth. Guard against runaway WAL growth by:
- Keeping read transactions brief: Never hold open SQLite cursors across slow external HTTP calls or user input waits.
- Scheduling passive or truncate checkpoints cleanly: Run
PRAGMA wal_checkpoint(TRUNCATE);during low-traffic maintenance windows once active backup streams complete. - Choosing
stricttables on new schemas: AddingSTRICTtoCREATE TABLEdefinitions in modern SQLite prevents silent type mismatches when PHP or JavaScript layers bind malformed values.