Lesson 02

Quorums

Building block

A leaderless store keeps N copies of each key and asks two numbers of every request. W is how many replicas must confirm a write. R is how many must answer a read. When R plus W is larger than N, every group of replicas a read asks shares at least one replica with every group a write reached, so the read meets the latest confirmed write.

That overlap is the whole guarantee. The same three numbers say how many replicas can fail, and the cases the overlap does not cover are where the interviewer pushes next.

N, R and W

Take three copies, writes confirmed by two, reads answered by two. Two plus two is four, which is more than three, so any two replicas a read asks include at least one of the two a write reached. The read sees the write.

The same numbers say what the store survives. Writes go on while N minus W replicas are down, here one. Reads go on while N minus R are down, also one. Put the three copies in three zones, with the coordinator beside one of them, and a write waits for one confirmation from another zone: 1 to 2 milliseconds.

A request is as slow as the slowest replica it waits for, so a bigger R or W buys overlap with latency. Amazon's Dynamo paper gives three, two and two as the common setting used by several of its instances. It is an example of a balanced choice, not a rule.

Two settings for three copies
N 3, R 2, W 22 + 2 = 4, more than 3
Overlap: yes; every read meets the last confirmed write.
Down, writes still work: 1 replica.
Down, reads still work: 1 replica.
A write waits for: the second replica to confirm, 1-2 ms in another zone.
N 3, R 1, W 11 + 1 = 2, not more than 3
Overlap: none; a read can miss a write that succeeded.
Down, writes still work: 2 replicas.
Down, reads still work: 2 replicas.
A write waits for: the fastest replica, often the one beside the coordinator.

Turning the Dials

Each setting is a trade with a condition that earns it. Write to one and read from all: a write waits for a single replica, and a read fails whenever any replica is down. It suits a key written far more often than it is read. Its cost is durability: a confirmed write sits on one replica until it spreads, and losing that replica first loses the write.

Read one and write one is the fastest setting. One plus one is two, so there is no guaranteed overlap, and a read can miss a write that succeeded. Use it for data the product may show stale, such as a view count.

Two and two out of three is the setting for most keys. It stops being enough once reads and writes must both survive two replicas down. Then you need five copies and three for each: three plus three is six, more than five, and five minus three leaves two that may fail.

What the Overlap Does Not Buy

The overlap is a promise about confirmed writes, and three cases fall outside it. A read that arrives while a write is half done can see the new value on one replica, and the next read can miss it and return the old one.

A write that reached fewer than W replicas reports failure and still sits on the replicas it did reach. A later read may find it there, so a failed write is not an unwritten one.

Two writes to one key at the same moment leave the replicas disagreeing, and picking the newest needs versions, which is the last lesson of this chapter.

So the overlap is not linearizability, the model the next lesson defines. Daniel Abadi's PACELC paper says it directly: such systems cannot reach full consistency even when R plus W is greater than N. One leader whose ordered log a majority agrees on is a different tool, and Chapter 5 covers it under consensus.

Sloppy Quorums and Hinted Handoff

When a key's home replicas are unreachable, a strict quorum refuses the write. A sloppy quorum sends it to the next healthy nodes on the ring, the ring of Chapter 3's consistent hashing, with a hint naming the replica the write belongs to. When that replica comes back, the node holding the hint delivers the write to it.

What it buys: writes succeed through node and network failures. What it costs: until the hints arrive, the replicas a read asks and the replicas a write reached can miss each other, and the overlap is gone. Use it where refusing a write is worse than a stale read, such as a shopping cart. Leave it off for keys whose reads must meet the last write.

Hinted handoff while a home replica is down
Home replica downB is unreachable
→
Write goes to Ewith a hint naming B
→
B returnsE notices
→
Hint deliveredB has the write

Repairing the Copies

Two mechanisms bring the replicas back together. Read repair works on the read path: a read that finds an old copy on one replica writes the newest value back to it. It fixes only keys that someone reads.

Anti-entropy works in the background. Each replica keeps a Merkle tree per key range, a tree of hashes in which every parent is the hash of its children. Two replicas compare the roots. When the roots match, the range is identical. When they differ, the replicas descend only into the branches that differ, find the keys out of sync, and copy only those. The cost is rebuilding trees whenever key ranges move between nodes.

Anti-entropy with a Merkle tree
Compare the rootsthe hashes differ
→
Descendinto the branch that differs
→
Find the keysthe leaves that differ
→
Copy those keysnothing else moves

A replica back after an hour away catches up in that order. The hints held for it arrive first, then anti-entropy finds whatever the hints missed. The key-value store problem in Chapter 10 asks exactly this.

Strict Quorum vs Sloppy Quorum

A strict quorum counts only the key's home replicas, so R plus W above N really means overlap, and requests fail when too few homes answer. Use it when a read must meet the last confirmed write.

A sloppy quorum counts any healthy nodes and hands writes home later, so writes succeed through failures and the overlap is lost in the meantime. Use it when accepting the write matters more than reading it back at once.

How Interviewers Probe This

On quorums, say the three numbers first, then the overlap they give and the failures they tolerate, and finish on the case the overlap misses.

The first probe is "Three replicas. Why two for reads and two for writes?" Show the overlap, say that either operation survives one replica down, and name the faster setting you rejected and why. You might say: "Two and two overlap on every read, and each survives one replica down. One and one would be faster, but a read could miss a confirmed write, and these are account settings."

The second is "R plus W is greater than N, so every read is up to date?" Say that confirmed writes are seen, then name one case that breaks it, a read during a write, a sloppy write or two concurrent writes, and how the design copes with that case.

The third is "A replica was down for an hour. How does it catch up?" Hints first, then the Merkle-tree comparison for what the hints missed. Reads alone would never finish the job, because a key nobody reads would stay stale.

Misconceptions
  • "R plus W above N gives strong consistency." It gives overlap on confirmed writes. Reads during a write, failed partial writes and concurrent writes still break linearizability.
  • "A quorum is a majority." A quorum is whatever R and W you set. A majority is one choice, and read one, write all is a quorum too.
  • "Quorums are how Raft works." Raft's majority agrees on one leader's ordered log, which is Chapter 5. A leaderless quorum has no order, so it cannot promise the same.
  • "A failed write was not written." It may sit on the replicas it reached and be read later, so a client that retries must expect the first attempt to appear.
  • "Read repair keeps replicas in sync." It fixes only keys that are read. A key nobody reads stays stale until anti-entropy reaches it.
Why It Matters
  • Say N, R and W as numbers, and say how many replicas can be down for each operation.
  • Put the N replicas in different zones, and say the zone round trip each write waits for.
  • Name the case the overlap does not cover in this design, before the interviewer does.
  • Pair read repair with background anti-entropy, and say how a replica back from an outage catches up.
RelatedPostgreSQL Deep Dive a leader that waits for any one of several standbysConsistent Hashing the ring a sloppy write walksConcurrent Writes and Conflicts versions and siblingsLeader Election and Consensus a majority with an order

Knowledge Check

A store keeps five copies of each key. Which setting guarantees that a read meets the last confirmed write?

  • Writes confirmed by two replicas, reads answered by three replicas
  • Writes confirmed by three replicas, reads answered by three replicas
  • Writes confirmed by one replica, reads answered by three replicas
  • Writes confirmed by two replicas, reads answered by two replicas

N is 3, W is 2 and R is 2. How many replicas can be down while both reads and writes still succeed?

  • One replica, because each operation still needs two of the three
  • Two replicas, because each operation needs only one of them to agree
  • None at all, because every operation needs all three copies to agree
  • Two for writes and none for reads, because reads need every copy

During a network failure, a store accepts writes on nodes outside a key's home replicas and hands them back later. What does it give up meanwhile?

  • The guarantee that a read meets the last confirmed write to the key
  • The ability to accept writes for keys whose home replicas are down
  • Durability, since a write held for another replica stays in memory
  • The ring, since every key is rehashed onto the nodes still reachable

With W of 2, a write reached one replica and the client got an error. A later read returns the new value. Why?

  • The write stayed on the one replica it reached, and that read asked it
  • The error was wrong, because a write on one replica already counts as done
  • The read hit a cache that kept the value even though the store refused it
  • The client library queued the failed write and replayed it on its own

What does anti-entropy do that read repair does not?

  • Repairs keys faster, because it runs on every read before answering
  • Repairs keys nobody reads, by comparing hash trees for each key range
  • Repairs conflicts between concurrent writes by picking the newer one
  • Repairs replicas without copying any data, by comparing timestamps

You got correct