Video in progress

Strong Junior 🏗️ System design

The database has a replica. Why do you still need a separate backup?

A replica copies a mistake just as fast: if someone sends a DELETE without a WHERE, the rows vanish on the replica a second later too. A backup keeps the state at a given point in time, so you can roll back to a moment before the mistake.

📖 Answer

Replication protects against hardware or server death. A backup protects against human error, a broken migration, ransomware and bugs. Those are different risks.

A real example: a migration script has UPDATE users SET phone = NULL — the condition was forgotten. The primary ran it, and the replicas did exactly the same within milliseconds. Now the phone numbers are gone everywhere. The replica is no help at all.

Kinds of backup:

  • a full snapshot — for example a nightly pg_dump or a disk snapshot;
  • point-in-time recovery (PITR) — a base backup plus a WAL archive. You can say "restore to the state at 14:31:59 today", that is, one second before the bad UPDATE;
  • a delayed replica — a replica that deliberately runs, say, 1 hour behind. If you notice the mistake quickly, you recover from it quickly.
# PostgreSQL: base backup
pg_basebackup -D /backup/base -Ft -z -P

The most forgotten part is testing the restore. A backup nobody has tested is just hope. Every so often, restore it onto a separate server and connect the application. That also answers the question "how long does a restore take?".

Don't keep the backup in the same account or the same cloud region: if the account gets compromised, both go together.