Tune reads and writes with quorums
You want strong reads sometimes and fast writes other times on a replicated store.
Say it like this
With three replicas I write to two and read from two, so any read overlaps the latest write without waiting for all three, and I can drop to one-of-three reads where stale data is fine.
Basic solution
With N replicas, write to W and read from R; R + W > N guarantees a read sees the latest write.
W=2 and R=2 across three replicas
The write is acknowledged by replicas 1 and 2. The read consults replicas 2 and 3. Replica 2 is in both sets, so the read returns the latest version even though replica 3 is stale.
How it works
- Each key is stored on N replicas, typically N = 3, chosen by hashing the key and taking the next N nodes on the ring.
- A write is sent to all N replicas, but the coordinator node that received the request replies to the client as soon as W of them have confirmed the write is on disk. The remaining replicas apply it whenever they can.
- A read is sent to R replicas. Each reply carries a version (a timestamp or counter); the coordinator returns the value with the newest version among the replies.
- If R + W > N, at least one replica in any read set also received the latest write, so the read cannot miss it: the read set and the write set are too big not to overlap.
- Replicas that missed a write are repaired in the background: read repair sends the newest value to any replica that replied with a stale one, and anti-entropy sweeps compare replicas and copy over the differences.
Common settings
- N=3, W=2, R=2: balanced, survives one replica down for both reads and writes.
- N=3, W=3, R=1: fast reads from any single replica, but writes stall if any replica is down.
- N=3, W=1, R=1: fastest, but a read can miss a write; only for data where staleness is harmless.
Worked example
Distributed cache backing a product catalogue
- 3 replicas per key across 3 zones. Zone round trip ~1 ms.
- W=2, R=2: a write waits for the fastest 2 of 3 acknowledgements, ~1 ms; a read likewise. Losing one zone leaves 2 replicas, still enough for both reads and writes to succeed.
- 100K reads/s and 5K writes/s: each read touches 2 replicas, so replica read load is 200K/s across the cluster, or 67K/s per zone. Size nodes for that, not for 100K, because the quorum doubles the read work.
- For the "recently viewed" list, switch to R=1: half the replica load and lower p99, and a stale list a few hundred ms old is invisible to the user.
- Hinted handoff holds writes for a replica that is down and replays them when it returns, so W=2 keeps succeeding during a zone outage without losing the third copy forever.
Variants and when each wins
Strict quorum (R + W > N)
- Reads and writes are sized so every read set overlaps every write set. It guarantees a read sees the latest acknowledged write and is the default when correctness matters.
Sloppy quorum with hinted handoff
- During a failure, writes go to any N healthy nodes rather than the designated ones, with a hint to deliver the data to the real owner later. It wins on availability, because writes never fail, at the cost that a read may miss the write until the hints replay.
Local quorum across data centres
- Acknowledgements are counted only within the local data centre (Cassandra LOCAL_QUORUM), so the request never waits for a cross-DC round trip. It wins for multi-region deployments; the remote DC catches up asynchronously.
Leader-based strong consistency
- A single leader per partition serialises all writes and serves linearizable reads, and the replicas exist for failover. It wins when you want simple semantics; the leader is the throughput bottleneck and failover needs consensus.
Use R=W=2 with N=3 by default; relax reads to R=1 per data class where staleness is acceptable; use LOCAL_QUORUM across regions.
Failure modes
Stale read despite a quorum
- A read returns an old value right after a write was acknowledged. It happens because the table is using a sloppy quorum, or because R + W ≤ N so the read set and the write set did not overlap. Fix it by checking the configured consistency levels per table and using a strict quorum for the data that needs it.
Quorum unavailable
- Writes fail with "not enough replicas". It happens because two of the three replicas are down, or a network partition has cut the coordinator off from them. Fix it by spreading replicas across zones so one zone failure costs one replica, enabling hinted handoff, and alerting on replica health before a second one is lost.
Latency spikes
- p99 jumps whenever one replica is slow. It happens because W=2 waits for the second-fastest reply, so one slow node is tolerated but two slow nodes are not. Fix it with speculative retry: after a latency threshold, send the request to another replica and take whichever answers first.
Divergent replicas
- Two replicas hold different values for the same version of a key. It happens because two clients wrote concurrently with W < N and nothing decided which write won. Fix it with last-write-wins on synchronised clocks, or with vector clocks so the application can merge the two versions on read.
Repair never catches up
- Stale replicas persist for days. It happens because anti-entropy repair is disabled or too slow for the write rate. Fix it by scheduling repairs; read repair on the hot keys handles the rest.
Interviewer follow-ups
Answer out loud before you open each one.
Prove that R + W > N gives you the latest write.
A write is acknowledged once W replicas hold it. A read consults R replicas. If R + W > N, the two sets must share at least one replica, because together they exceed the total. That shared replica holds the latest write, and the coordinator returns the newest version among the replies.
What happens to writes when one replica is down with N=3, W=2?
They succeed on the remaining two. The coordinator stores a hint for the missing replica and replays it when it returns, or anti-entropy repairs it later. Reads with R=2 still overlap the write set, so correctness holds throughout.
How do you resolve two concurrent writes to the same key?
Either last-write-wins using a timestamp, which is simple but can drop a write under clock skew, or keep both versions with vector clocks and let the application merge on read, as Dynamo did with shopping carts. Say which the product can tolerate.
Why not just use a single leader?
A leader gives simpler, linearizable semantics but caps write throughput at one node per partition and needs consensus for failover. Quorums let any replica coordinate, so the system stays writable through single-node failures without an election, at the cost of weaker ordering guarantees.
Connections
- In study paths
- Surviving a failed node
- Built on
- Merkle tree
- Pairs with
- Survive losing a node · Accept a short delay for scale · Pick consistency or availability under partition
- Pulls against
- Agree on one leader or one value
- Used in
- Distributed Key-Value Cache (Redis / Memcached-like)