Replication & Quorums
Why databases replicate, the anomalies caused by replication lag, and how Dynamo-style W + R > N quorum math trades latency and availability for freshness.
A single database server is a SPOF and has a hard ceiling on read throughput. Replication keeps copies of the same data on multiple machines so the system survives hardware failure and serves more reads.
The moment data lives on more than one machine, however, network delay creates a fundamental split: does a write wait for replicas before returning, or acknowledge immediately and replicate in the background?
Leader-follower replication: sync vs async
In PostgreSQL, MySQL, and MongoDB, one leader accepts writes and streams its WAL to followers.
- Synchronous: the leader waits until at least one follower persists the write before acknowledging the client. Zero data loss on leader failover, but every write pays a network round-trip and stalls if the sync follower slows down.
- Asynchronous: the leader commits locally and replies immediately. Fast writes and high availability, but followers lag behind by milliseconds (or seconds under heavy load).
Two classic replication-lag anomalies
- Broken Read-Your-Writes: a user updates their profile bio (hits the
leader), reloads the page (routed to a lagging follower), and sees their old
bio.
- Fix: route a user’s own reads to the leader for a short window after a write (e.g. 5–10 seconds), or track the write’s log sequence number (LSN) and require any follower serving that user to be caught up to at least that LSN.
- Non-monotonic reads (moving backward in time): a user refreshes
a feed twice — the first read hits a fresh follower showing comment #5, and
the second read hits a lagging follower where comment #5 vanishes.
- Fix: pin each user session to the same replica (sticky routing) unless that replica fails.
Leaderless replication and quorum math (W + R > N)
Dynamo-style databases (Cassandra, DynamoDB, Riak) remove the single leader altogether. Any coordinator node can send a write to all N replicas and wait for W acknowledgments, and send a read to N replicas waiting for R responses (returning the value with the highest version or timestamp).
W + R > N
strict quorum · guaranteed overlap
N - W
max offline nodes for writes
N - R
max offline nodes for reads
By the pigeonhole principle, if W + R > N, the set of replicas that accepted
the latest write and the set of replicas queried by the read must share at
least one node. Even if the other R - 1 replicas are stale, the client
picks the highest version number and gets fresh data.
Interactive lab: prove W + R > N
Start below with N=3, W=1, R=1 where a read misses the updated replica and
returns a STALE READ. Raise W and R to 2 (2 + 2 > 3), write a new
version, and kill a replica to see both consistency and fault tolerance in
action.
interactive · tune N, W, and R, write a new version, and test whether reads stay fresh
W(1) + R(1) <= N(3) ·STALE READ (v1)STALE READ: returned v1 (latest is v2) — zero overlap
Healing stale replicas: read repair and anti-entropy
Quorums let a write succeed even when one replica is temporarily down or slow,
which leaves that replica holding an older version (v1 while others hold
v2). Leaderless stores heal drift in two ways:
- Read repair: when a quorum read receives
v2from R1 andv1from R3, the coordinator immediately writesv2back to R3 in the background. - Anti-entropy (Merkle trees): background processes exchange hash trees of key ranges between replicas to detect and sync any silent divergence — even for cold keys nobody reads.
note
Even with W + R > N, subtle edge cases can still surface stale reads in
real clusters: concurrent writes racing with wall-clock timestamps, or
sloppy quorums that temporarily write to stand-in nodes during a network
partition. Treat quorum math as a strong practical guarantee, not full
linearizability without consensus.
Tuning (N, W, R) for your workload
| Config (N=3) | Overlap? | Write latency | Read latency | Survives 1 dead node? |
|---|---|---|---|---|
| W=2, R=2 | Yes (4 > 3) |
Balanced | Balanced | Yes (both reads & writes) |
| W=3, R=1 | Yes (4 > 3) |
Slow (all 3) | Fast (1 node) | Writes fail if 1 node dies |
| W=1, R=3 | Yes (4 > 3) |
Fast (1 node) | Slow (all 3) | Reads fail if 1 node dies |
| W=1, R=1 | No (2 <= 3) |
Fastest | Fastest | Survives 2 dead nodes; stale reads likely |
Where this shows up in system design
- DynamoDB & CassandraExpose per-request tunable consistency (ONE, LOCAL_QUORUM, ALL) so teams pick W and R per query path.
- PostgreSQL Streaming ReplicationAsync read replicas scale reporting and read traffic while semi-sync standbys protect against leader data loss.
- Multi-Region Read ReplicasPlace followers in EU and APAC for low-latency local reads while routing writes back to the primary region.
- Shopping Cart Conflict ResolutionAmazon's original Dynamo paper used low W/R for always-writable carts and merged concurrent versions on read.
Check yourself: system design scenarios
interactive · choose the right replication strategy for each requirement
1. User edits a comment and immediately refreshes
async followers · read-your-writes · keep read scaling
Your social app uses PostgreSQL with 1 leader and 4 async read replicas. Users complain that right after editing a post, refreshing the page shows the old text for 2 seconds.
Select an option to check the trade-off.
2. High-availability quorum cluster with 3 replicas
N=3 · no stale reads · tolerate 1 dead node
You run a 3-replica Cassandra keyspace (`N=3`). Reads must always see the latest committed write, and both reads and writes must keep succeeding if 1 replica goes down for maintenance.
Select an option to check the trade-off.
3. Cold keys silently drifting out of sync
leaderless · cold unread keys · 4h outage
A replica was offline for 4 hours and missed writes to older customer records that haven't been read since. How does a leaderless store heal those cold keys?
Select an option to check the trade-off.
SPOFreliability
Single Point of Failure: any component whose failure stops the whole system because there is no independent fallback.
WALstorage
Write-Ahead Log: an append-only disk log written before mutating in-memory structures so crashes never lose committed writes.
monotonicordering
Always moving in one direction: every newly generated value is strictly greater than (or never less than) the previous one.