Replication and consistency¶
What you'll be able to do¶
- Compare single-leader, multi-leader and leaderless replication.
- Explain replication lag and the anomalies it causes, and how to tame them.
- Use quorum arithmetic (W + R > N) to reason about consistency.
Why replicate¶
Keeping copies of data on several machines gives you:
- Availability: if one machine dies, another copy can serve.
- Read capacity: reads can be spread across copies.
- Latency: keep a copy near each group of users.
The price is that copies can disagree. Almost everything in this lesson is about managing that.
Single-leader (primary / replicas)¶
One node, the leader, accepts all writes and ships them to followers. Reads can go to followers.
- Simple and widely used (PostgreSQL, MySQL, many managed databases).
- Synchronous replication: the leader waits for the follower before acknowledging. No data loss on failover, but a slow follower slows writes.
- Asynchronous replication: the leader acknowledges immediately. Fast, but if the leader dies, recent writes may be lost.
- Common compromise: one synchronous follower, the rest asynchronous.
- Failover means promoting a follower. Hard parts: detecting the leader is really dead, avoiding two nodes both thinking they are leader (split brain), and what to do with writes the old leader had not shipped.
Replication lag and what it breaks¶
With asynchronous followers, a follower can be behind. Users notice:
- Read-your-writes violated: you post a comment, refresh, and it's gone (the read hit a stale follower). Fix: route a user's reads to the leader for a short time after they write, or track a version.
- Monotonic reads violated: you see newer data, refresh, and see older data (different followers). Fix: pin a user to one follower.
- Causality violated: you see a reply before the question it answers. Fix: keep related data on the same partition, or track causal order.
This state, where replicas converge eventually if writes stop, is called eventual consistency.
Multi-leader¶
Several nodes accept writes, typically one per region, and replicate to each other.
- Good: local writes in every region and survival of a region outage.
- Bad: two leaders can change the same record at the same time, so you need conflict resolution: last-writer-wins (simple, but can silently lose a write), merging (CRDTs), or application logic.
Leaderless (Dynamo-style)¶
Any replica accepts reads and writes. A client writes to several replicas and reads from several, and the system uses a quorum.
With N replicas, writes acknowledged by W, reads that consult R:
if W + R > N, a read overlaps at least one replica holding the latest write
N=3, W=2, R=2 → 2+2 > 3 OK (tolerates 1 node down)
N=3, W=1, R=1 → 1+1 < 3 NOT OK (fast, but may read stale data)
Tune W and R to trade write latency, read latency and consistency. Quorums are not magic: concurrent writes, failed writes and sloppy quorums can still produce anomalies.
Consistency vs availability¶
When the network splits, a system must choose: refuse some requests to stay consistent, or answer and risk inconsistency. This is the CAP theorem in practice. Even when there is no partition, there is a trade-off between latency and consistency (often called PACELC). Most replicated systems let you choose per operation.
Replication is not a backup¶
Replication copies every change, including a bad one. If someone runs DELETE without a WHERE, or a bug corrupts rows, the mistake is faithfully replicated to every copy within seconds. You still need point-in-time backups. The disaster-recovery lesson comes back to this.
Interview angle
Say which consistency you need per feature. A bank balance and a "likes" counter do not need the same guarantees. Then pick the replication strategy that gives exactly that, no stronger.
For AI engineers¶
For AI engineers
After a training job registers a new model, a model registry backed by a primary database and read replicas may return the old version to a deployment worker reading a lagging replica. Carry the registry write's version into that read, or route it to the primary until a replica catches up. Embedding and vector index replicas have the same freshness problem: a newly ingested document may be absent from one search result. Online and offline copies of a feature store can also drift while updates propagate, producing training/serving skew. Track which data or index version each job consumed instead of assuming every copy is current. Cross-region copies of datasets and checkpoints improve access and recovery from a machine or region failure, but replication also copies accidental deletion. Keep independent, point-in-time backups for experiment tracking databases and datasets.
Common mistakes¶
- Reading immediately from a lagging replica after registering a model and assuming the write disappeared.
- Treating all replicas as if they are consistent at the same instant, without checking their applied versions.
- Promoting a stale follower without counting acknowledged writes it never received.
- Calling extra replicas a backup, even though deletion and corruption replicate too.
- Ignoring split brain during failover, which can let two leaders accept conflicting writes.
Check yourself¶
Users post a comment, reload the page, and sometimes don't see it for a few seconds. What is happening and how do you fix it without making everything synchronous?
Asynchronous replication lag: the reload was served by a follower that hasn't received the write yet (read-your-writes violation). Fix by reading from the leader (or an up-to-date replica) for that user for a short window after they write, or by tracking the write's version and only reading from replicas that have reached it.
N=5, W=3, R=3. How many nodes can be down while reads and writes both still succeed, and do reads see the latest write?
Both need 3 of 5 nodes, so up to 2 nodes can be down. W+R = 6 > 5, so every read quorum overlaps every write quorum and sees the latest acknowledged write (ignoring edge cases like concurrent writes).
Why isn't "we replicate to 3 regions" an answer to "how do you recover from a bad migration that deleted data"?
Replication propagates the deletion to every copy. Recovery needs independent point-in-time backups or a replica deliberately delayed behind the primary.
Lab¶
Build a small replication simulator in code/replication/ with a leader, followers, and explicit log delivery. Make session reads avoid stale followers, then compare overlapping and non-overlapping read/write quorums. Resolve a concurrent-write conflict with last-writer-wins and report writes lost when a follower is promoted. The tests control delivery and failures, so no threads, network, or real clock are needed. Run: cd code/replication && PYTHONPATH=starter python -m pytest tests -q
Real systems that use this¶
The linked Atlas cases include DynamoDB Global Tables across regions with data-residency rules, RDS high availability, active/active multi-region designs with three different write strategies, and a CQRS/Saga design that accepts eventual consistency on purpose.
Go deeper¶
- Interactive: Designed to Scale – replication, CAP
- Deeper: Kleppmann's Cambridge distributed systems notes (free); Designing Data-Intensive Applications, ch. 5
- Your handbook: Software Engineering Handbook, ch. 8 and ch. 10
See it in the wild¶
Open these real architectures and find the idea in the diagram. Predict how it is used before you read the analysis.




