Lesson 5 of 13 · 18m
Crash-Consistent vs. Application-Consistent
A file copy of a live data directory is worse than a crash; engines only self-repair when the snapshot is atomic.
cp -r /var/lib/postgresql /backups/ on a running database produces something that looks like a backup and restores like a crime scene. Understanding why requires three distinct levels of "consistent," and the middle one is where the nuance lives.
Torn, crash-consistent, application-consistent
Torn. A file-by-file copy of a live directory visits each file at a different instant. The WAL segments copied at 02:01 describe writes to heap pages copied at 02:04. Postgres's own documentation says flatly that copying the data directory while the server runs is useless — the copy is worse than a crash, because crash recovery assumes one instant of interruption, and a torn copy supplies many, mutually contradictory ones. This is also what tar czf pg.tar /var/lib/postgresql gives you, and what rsync of the live data dir gives you, and what tarring a Docker named volume mid-write gives you.
Crash-consistent. An instantaneous image of the whole filesystem — an LVM snapshot, an EBS snapshot under fsfreeze, a ZFS snapshot taken atomically. The engine treats it exactly like the power cord was pulled. This is survivable by design: InnoDB writes pages twice through a doublewrite buffer specifically so torn pages get detected and repaired from redo; Postgres runs WAL redo on startup for the same reason. Crash-consistent is fine for single-engine state — the database was built to survive crashes. The caveat is the atomicity: the snapshot must be one instant — and the instant has to cover all the storage involved. An app with heap on one EBS volume and WAL on another needs both snapshotted under one fsfreeze; multi-volume EBS snapshots are crash-consistent per volume but not coordinated across volumes, so the pair can disagree by whatever was mid-flight between them. A "snapshot" that is actually a fast file copy is torn, not crash-consistent — torn is worse, and the engine's crash machinery is not built to reconcile it.
Application-consistent. The backup is taken through the engine's own guarantees:
pg_basebackup -X streamcopies the cluster while streaming exactly the WAL needed to make it self-consistent — the bundle restores alone.pg_dumptakes one repeatable-read snapshot: the dump is the database at a single instant regardless of concurrent writes.FLUSH TABLES WITH READ LOCK(MySQL) quiesces writes so an LVM/filesystem snapshot is engine-consistent, not just crash-consistent. The cost is the name: it is a global read lock — every write stalls untilUNLOCK TABLES, so the snapshot must complete while production waits. mariabackup/XtraBackup exist precisely to avoid that window, taking a brief lock only at the end for binlog coordinates.fsfreeze --freeze /databefore an EBS snapshot,--unfreezeafter: the filesystem flushes and pauses writes so the block image is quiescent.sqlite3 db.sqlite ".backup out.sqlite"uses SQLite's online backup API — consistent without stopping the writer.VACUUM INTO(3.27+) does the same via SQL.
The failure you actually see
Torn backups do not announce themselves. They restore — the files exist — and then Postgres refuses to start on a WAL hole, or InnoDB logs a checksum mismatch, or the dump imports with subtly wrong rows that surface as reconciliation errors weeks later. mysqldump --single-transaction is a trap of a different shape: it is consistent for InnoDB tables via an MVCC snapshot, and silently inconsistent for any MyISAM table still in the schema, because MyISAM has no transactions. Check information_schema.TABLES for stray non-transactional engines before trusting the flag. The other quiet trap is pg_dump's consistency being per-database: two databases dumped back-to-back are two different instants — foreign-data or app-level references crossing databases can disagree.
The practice
Never copy a live engine's files with a file tool. Either:
- use the engine's own backup path (
pg_basebackup/pg_dump,mariabackup,.backup), or - quiesce then snapshot (
FLUSH TABLES WITH READ LOCK+ LVM,fsfreeze+ EBS) — crash-consistent at minimum, engine-consistent when you quiesce first.
Application-consistent across services — a transaction spanning Postgres and Redis and an upload — is not something the OS hands you. Design for it: idempotent reapply, or a logical dump per engine taken at a coordinated point. The word "consistent" in a backup runbook must name which of the three levels it means, because the difference only shows up at restore time.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works
Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.
Zero Trust Everything: The Complete Guide to Self-Hosting Teleport for Secure Infrastructure Access
SudoSecurity runs Teleport internally for everything — admin panel access, secure database connections, Kubernetes, and zero-trust philosophy across our entire team. This is the complete technical guide to installing, configuring, and running your own self-hosted Teleport cluster from scratch.
Running a Minecraft Network: Four JVMs, One Box, and the Settings That Lie to You
Four servers on one host means four JVMs each sizing its thread pools for a machine it does not own. The measured cost of that, the systemd ordering that guarantees nothing, and the throttle that switches itself back on when your forwarding breaks.
Building a Minecraft Network: Four Servers Pretending to Be One Place
A proxy, a lobby, an SMP and a PvP arena is four JVMs that know nothing about each other. What actually has to be shared, why your permissions don't follow players, and the two LuckPerms defaults that silently share nothing at all.