Replication Strategies

Difficulty: Intermediate

Question

Compare synchronous and asynchronous database replication. What are the trade-offs, and what sits in between?

Answer

In synchronous replication, the primary waits until at least one replica confirms it has durably received a write before telling the client the transaction committed, so a committed write survives losing the primary. In asynchronous replication, the primary commits locally, replies immediately and ships changes to replicas afterwards, which is faster but means recently committed writes can be lost if the primary dies before replicas catch up. The choice is a trade-off between durability across machines and write latency plus availability.

How it works under the hood

The primary records every change in its log (Postgres WAL, MySQL binlog). Replicas stream that log and replay it. The only difference is when the primary acknowledges the client:

  • Async: after its own log flush. Replica lag can be milliseconds or, under load, seconds to minutes.
  • Sync: after the replica confirms. Postgres lets you choose how strict with synchronous_commit (remote_write, on, remote_apply).
  • Semi-synchronous (MySQL): waits until at least one replica has received the transaction into its relay log, not applied it. A middle ground widely used in production.

Trade-offs side by side

AspectSynchronousAsynchronous
Data loss on primary failureNone for acknowledged commits (RPO 0)Recent commits can be lost
Write latencyAdds a network round trip per commitLocal commit speed
Replica failureCan block writes unless another sync replica existsNo effect on primary
Cross-region usePainful: every commit pays inter-region latencyCommon
Read-your-writes on replicasGuaranteed with remote_applyNot guaranteed (lag)

Practical patterns

  • Quorum sync: Postgres synchronous_standby_names = 'ANY 1 (r1, r2)' waits for any one of two replicas, so one slow or dead replica does not stall writes.
  • Sync within a region, async across regions: a Mumbai primary with a synchronous replica in another availability zone and an async replica in Hyderabad for disaster recovery.
  • Consensus-based replication: systems like CockroachDB, Spanner, etcd and MySQL Group Replication commit once a majority (Raft or Paxos) has the write, combining no-loss with tolerance of a minority failing.

Gotchas candidates miss

  • Replication lag bugs: a user updates their profile, the next read hits an async replica and shows the old data. Fixes: read from primary for a few seconds after a write, or route by session.
  • Failover with async loses data and can cause split brain if the old primary comes back accepting writes. Use fencing and tools like Patroni or Orchestrator.
  • Replication is not backup: a DROP TABLE replicates instantly. Keep point-in-time backups too.
  • Terminology: modern docs use primary/replica (or leader/follower) instead of master/slave.
When the client hears "committed". Synchronous waits for the replica; asynchronous replies first and ships later.
  1. Client → Primary: COMMIT
  2. Primary → Replica: Flush WAL locally, then stream it
  3. Replica → Primary: Ack flushed (sync waits here)
  4. Primary → Client: Commit OK

Code examples

Configuring synchronous replication in PostgreSQL

# postgresql.conf on the primary
synchronous_standby_names = 'ANY 1 (replica_a, replica_b)'
synchronous_commit = on        # wait for standby WAL flush

# Per-transaction override for low-value writes
psql -c "BEGIN; SET LOCAL synchronous_commit = local; \
INSERT INTO page_views(path) VALUES ('/home'); COMMIT;"

# Check replication state and lag
psql -c "SELECT application_name, sync_state, replay_lag
         FROM pg_stat_replication;"

ANY 1 means a commit needs just one of the two replicas to confirm, so one replica failing does not block writes. SET LOCAL lets cheap writes skip the wait.

Key points

  • Sync: zero data loss for acknowledged commits, higher latency, can block on replica failure.
  • Async: fast commits, replicas lag, recent writes may be lost on failover.
  • Semi-sync and quorum sync are common middle grounds.
  • Read replicas can return stale data; design for read-your-writes.
  • Replication does not replace backups.

Common follow-up questions

  • How do you handle read-after-write consistency with async replicas?
  • What is split brain and how do you prevent it during failover?
  • How does Raft-based replication differ from primary-replica sync replication?
  • What are RPO and RTO?

Interview tips

  • Frame the answer around "when does the primary acknowledge the client"; everything else follows.
  • Mention semi-sync or quorum sync to show you know the real-world middle ground.
  • Bring up replication lag and its user-visible symptoms.

Concepts covered

Primary-Replica, Synchronous Replication, Asynchronous Replication, Semi-Synchronous, Replication Lag, Failover

More Database Design questions