C.W.K.
Stream
Lesson 02 of 10 · published

Corruption Prevention and Detection

~14 min · corruption, integrity, production

Level 0Scout
0 XP0/80 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

SQLite is incredibly reliable — when used correctly

SQLite is one of the most-tested pieces of software on Earth. Corruption almost always comes from outside the engine. The classic causes:

  • NFS / SMB / FUSE — broken file locking. Never put a SQLite file on network storage with a writer.
  • Killed mid-write without WAL — pre-WAL journal modes can corrupt on hard kill. WAL recovers cleanly.
  • Two processes both with the same file open via different paths — symlinks, bind mounts.
  • Hardware — bad disk, bad RAM, lying SSDs that ack writes before persisting them.
  • Restoring backups while writers are active — partial overwrite of an in-use database.

Detect corruption with PRAGMA integrity_check (full scan; slow on large DBs) or PRAGMA quick_check (faster, weaker guarantees). Run after every backup and on a schedule.

Self-reference: Pippa's SQLite + JSONL design exists partly because corruption recovery is bounded: if SQLite ever shows damage, purge the affected conversation's rows and replay from JSONL. The JSONL ground truth is the disaster-recovery mechanism, not just an audit log.

Code

Health check on a schedule·bash
#!/bin/bash
DB=/var/lib/myapp/myapp.db

result=$(sqlite3 "$DB" 'PRAGMA integrity_check')
if [ "$result" != "ok" ]; then
  echo "Corruption detected: $result" | mail -s 'SQLite corruption' admin@example.com
  exit 1
fi

External links

Exercise

Read the SQLite 'How To Corrupt' page in full. List every cause that applies to your environment. For each, note the mitigation (PRAGMA setting, deployment choice, monitoring). Then add a periodic PRAGMA integrity_check alert to one of your services.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.