13 min read
System Design Index
Start Here
Tier 1 -- Building Blocks
Scale Reads
Scale Writes
Database Selection
Traffic Control
Consistency & Coordination
Estimation
Tier 2 -- Core Systems
Tier 3 -- Location & Real-Time
Tier 4 -- Infrastructure & Data
Tier 5 -- Finance & Commerce
Tier 6 -- Advanced & Collaborative
System Design Index
Start Here
Tier 1 -- Building Blocks
Scale Reads
Scale Writes
Database Selection
Traffic Control
Consistency & Coordination
Estimation
Tier 2 -- Core Systems
Tier 3 -- Location & Real-Time
Tier 4 -- Infrastructure & Data
Tier 5 -- Finance & Commerce
Tier 6 -- Advanced & Collaborative
Replication Strategies (Leader-Follower, Multi-Leader, Leaderless)
1. What Is It?
is the practice of keeping copies of the same data on multiple machines. It serves three purposes: (if one node fails, others serve traffic), read scalability (distribute reads across replicas), and reduced (place replicas geographically close to users).
The fundamental challenge of is handling writes: when the same data exists on multiple machines, how do you ensure they stay consistent? The three main replication strategies answer this differently:
- Single-leader (leader-follower): One node accepts all writes. Simpler, but the leader is a write bottleneck.
- Multi-leader: Multiple nodes accept writes. More available, but writes can conflict.
- Leaderless: Any node accepts reads and writes. Maximum and write , but requires coordination and conflict resolution.
Your team is building a global e-commerce platform where users are distributed across North America, Europe, and Asia. The primary complaint is that users in Asia experience high write latency because all writes must travel to a single data center in North America. Which replication strategy would most directly address this problem, and what new challenge does it introduce?
2. How It Works
Strategy 1: Single-Leader (Leader-Follower) Replication
All writes go to the leader (also called primary, master). The leader replicates changes to followers (replicas, standbys) via a log. Reads can go to any replica.
A (Write-Ahead Log) is a sequential file that records every database write before it's applied to the main data files. In , the leader streams its to followers so they can replay the same writes.
Synchronous vs Asynchronous Replication:
| Mode | How It Works | Pros | Cons |
|---|---|---|---|
| Synchronous | Leader waits for follower ACK before confirming write to client | No data loss on leader failure; strong consistency | Write latency increases; if follower is down, leader blocks writes |
| Asynchronous | Leader confirms write immediately; follower applies later | Low write latency; high availability | Replication lag; potential data loss if leader crashes before replication |
| Semi-synchronous | One designated sync follower; rest async | Balance of durability + availability | Single sync follower is a bottleneck; if it dies, must promote an async one |
PostgreSQL streaming replication is the canonical leader-follower implementation. The primary sends a continuous stream of WAL (Write-Ahead Log) records to standbys via the replication protocol. Each standby tracks its position using the Log Sequence Number (LSN). Same-region async lag is typically sub-100ms; cross-region can reach seconds.
Replication lag and consistency problems:
Timeline:
t=0: Write "user.email = alice@new.com" → Leader
t=0: Leader confirms write to client
t=1s: Follower still showing "alice@old.com" (lag = 1s)
Problem 1: Read-your-writes violation
User writes email change → leader (t=0)
User immediately reads profile → routed to follower (t=0.5s)
Follower shows OLD email → "my change wasn't saved!"
Problem 2: Monotonic reads violation
First read → Follower 1 (lag=0s) → shows new post
Second read → Follower 2 (lag=5s) → post doesn't exist yet
"The post I just read has vanished!"
Fix: Route user's reads to the leader (or the specific follower they wrote to)
for at least N seconds after a write.
Failover:
- Followers detect leader failure via missed heartbeats (timeout: typically 3–10s)
- Election: followers vote, require a (a majority of nodes — e.g. 2 of 3 — that must agree before a decision is made); Raft uses randomized 150–300ms election timeouts
- Winning follower promotes to leader; clients re-discover via or ZooKeeper
- Total failover time in production: typically 6–20 seconds
Split-brain risk: A partitioned old leader that's still alive may accept writes simultaneously with a newly elected leader → data divergence. Prevention: require strict (minority partition rejects writes) + STONITH (Shoot The Other Node In The Head — forcibly fence the old leader).
Strategy 2: Multi-Leader (Active-Active) Replication
Multiple nodes accept writes independently. Changes replicate to all other leaders asynchronously.
Write conflicts: When Client A updates record X to "Alice" via Leader 1, and Client B simultaneously updates the same record X to "Bob" via Leader 2, both writes succeed locally. When they replicate to each other, there's a conflict: who wins?
Conflict resolution strategies:
| Strategy | How It Works | Pros | Cons |
|---|---|---|---|
| Last-Write-Wins (LWW) | Each write has a timestamp; highest timestamp wins; others are discarded | Simple, O(1) | Can silently lose concurrent writes; requires synchronized clocks |
| Merge / Union | Combine both values (e.g., set union, append-only) | No data loss for sets | Only works for specific data types |
| CRDT (Conflict-Free Replicated Data Type) | Math-defined data structures that always merge deterministically | No conflicts by definition; eventually consistent | Only works for specific CRDT types (counters, sets, maps) |
| Application-level | Store all conflicting versions, surface to application code | Maximum flexibility | Developer must write conflict resolution logic |
| Custom resolution | User or application picks the winner (e.g., "last edit wins for this field") | Domain-appropriate | Complex to implement |
CRDTs in production: Riak's CRDT implementation powers League of Legends' chat service at 7.5 million concurrent users, using G-Counter (grow-only counter) and OR-Set (observed-remove set) CRDTs.
Multi-leader use cases:
- Multi-datacenter replication (each datacenter has its own leader)
- Offline-first mobile applications (device is its own leader while offline; syncs when reconnected — CouchDB uses this model)
- Collaborative editing (Google Docs uses OT/CRDT to handle simultaneous edits to the same document)
Strategy 3: Leaderless (Dynamo-Style) Replication
No designated leader. Clients write to and read from multiple nodes simultaneously. Any node can accept any request.
Quorum reads and writes (W + R > N):
N = 3 replicas for key K
W = 2 (need 2 ACKs before confirming write)
R = 2 (need 2 responses for a read)
W + R = 4 > N = 3
Overlap guarantee: at least 1 node in every read set has seen the latest write
→ Strong consistency: always read the most recent write
If W=1, R=1: 1+1=2 ≤ 3 → eventual consistency (fast, but may return stale data)
If W=3, R=1: 3+1=4 > 3 → strong consistency (every replica acknowledged the write)
Read repair: When a client reads from multiple nodes and detects version discrepancies (one node has old data), it writes the newer version back to the stale nodes. Happens synchronously at read time.
Anti-entropy: Background process that continuously scans replicas for divergences (using Merkle trees to efficiently compare large datasets) and synchronizes missing data. Unlike log-based replication, anti-entropy doesn't preserve write order — it just ensures eventual convergence.
Hinted handoff: If a node is temporarily unreachable when a write occurs, another node temporarily stores the write with a "hint" — when the target node recovers, the hint is replayed. Improves at the cost of brief inconsistency.
Version tracking with vector clocks:
Initial: key X = "" at all nodes
VC = [N1=0, N2=0, N3=0]
Write "alice" from Client A via N1:
N1: X="alice", VC=[N1=1, N2=0, N3=0]
Concurrent write "bob" from Client B via N2:
N2: X="bob", VC=[N1=0, N2=1, N3=0]
N1 and N2 replicate to each other:
N1 VC=[N1=1, N2=0] vs N2 VC=[N1=0, N2=1]
Neither dominates the other → CONFLICT
Both values preserved as siblings → application resolves
Comparison Table
| Property | Single-Leader | Multi-Leader | Leaderless |
|---|---|---|---|
| Write scalability | Limited (leader bottleneck) | High (any leader) | High (any node) |
| Read scalability | High (many replicas) | High | High |
| Consistency | Strong (sync), eventual (async) | Eventual (conflict possible) | Tunable (W+R>N) |
| Conflict handling | None (global write order) | Required | Required |
| Failover | Required (elect new leader) | Automatic (other leaders continue) | N/A (no single leader) |
| Write conflicts | Impossible | Possible | Possible |
| Complexity | Low | Medium | High |
| Examples | PostgreSQL, MySQL, MongoDB | CouchDB, multi-datacenter active-active | Cassandra, DynamoDB, Riak |
A distributed database uses single-leader replication with asynchronous followers. A user updates their profile email and immediately reloads the page. The read request is routed to a follower with 2 seconds of replication lag, so the user sees their old email address. Which consistency problem does this describe, and what is the standard fix?
3. When to Use It (and When NOT To)
Use Single-Leader When:
- Simplicity and are the priority: Single-leader provides the strongest consistency guarantee (synchronous mode), and conflict-free write ordering simplifies application logic.
- Read-heavy workload: Many follower replicas serve reads; one leader handles all writes. 90% of web apps fit this pattern.
- ACID transactions are required: Multi-leader and leaderless make cross-node transactions much harder; single-leader keeps transaction scope local to the leader.
- Operational simplicity matters: Managed databases (RDS, Cloud SQL) all default to single-leader; well-understood failure modes.
Use Multi-Leader When:
- Multi-datacenter active-active: Low write in each region (writes don't cross the WAN). Essential when a single-leader in Region A would add 100+ ms to every write from Region B.
- Offline-first clients: Mobile apps that need to function without connectivity. Each device is its own "leader"; syncs when online (CouchDB's model).
- Collaborative real-time editing: Multiple users editing the same document simultaneously. CRDTs or OT (Operational Transformation) handle merging.
Use Leaderless When:
- Maximum is required: No ; can tolerate multiple node failures while still accepting writes (as long as W nodes are reachable).
- High write at global scale: Cassandra, DynamoDB — write to the nearest node, replicate asynchronously.
- Tunable consistency: Different operations in the same system can use different quorums (e.g., financial reads use W=3,R=3; analytics reads use W=1,R=1).
Anti-Patterns:
- Using async single-leader and routing all reads to followers: You will violate read-your-writes consistency. Users will see their changes "disappear." Always route post-write reads back to the leader (for a configurable window).
- Multi-leader without conflict resolution: Silent LWW data loss is worse than no multi-leader. Design conflict resolution before enabling multi-leader.
- Leaderless with W=1: Tuning for maximum write speed at the cost of consistency. Fine for analytics; dangerous for user-visible data.
- Assuming replicas are fully in sync: lag is real. Never make business decisions (e.g., "deduct inventory") based on a replica read without ensuring it's reading from the .
A global e-commerce platform has warehouses in North America and Europe. Each region needs to process customer orders independently, even when the transatlantic link is slow or unstable, and write latency must stay low for users in both regions. Which replication strategy is the best fit, and what critical design concern must be addressed before deploying it?
4. Real-World Usage
PostgreSQL — Streaming Replication (Single-Leader)
PostgreSQL uses physical streaming : the primary sends records to standbys via a connection. Standbys replay to stay current. The synchronous_standby_names config option controls whether any standbys are synchronous (wait for ACK before confirming write) — enabling semi-synchronous mode for critical data. Organizations like GitLab run PostgreSQL with one synchronous standby for + multiple async standbys for read scaling.
Cassandra — Leaderless (Dynamo-Style)
Cassandra's leaderless design comes directly from Amazon's Dynamo paper. Each key is replicated across N nodes determined by . Clients use a "coordinator" node (any node can be coordinator) that fans out writes to the N replica nodes. Tunable consistency lets teams choose their CP/AP tradeoff per query: for financial operations, LOCAL_ONE for analytics. Cassandra's gossip protocol handles anti-entropy between nodes, and hinted handoff handles temporary node unavailability.
CouchDB — Multi-Leader with MVCC
CouchDB implements multi-leader replication for its "offline-first" use case. Each CouchDB instance is an independent leader. Replication is bidirectional and explicit: you trigger replication between instances. Conflicts are detected and stored as document revisions — the application code chooses the winner or merges. This model enables mobile apps that store data locally in CouchDB and sync to a central server when connectivity is available.
A fintech team is using Cassandra to store transaction records. For balance-critical writes and reads, they need strong consistency guarantees. For generating usage analytics dashboards, they are comfortable trading consistency for lower latency. Which consistency level configuration best fits this requirement?
5. Interview Cheat Sheet
5 Sentences to Show Deep Understanding
-
"Single-leader eliminates write conflicts by funneling all writes through one node — the trade-off is a write ceiling and a , mitigated by failover automation (Raft/Paxos ) and async for low- writes at the risk of replication lag."
-
"Replication lag creates three consistency anomalies in async single-leader replication: read-your-writes violations (user reads a replica that hasn't received their write yet), monotonic reads violations (user reads from two replicas at different lag depths), and causality violations — all solvable by routing reads to the leader for a short window after writes."
-
"Multi-leader replication makes write conflicts unavoidable — when two leaders independently accept conflicting writes to the same key, there's no global write ordering to determine which is 'correct.' The safest conflict resolution is CRDTs (mathematically conflict-free data structures); the most common is Last-Write-Wins (LWW), which silently discards concurrent writes."
-
"Leaderless W+R>N guarantees that any read set overlaps with at least one node from the write — with N=3, W=2, R=2: any 2-node read set and any 2-node write set must share one node, ensuring the latest write is always visible. Setting W+R ≤ N gives with faster reads and writes."
-
"Vector clocks detect concurrent writes in leaderless systems: if neither [N1=1, N2=0] dominates [N1=0, N2=1], the writes were concurrent — both values are preserved as 'siblings' and the application must reconcile them. This explicit conflict surfacing is more correct than silently picking a winner with LWW."
Common Follow-Up Questions
Q: How does PostgreSQL replication failover work and how long does it take? A: (1) Followers stop receiving heartbeats from the primary; after a configurable timeout (typically 3–10s), they declare the primary dead. (2) Patroni/repmgr/pg_auto_failover (HA tools) elect a new primary — the candidate with the most recent position wins. (3) Other standbys reconfigure to follow the new primary. (4) or DNS update points clients to the new primary. Total time: typically 6–20 seconds including detection + election + client reconnection.
Q: What is the "tombstone" problem in leaderless systems?
A: When you delete a record in a leaderless system, you can't just remove the data — if a replica missed the delete (due to lag), the next anti-entropy sync would restore the deleted record. Instead, a "tombstone" marker is written that signals deletion. Anti-entropy then propagates the tombstone. The problem: tombstones accumulate indefinitely unless compacted. Cassandra's process removes tombstones after a configurable gc_grace_seconds (default: 10 days), after which the old deleted values are guaranteed to have been replicated.
Q: When would you choose multi-leader over single-leader? A: Multi-leader shines when write across WAN is unacceptable. Example: a globally distributed app with users in US, EU, and APAC. With single-leader in US, EU and APAC writes cross the Atlantic/Pacific (100–200ms one-way). With multi-leader, each region has a local leader, writes complete in <10ms locally, and regions sync asynchronously. The cost is conflict resolution complexity. Business logic that requires global sequential ordering (e.g., sequential ticket numbers, bank transfers) should stay single-leader despite the latency.
Q: How does Cassandra handle a node failure during a write? A: Hinted handoff: if one of the W target replicas is unreachable, the coordinator temporarily stores the write with a "hint" — a record saying "deliver this write to Node X when it comes back." When Node X recovers, the coordinator replays the hint. This maintains (the write succeeds as long as W nodes respond, even if the originally targeted node is down) at the cost of brief inconsistency on Node X until the hint is replayed.
Connections to Other Building Blocks
- CAP Theorem & PACELC: Replication strategy directly determines CAP/PACELC position. Single-leader sync = CP (PC/EC); leaderless with W=1,R=1 = AP (PA/EL); leaderless with W+R>N = CP.
- (Raft/Paxos): in single-leader systems uses Raft/Paxos. These protocols are what makes failover safe (prevents split-brain by requiring majority quorum).
- & : Replication and are orthogonal: sharding splits data across nodes (partition by key), replication copies each partition to multiple nodes. Every shard has its own replication group.
- Write-Ahead Log (): PostgreSQL streaming replication works by shipping WAL records from primary to standbys. The WAL is both the mechanism and the replication mechanism.
- Distributed Locking: Multi-leader systems need distributed locks or CRDTs to handle concurrent writes safely. Leaderless systems need quorum coordination.
Glossary History
Click dotted jargon to save explanations here.
Glossary History
Click dotted jargon to save explanations here.