Recover from bad data, not just dead disks
Replication faithfully copies a bad migration or a DELETE without WHERE to every replica.
Say it like this
Replication protects against hardware failure but copies a bad DELETE faithfully, so I keep nightly base backups plus a continuous archive of the write-ahead log, which lets me restore the database to any second before the mistake, and I rehearse that restore every month.
Basic solution
Take regular backups and keep the write-ahead log for point-in-time recovery; test restores.
Base backup plus log archive, restored to 14:01:59
The replica received the bad UPDATE within milliseconds. The restore takes the 02:00 base backup, replays the archived log up to the second before the mistake, and the fixed rows are copied back to production.
How it works
- On a schedule, copy the whole database (a base backup) to durable storage in another region. Nightly is common; large change rates justify more often.
- As the database writes its write-ahead log, archive each closed log segment to the same storage, typically every 16 MB or every minute. The archive now holds every change made since the base backup.
- To restore to a time T: fetch the latest base backup taken before T, then replay the archived log segments on top of it in order, stopping at T. The result is the database exactly as it was at that second.
- Start the restored copy as a separate instance alongside production. Verify it, then either promote it to replace production or copy only the affected rows back into production.
- Keep backups for a retention window (30 days is common). Thin out older base backups, keeping weekly ones, but keep the log archive for the whole window so every second in it is reachable.
- Test the restore automatically: every night restore the latest backup to a scratch instance, run checks such as row counts and checksums, and record how long it took.
Why replicas are not backups
- A replica applies every write within milliseconds, including DROP TABLE. A backup is a copy that stops receiving writes, so the mistake has not reached it yet.
Worked example
Notification system, Postgres with notification and delivery tables
- 250M notifications/day, about 75 GB of new rows per day, 2.3 TB database with 30-day retention.
- A migration at 14:02 runs UPDATE user_preference SET channels = default without a WHERE clause. 50M users lose their preferences. The sync replica has the same bad rows 5 ms later.
- Restore plan: last base backup at 02:00 (2.3 TB, 20 minutes to copy from object storage at 2 GB/s), then replay 12 hours of WAL (about 40 GB at this write rate, 15 minutes), stopping at 14:01:59.
- Total time to a restored copy: about 40 minutes. Then: SELECT the user_preference table from the restored copy (50M rows, 5 GB) and UPDATE production from it, 10 more minutes.
- Loss: preference changes made between 14:02 and the fix, a few thousand rows, which can be replayed from the application’s audit log. Everything else in the database stays live throughout.
- Cost: 2.3 TB × 30 nightly backups thinned to 10 copies (23 TB) plus 30 days of WAL (2.4 TB) in object storage, about 500 dollars a month. The migration mistake would have cost more than that in one hour of support tickets.
Variants and when each wins
Snapshots only
- The storage volume is snapshotted nightly or hourly. It wins for simplicity, but you can restore only to a snapshot time, losing up to one interval of writes.
Base backup plus log archive (PITR)
- A periodic full copy plus a continuous archive of the write-ahead log. It wins as the standard for transactional databases, because you can restore to any second in the window and the log archive is small next to the data.
Logical backups
- Table exports in the style of pg_dump. It wins for restoring one table or moving across database versions, though restoring a whole large database this way is slow.
Delayed replica
- A replica that applies the log with a deliberate lag, say one hour. It wins for fast recovery from recent human error, because the mistake can be stopped before it reaches the replica; it is useless for errors found later.
Recommendation: PITR as the foundation, a delayed replica if human error is the main risk and fast recovery matters, logical exports for per-table and cross-version needs.
Failure modes
Backup never restored
- The restore fails on the day it matters: a WAL segment is missing, a credential is wrong, or the base backup is corrupt. It happens because nobody ever tried restoring. Run an automated restore test every night with checksum and row-count checks, and a manual drill every month with the clock running.
Backups in the same failure domain
- The region outage that lost the database also lost the backups. It happens because the backups live in the same region or the same cloud account. Store them cross-region in a separate account, with write-once retention so an attacker holding production credentials cannot delete them.
Log archive gap
- Point-in-time recovery can only reach yesterday at 02:00, because the archive command failed silently for 12 hours. It happens because nothing watched the archive. Alert when the newest archived segment is more than a few minutes old.
Restore too slow for the RTO
- The restore takes 40 minutes against a 10-minute recovery time objective. It happens because a day of log must be replayed on top of the base backup. Take base backups more often so less log is replayed, use faster storage, or keep a delayed replica for the fast path.
Restoring over production
- A restore meant to fix one table wipes out every write made since the backup. It happens because the restore was run on top of the live database. Always restore to a separate instance, then copy the needed rows back.
Retention too short for discovery
- Data corruption is found after 45 days, and backups are kept for 30. It happens because retention was set from storage cost, not from how long problems take to notice. Keep retention longer than the longest plausible discovery time, thinned to weekly copies for the tail.
Interviewer follow-ups
Answer out loud before you open each one.
What is the difference between replication and backup?
Replication gives you a live copy that survives hardware loss; it cannot undo a write because it applies every write. Backup gives you a copy frozen at a point in the past; it can undo any write after that point but takes time to bring online. You need both, for different failures.
How do you choose backup frequency and log retention?
Backup frequency sets restore time, because a restore replays the log since the last backup; nightly is fine if replaying a day of log fits the recovery time objective. Log retention sets how far back you can go; it should exceed the time it could take to discover a problem, commonly 30 days.
How do you protect backups from an attacker or a bad script?
Different account or project from production, credentials that production cannot read, object-lock or write-once retention so nothing can delete them before the window ends, and encryption with a key that production does not hold. Then a compromised production database cannot take its own backups with it.
How do you restore a single table without downtime?
Restore to a scratch instance at the point in time just before the mistake, dump the affected table or rows, and apply them to production with a targeted UPDATE or INSERT while production stays up. Only promote a full restore when the whole database is unusable.
Connections
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon Aurora | Postgres primary, Sync replica, Restore instance | AlloyDB for PostgreSQL, Cloud SQL | Azure Database for PostgreSQL or MySQL | PostgreSQL, MySQL, CockroachDB |
| Amazon S3 | Base backup 02:00, WAL archive | Cloud Storage | Blob Storage | Cloudflare R2, MinIO, Ceph |