Skip to the page

Reliability and scale · senior

Database failover, backups and multiple regions

Failover needs a fenced old primary and a stable connection path. Backups protect against bad writes that replicas copy. RPO and RTO set the design, and regions add a write conflict problem.

The notes

What you must know

  • Primary dies at 2 am: confirm it is really down, the scope and replica lag. Promote the most up-to-date replica (managed services such as RDS, Aurora or Cloud SQL do this), checking lag first.
  • Split brain: fence the old primary (stopped, access revoked) so two primaries never take writes. It rejoins later as a rebuilt replica.
  • Apps connect through a DNS name or proxy that moves with the primary. Stale DNS, pools with old connections and hard-coded addresses break this. Use pool resets, low TTLs and a proxy (RDS Proxy, PgBouncer).
  • Lost seconds: asynchronous replication can lose some data. Reconcile with the payment gateway and events. A synchronous replica avoids it at a latency cost. Automate failover, run drills and alert on replica lag.
  • RPO is the most data you can lose, in time. RTO is the longest you can be down. Set per system (wallet ledger: RPO near zero, RTO minutes; analytics: hours). RPO near zero needs synchronous replication; RTO of minutes needs a hot standby and automation.
  • Replicas copy mistakes and deletes at once, so backups are still needed. Point-in-time recovery uses base backups plus continuous log archiving (WAL or binlog). Test restores regularly and time them. Keep backups in a separate account with immutable retention (object lock), encrypted, and in India for payment data.
  • Active-passive: one region serves, data loss equals replication lag, downtime is the time to promote. Active-active: both serve, hard part is writes to the same records. Use a home region per user or a globally consistent database (Spanner, CockroachDB, YugabyteDB, Aurora DSQL) with higher write latency.
  • Money has one source of truth per account. In India RBI requires payment system data to be stored only in India, so both regions must be in India. Failover is DNS failover with low TTL or a global load balancer. Drill it regularly.

The red pen

Common traps: mistake → fix

  • Mistake: We have replicas, so no backups

    Fix: replicas copy deletes instantly.

  • Mistake: Promote any replica

    Fix: check lag and fence the old primary.

  • Mistake: Active-active with async replication for balances needs no conflict plan

    Fix: double spend risk.

  • Mistake: Backups never restored are fine

    Fix: an untested backup is a hope.

Easy to hard

Questions interviewers ask, easy to hard

  1. The primary database dies at 2 am. What do you do?
  2. What are RPO and RTO? Set them for a wallet.
  3. Active-active or active-passive across regions?
  4. How do you restore to a point in time?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview