Why Replicate
not the same problem as shardingReplication keeps multiple copies of the same data. It buys four things: durability (a dead node doesn't lose data), read scaling (spread reads across copies), geographic locality (a copy near each region), and failover (promote a copy when the primary dies).
This is a different problem from sharding. Sharding splits data so each node holds a part; replication copies data so each node holds the same part. They're orthogonal, and most real systems do both â shard for capacity, replicate each shard for durability and reads (Sharding).
Leader-Follower
one writer, many readersThe most common model: one leader takes all writes, then streams changes to followers that serve reads. Writes have a single ordering point (no write conflicts), and reads scale by adding followers. The tension is in when a write is considered done.
writes reads
â ââââââ´âââââŦââââââââââ
âŧ âŧ âŧ âŧ
âââââââââââ replicate ââââââââ ââââââââ ââââââââ
â LEADER â âââââââââââļ â foll â â foll â â foll â
âââââââââââ ââââââââ ââââââââ ââââââââ
SYNC replication: leader waits for follower ack before confirming write
â no data loss on failover, but slower writes
ASYNC replication: leader confirms immediately, replicates in background
â fast writes, but a lagging follower can lose recent data
Replication lag is the gap between a write hitting the leader and reaching a follower. Read from a lagging follower right after writing and your own change is missing â a read-your-writes violation. This is where session guarantees come in: pin a user to the leader for their own reads, or track the version they've seen (Consistency Models).
Failover
promoting a follower when the leader diesWhen the leader dies, the system detects it, picks a follower, and promotes it. Sounds simple; three things make it hard.
| Hazard | What goes wrong |
|---|---|
| Detection | Can't tell "dead" from "slow" â promote too eagerly and you cause outages, too slowly and you extend one |
| Split brain | The old leader comes back thinking it's still leader â now two leaders take conflicting writes |
| Lost writes | With async replication, writes acked but not yet replicated vanish when a behind follower is promoted |
Multi-Leader
writes in many places, conflicts to resolveWith multiple leaders â typically one per region â each takes writes locally and replicates to the others. Writes are fast and local everywhere, and no single leader is a bottleneck or a distant round trip. The price is write conflicts: two regions can edit the same record at the same time, and both are "correct" locally.
Resolving conflicts is the whole problem. Last-write-wins is simplest and silently drops one of the two writes â sometimes fine, sometimes data loss you'll regret. CRDTs (conflict-free replicated data types) are data structures designed so concurrent updates merge deterministically without loss â the right tool for counters, sets, and collaborative text, at the cost of constrained operations. Multi-leader is the write side of active-active regions (Multi-Region).
Leaderless / Quorum
Dynamo-style: write to many, read from manyLeaderless stores (Dynamo, Cassandra) drop the leader entirely. A client writes to several replicas and reads from several, and overlap guarantees freshness. With N replicas, requiring W to ack a write and R to answer a read, the rule W + R > N forces every read set to overlap every write set by at least one up-to-date replica.
N = 3 replicas. W = 2, R = 2. W + R = 4 > 3 â
write ââļ [R1] [R2] [R3] ack from 2 (W=2) â write succeeds
â â â
read ââ [R1] [R2] [R3] ask 2 (R=2); at least one overlaps the write
â â â the reader sees the latest value
Tuning: W=N (all) â strong writes, slow, fragile to one node down
R=1 â fast reads, weaker guarantee
W+R ⤠N â fast but may read stale data
Two repair mechanisms keep replicas honest. Read repair: when a read notices a stale replica, it writes the fresh value back. Anti-entropy: a background process compares replicas and reconciles differences. And a sloppy quorum with hinted handoff keeps writes flowing during a partition â a temporary node accepts the write and hands it off when the real owner returns, trading strictness for availability.
Write-Ahead Logs
the durability primitive underneath it allUnderneath every model above sits one idea: log first, apply second. Before a database changes a page, it appends the change to a write-ahead log on durable storage. If the process crashes mid-operation, it replays the log on restart and recovers. The log is the source of truth; the data files are a materialized view of it.
Quick Reference
models and quorum on one card| Model | Write path | Conflict risk | Best fit |
|---|---|---|---|
| Leader-follower | One leader | None (single writer) | Read scaling, most systems |
| Multi-leader | Leader per region | High â needs resolution | Multi-region local writes |
| Leaderless / quorum | Write to W of N | Handled by versioning | High availability, tunable |
| Quorum (N=3) | Gives |
|---|---|
| W=2, R=2 | Strong-ish reads, tolerates one node down â the common default |
| W=3, R=1 | Fast reads, slow/fragile writes |
| W=1, R=1 | Fast both ways, may read stale (W+R ⤠N) |