CONCEPT SHEET · Concepts

Database Replication

One-liner: Copy data across nodes for read scaling, availability, and locality; the choices are who accepts writes and whether acks wait for replicas.

ArticleConcepts© Arthur Bernier Jr578 words1 code block

Say this#

How it works#

TEXT
writes -> [Leader] --WAL stream--> [Follower 1] (sync)   -> commit ack                   --WAL stream--> [Follower 2] (async)  -> reads                   --WAL stream--> [Follower 3] (async)  -> reads / DR in another region
  1. Client writes to the leader; leader appends to WAL.
  2. Followers stream and apply the WAL (physical) or decoded changes (logical).
  3. Sync follower acks before leader commits; async followers trail by the lag.
  4. Reads routed to replicas; sensitive reads (post-write) pinned to the leader or wait for LSN.
  5. On leader failure: health check → consensus (etcd/Zookeeper-backed Patroni) elects a new leader → fence the old one → update DNS/VIP/proxy (PgBouncer, HAProxy).

Multi-leader: each region has a leader; cross-region async replication; conflicts detected by version vectors, resolved LWW or by app. Good for offline-first and geo-distribution; hard to reason about.

Trade-offs#

ChoiceProCon
Leader-followerSimple, no write conflictsWrite scalability limited to one node; failover complexity
Multi-leaderLocal writes in every region, survives region lossConflicts, hard invariants (uniqueness)
LeaderlessNo failover needed, always writableWeaker consistency, read repair, app-level conflict handling
SynchronousRPO = 0Latency = slowest replica; leader stalls if replica dies
AsynchronousFast writes, replicas can be far awayRPO > 0; stale reads; lost writes on failover
Many read replicasRead scaleEach adds lag risk and leader bandwidth

Numbers#

  • In-region async lag: typically milliseconds; cross-region: tens to hundreds of ms, seconds under load.
  • Sync replication adds roughly one RTT per commit: ~1 ms same AZ, ~1-2 ms cross-AZ, 50-150 ms cross-region.
  • Automated failover (Patroni/RDS Multi-AZ): ~30-120 s typical.
  • Postgres can comfortably run ~5-15 streaming replicas; beyond that use cascading replication.

In the interview#

See also