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.
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.
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?
- Synchronous: the leader waits for a follower to confirm before acknowledging. A leader crash loses nothing acknowledged. But a slow or dead follower stalls every write.
- Asynchronous: the leader acknowledges as soon as its own write is durable. Fast and unaffected by followers — and a leader crash loses whatever had not shipped yet.
- Semi-synchronous: wait for one follower (or a quorum), not all. This is what most production
setups actually run: Postgres
synchronous_standby_nameswithANY 1 (...), MySQL semi-sync, Kafkaacks=allwithmin.insync.replicas=2.
“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.
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.
“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:
- Detection is a timeout. Too short and a GC pause triggers a needless failover; too long and you are down for longer.
- Choosing should pick the most up-to-date follower, and with async replication even that one may be missing writes.
- Split brain: the old leader was only slow, comes back, and still believes it is the leader. Two leaders accept conflicting writes. Prevention is fencing: the new leader is elected with a higher term or epoch number, and storage or clients reject anything carrying an older one. This is the same idea as the fencing token in the Redis lock section.
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
- How many replicas? Three per shard is the default: survive one failure and still have a quorum. Five if you must survive two at once or a whole zone plus one node.
- Across zones: put the three in three availability zones. Cross-zone latency is ~1–2 ms, so semi-sync to one other zone is affordable.
- Across regions: cross-region round trips are 60–150 ms. Synchronous cross-region writes put that on every write. Most designs replicate asynchronously across regions and accept a few seconds of potential loss in a region failure — say the number out loud (the RPO).
- Read replicas: if one node serves ~10k reads/s and you need 60k, that is six replicas — but check the lag tolerance of each read path before sending it to one.
The follow-ups, and the one-line answers
- “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.
- “What happens to acknowledged writes on failover?” With async replication, some can be lost; semi-sync to one follower is the usual fix.
- “How does Cassandra stay consistent?” Quorums with
R + W > N, plus read repair and anti-entropy to converge replicas over time. - “Is your system CP or AP?” Per operation: these reads tolerate staleness and stay available; these writes refuse rather than diverge.
- “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.
- “Multi-region writes?” Only if writes partition by region or merge cleanly; otherwise one home region per user, with async replication everywhere else.