← All lessons

Core concepts

Replication and consistency

Copies make a system survive failures and serve more reads. They also make "the current value" a question with more than one answer, and the interview is about which answer you chose on purpose.

20 min readUpdated November 2026Free

You replicate data for three reasons: to survive a machine dying, to serve more reads than one machine can, and to put data near users in other regions. All three are about having copies. The cost is that copies can disagree, and the moment they can disagree you have to say which one a reader sees.

“We’ll add read replicas” is a fine sentence. The follow-up is always some version of “a user updates their profile and refreshes — what do they see?”, and this lesson is about answering it precisely.

Three shapes of replication

Single leader

One node (the leader or primary) accepts every write and streams changes to followers. Reads can go to either. Postgres streaming replication, MySQL, MongoDB replica sets, Redis replication and Kafka partitions all work this way.

It is the default for good reason: there is exactly one order of writes, so there are no write conflicts to resolve. The cost is that the leader is a bottleneck for writes and a single point of failure until a follower is promoted.

Multi-leader

Several nodes accept writes — typically one per region — and replicate to each other.

It buys local write latency in every region and survival of a whole region going away. The cost is write conflicts: two regions update the same row concurrently and both succeed locally. Somebody has to decide what the row ends up as, which is why most teams avoid multi-leader unless writes naturally partition by region (a user’s data lives in their home region) or the data type merges cleanly.

Leaderless

Any replica accepts reads and writes. The client (or a coordinator) sends each write to all N replicas and waits for W acknowledgements; each read asks R replicas and takes the newest answer. Cassandra, ScyllaDB, Riak and the original Dynamo work this way.

The point

R + W > N means every read set overlaps every write set, so at least one replica you read from has the latest acknowledged write. With N = 3, W = 2, R = 2 is the usual choice: it tolerates one replica being down for both reads and writes. It is not full linearizability — concurrent writes, sloppy quorums and failed partial writes all leave gaps — but it is the right first answer for “how does Cassandra stay consistent?”

Synchronous vs asynchronous, the decision that matters most

When the leader acknowledges a write, how many copies exist?

Where candidates lose it

“We have replicas, so no data is lost on failover” is only true if replication is synchronous. With the default asynchronous setup, promoting a follower after the leader dies silently discards acknowledged writes that never reached it. Know which mode your design uses and say it. The Redis lesson has the same caveat for Redis replication.

Replication lag, and the three anomalies it causes

Asynchronous followers are behind — usually by milliseconds, sometimes by minutes during a bulk load or a network hiccup. Reading from them produces three classic problems, each with a named guarantee that fixes it.

A user writes name equals Priya to the leader, which acknowledges at once. The change reaches the follower later. In between, the user's read is routed to the follower and returns the old name, Pri. Once the follower replays the change, reads return Priya. UserLeaderFollowerwrite 'Priya'Priyaack ✓replication lag: follower still has 'Pri'refresh → readrouted to followerreturns 'Pri' ✗Priyareplayed —now consistent
Acknowledged is not the same as replicated. Route a user's own reads to the leader for a short window after they write.

Read-your-writes

A user saves their new display name, the page redirects, the read goes to a lagging follower, and the old name comes back. They save again. Support gets a ticket.

Fixes: read the user’s own data from the leader for a short window after they write (say, ten seconds, tracked in their session); or remember the write’s log position (Postgres LSN, MySQL GTID) and only read from a follower that has replayed past it.

Monotonic reads

A user refreshes twice and the comment count goes 12 → 14 → 12, because the second read hit a fresher replica than the third. Time appears to go backwards.

Fix: pin each user to one replica (hash the user id to choose it), so their reads only ever move forward.

Consistent prefix

In a sharded system, a reader sees an answer before the question it answers, because the two were written to different partitions that replicate at different speeds.

Fix: write causally related data to the same partition, or carry explicit ordering information (a version or a causal token) and wait for it.

The consistency vocabulary, ranked

From strongest to weakest, roughly:

Model What a reader is promised Who gives it
Linearizable Every read sees the latest completed write, as if there were one copy A single leader read from the leader; Spanner; etcd/ZooKeeper; consensus
Sequential Everyone sees writes in the same order, maybe late Leader-based logs
Causal If B depends on A, nobody sees B without A Some session guarantees; MongoDB causal sessions
Read-your-writes / monotonic Guarantees for one client’s view Session-level routing
Eventual If writes stop, replicas converge — eventually Async replicas, leaderless defaults, DNS

The useful move in an interview is not to pick the strongest. It is to pick per operation: “the balance check needs linearizable reads from the leader; the feed tolerates eventual.”

CAP, said correctly

CAP says that when a network partition separates replicas, a system must choose between consistency (refuse some requests so nobody reads stale data) and availability (answer everything, possibly stale). It is a statement about behaviour during a partition, not a menu of two-out-of-three.

Where candidates lose it

“We’ll pick CA” is the answer that tells an interviewer you have memorised the acronym. Partitions are not optional in a distributed system — networks fail — so the real choice is only between C and A when one happens. A single-node database is “CA” only because it is not distributed.

PACELC is the more useful framing: if there is a Partition, choose A or C; Else, in normal operation, choose Latency or Consistency. Most of the time there is no partition, and the everyday trade is that waiting for more replicas makes every request slower. Cassandra at consistency level ONE is the textbook PA/EL; Spanner is PC/EC, paying for consistency in latency even when nothing is wrong.

Failover, and the split brain it can cause

When the leader dies, something must notice, choose a follower, promote it and redirect clients. Each step can go wrong:

Use a consensus system (etcd, ZooKeeper, or the Raft built into your database) to decide who the leader is. Hand-rolled “whoever pings first wins” is how split brain happens.

Sizing it on the whiteboard

The follow-ups, and the one-line answers

  1. “A user edits their profile and refreshes. What do they see?” Their new value: their own reads go to the leader for a short window, or to a follower past their write’s LSN.
  2. “What happens to acknowledged writes on failover?” With async replication, some can be lost; semi-sync to one follower is the usual fix.
  3. “How does Cassandra stay consistent?” Quorums with R + W > N, plus read repair and anti-entropy to converge replicas over time.
  4. “Is your system CP or AP?” Per operation: these reads tolerate staleness and stay available; these writes refuse rather than diverge.
  5. “How do you prevent two leaders?” A consensus system elects the leader with a monotonically increasing term, and anything with an old term is fenced out.
  6. “Multi-region writes?” Only if writes partition by region or merge cleanly; otherwise one home region per user, with async replication everywhere else.