Lesson 9 of 9 · 24m
Corruption, Operations, and Observability for a Serverless Database
What corrupts SQLite, how to diagnose integrity failures, what telemetry to collect, and the operational checklist that makes embedded databases boring.
SQLite is famously reliable, but 'SQLite corrupted itself' is usually incomplete forensics. Corruption almost always involves something outside the engine's assumptions: broken filesystem locking, power loss with misreported flush behavior, direct file manipulation, hardware failure, or copying live files incorrectly.
Start with symptoms, then preserve evidence
Typical symptoms include database disk image is malformed, database is locked, and file is not a database. Do not run random repair commands against the only copy. First preserve a byte-for-byte copy of the database and any -wal/-shm files if the process is stopped, or use the backup API if the application is live. Evidence destroyed during 'repair' cannot be analyzed.
integrity_check is the first diagnostic
PRAGMA integrity_check;A healthy result is:
okAnything else names structural issues such as missing pages, malformed B-trees, or index inconsistencies. It can take time on large databases because it walks the file. PRAGMA quick_check is faster but less thorough. Neither validates business meaning; they validate database structure.
Recovery is extraction, not wizardry
The standard last-resort path is dumping what can be read and loading it into a fresh database:
sqlite3 broken.db ".recover" > recovered.sql
sqlite3 rebuilt.db < recovered.sqlDepending on damage, rows may be missing, constraints may fail during load, and indexes may need recreation. A dump is not a time machine. Tested backups matter more than recovery heroics.
The usual root causes
- copying a live database without SQLite-aware backup,
- using WAL over network filesystems with broken locking,
- deleting
-walwhile the database is open, - filesystems or devices that lie about fsync durability,
- multiple containers writing a shared mounted file without a deliberate design,
- power loss on cheap SD cards or consumer storage,
- opening the same file through symlink or bind-mount paths that confuse lock coordination.
Instrument it like infrastructure
SQLite has no server metrics endpoint, so export application-level telemetry: lock wait seconds, transaction duration, WAL file bytes, checkpoint failures, integrity-check result, and write retries. Log migration start/end, database size, SQLite library version, and whether foreign_keys, journal_mode, synchronous, and busy_timeout were actually applied.
Operations should be boring
A healthy runbook is short: configure connections consistently, use one writer lane, checkpoint normally, back up through the API, test restores, monitor lock errors and WAL growth, and never bypass locking semantics. When those are true, SQLite disappears into the background — exactly what embedded infrastructure should do.
Field notes
- The
sqlite3CLI and your application may link different SQLite versions; checksqlite_version()in-process before debugging behavior differences. - Corruption after power loss often means storage acknowledged writes before they were durable; changing SQL rarely fixes hardware truthfulness.
- Use
PRAGMA application_idfor application-specific file identification when SQLite is your file format. - Corruption recovery without understanding cause usually produces a second corrupted database a week later.
Was this lesson helpful?