Sub topic 8
Tunable consistency with read and write quorums
This chapter assumes you know how a key maps to N replicas through a preference list, and that you can read short Python snippets. It covers how a client-visible operation turns into messages to those replicas, and how three numbers (N, R and W) let you trade consistency against latency and availability.
The three knobs
Each key is stored on N replicas. A quorum is the number of replicas that must respond before an operation counts as done.
- N is the replication factor: how many replicas hold each key.
- W is the write quorum: how many replicas must acknowledge a write before the coordinator reports success.
- R is the read quorum: how many replicas must answer a read before the coordinator returns a result.
The coordinator is the node handling the request. It sends every request to all N replicas, not only W or R of them. It then waits for the first W or R responses and returns. The remaining replicas still receive the write, so they usually catch up without help.
Why R + W > N works
Suppose a write succeeded on W replicas. A later read contacts R replicas. If R + W > N, those two sets must share at least one replica, because there are only N replicas in total and the sets together are larger than N. That shared replica holds the latest successful write. The coordinator compares the versions in the R responses and returns the newest one.
The simulation below makes this concrete. A write with W=2 reaches replicas 0 and 1, and replica 2 is still stale. We then try every possible read set of size R.
from itertools import combinations
N = 3
replicas = [{"v": 1, "val": "old"} for _ in range(N)]
def write(targets, version, value):
for i in targets:
replicas[i] = {"v": version, "val": value}
def read(targets):
answers = [replicas[i] for i in targets]
return max(answers, key=lambda a: a["v"])
write([0, 1], 2, "new") # W = 2, replica 2 has not applied it yet
for r in (1, 2):
results = [read(c)["val"] for c in combinations(range(N), r)]
print(f"R={r}:", results)The output is:
R=1: ['new', 'new', 'old']
R=2: ['new', 'new', 'new']With R=1 and W=2 (sum 3, not greater than 3), one read set hits only the stale replica. With R=2 and W=2 (sum 4), every read set overlaps the write set.
The overlap check from the spine is one line:
def is_overlapping_quorum(n: int, r: int, w: int) -> bool:
"""True when every read quorum must intersect every write quorum."""
return r + w > n
print(is_overlapping_quorum(3, 2, 2)) # True
print(is_overlapping_quorum(3, 1, 1)) # FalseCommon configurations
Choosing R and W moves cost between reads and writes. The table assumes N=3.
| R | W | Overlap | Reads tolerate | Writes tolerate | Typical use |
|---|---|---|---|---|---|
| 2 | 2 | Yes | 1 replica down | 1 replica down | Balanced default |
| 1 | 3 | Yes | 2 replicas down | 0 replicas down | Read-heavy data that changes rarely |
| 3 | 1 | Yes | 0 replicas down | 2 replicas down | Write-heavy data, rare reads |
| 1 | 1 | No | 2 replicas down | 2 replicas down | Caches, counters you can lose |
A request that needs R or W replicas can only succeed if that many replicas are reachable. Reads tolerate N − R failures and writes tolerate N − W failures. The R=1, W=3 row shows the cost: one dead replica blocks every write to that key.
Latency follows the same logic. An operation finishes when the R-th or W-th fastest replica answers. A larger quorum means you wait for slower replicas, so tail latency rises. With W=1 you take the fastest acknowledgement, but fewer replicas hold the data at the moment you return success.
Choosing values
Work from the requirement, not from a favourite number.
- Pick N from durability needs. N=3 is common because a key survives two simultaneous node losses and the cost is three copies.
- Decide whether reads must see the latest acknowledged write. If yes, require R + W > N.
- Decide which operation is more frequent. Make the frequent one cheap by giving it the smaller quorum, and the rare one the larger.
- Check availability. For each failure scenario you care about (one node, one rack), confirm that N minus the failed replicas is at least R and at least W.
Many stores expose R and W per request, not per cluster. A shopping cart might write with W=1 for speed, while an account balance uses R=2, W=2.
What overlap does not give you
The overlap guarantee is narrower than people expect. The following situations all occur with R + W > N.
- Failed writes leave residue. If a write reaches one replica but never reaches W, the client gets an error. That replica still holds the new value, and later reads may or may not return it.
- Concurrent reads and writes. While a write is in flight, one read may reach an updated replica and return the new value. A second read that starts afterwards may reach only stale replicas and return the old one. The value appears to go backwards.
- Concurrent writers. Two clients writing the same key at the same time both reach quorums. The read sees two versions and needs a rule (vector clocks, last-write-wins) to pick or merge them.
- Clock-based ordering. If versions are timestamps from different machines, "newest" depends on clock skew. A write with a lagging clock can lose to an older one.
Sloppy quorums
A strict quorum counts only replicas from the key's preference list. A sloppy quorum counts the first N healthy nodes it can find, even if some are outside the preference list. When a real replica is down, a stand-in node accepts the write and holds a hint for later delivery. This keeps writes available during failures.
The cost is that the overlap argument breaks. The W acknowledgements may come from stand-ins that no reader contacts. A read of R replicas from the real preference list can then miss the write even though R + W > N. Hinted handoff, covered in a later chapter, delivers the data once the real replica returns. Until then, sloppy quorums give availability, not the overlap guarantee.
Read repair in the read path
During a quorum read, the coordinator receives R responses, and these may differ. It returns the newest version, and it can also send that version to any replica that answered with an older one. This is read repair. It means the read path slowly fixes stale replicas on keys that clients actually access. Keys that nobody reads need a background process, which a later chapter covers.
Failure modes to plan for
| Situation | Effect | Mitigation |
|---|---|---|
| Fewer than W replicas reachable | Writes fail | Lower W per request, or accept the error |
| Fewer than R replicas reachable | Reads fail | Lower R, accepting possible stale data |
| Write acknowledged by W, third replica slow | Brief staleness on that replica | Read repair, anti-entropy |
| Coordinator crashes after partial write | Client sees a timeout, unknown outcome | Make writes idempotent so the client can retry |
| Network partition splits replicas | Only the side holding a quorum can serve strict-quorum requests | Size N and quorums for the partitions you expect |
Note the timeout row. A client that times out does not know whether the write applied. Retrying a put with the same value and version is safe. Retrying an increment is not, so a key-value store usually offers only idempotent operations.
Exercise
A cluster uses N=5. A team sets W=2 and R=3 for a key that is read often and written rarely. Does this configuration guarantee that reads overlap the latest successful write? How many replica failures can writes and reads each tolerate? Suggest a change that keeps the overlap and makes reads cheaper.
Show answer
R + W = 5, which is not greater than N=5, so the overlap is not guaranteed. A write could reach replicas 1 and 2 while a read contacts replicas 3, 4 and 5 and misses it. Writes tolerate N − W = 3 failures and reads tolerate N − R = 2 failures. To keep the overlap and make reads cheaper, use R=2 and W=4 (sum 6 > 5). Reads then tolerate 3 failures and need only two responses, but writes tolerate only 1 failure. Since the key is read often and written rarely, that is an acceptable trade.
Exercise
In the simulation above, change the write to reach only replica 0 (W=1) and keep R=2. Predict which read sets return "old", then explain why R=2 no longer protects the reader.
Show answer
The read sets (1, 2) return "old", because neither replica 1 nor 2 received the write. The sets (0, 1) and (0, 2) return "new". With W=1 and R=2 the sum is 3, which is not greater than N=3, so a read set can avoid the single replica that holds the write.