System design lab · Episode 13

Quorums: when does a read see the latest write?

A database keeps three copies of each value. Waiting for all three on every write is slow; waiting for one is fast but risky. A quorum is the middle way: wait for W copies on a write and ask R copies on a read. Follow the lessons next to the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
00102030
A write on its way, and the wires of the last readReplicas 1 to 3; the number on top is the version each one holdsWriter at the left, reader at the right

Controls

W: copies a write waits for
R: copies a read asks
Replicas
Time

Want the details?

What is a quorum?

To survive a broken server, a database keeps several copies (replicas) of each value, usually N = 3. Copying takes time, and some replicas are slower or farther away. So the database must decide how many copies to wait for.

A quorum is that number. A write is confirmed when W replicas have it. A read asks R replicas and keeps the newest answer. If R + W > N, every set of R replicas overlaps every set of W replicas, so at least one replica in every read has the newest confirmed write. If R + W ≤ N, a read can miss it.

How to read the bench
Part on the benchWhat it means in a real system
Box at the leftThe writer (a client). Its number is the last confirmed version.
Ball on a laneA write on its way to one replica. Replica 1 is near (0.3 s), 2 is farther (1.2 s), 3 is far (2.6 s).
Replica boxesThe three copies. The number on top is the version each holds. A cross: it is down.
Lit wiresThe replicas that answered the last read.
Box at the rightThe reader. Its number is the value it got; a cross means the value was older than the last confirmed write.
The numbers
  • N = 3 replicas. A new version every 2 s after the previous one is confirmed. A read every 0.6 s.
  • W = 1, R = 1: about a third of reads are stale; 26 versions a minute.
  • W = 2, R = 2: no stale reads; 19 versions a minute; still works with one replica down.
  • W = 3, R = 1: no stale reads; 13 versions a minute; with one replica down, no write is ever confirmed.
  • W = 1, R = 3 with one replica down: every read fails.

The test checks these numbers, and that with R + W > 3 no read is ever stale through hundreds of random changes of W, R and outages.

In real systems
  • Apache Cassandra lets you choose the consistency level per query: ONE, QUORUM, ALL. QUORUM for both reads and writes gives R + W > N.
  • Amazon's Dynamo paper made N, R and W famous; Riak followed the same idea.
  • Raft and Paxos (the replication lab) also use majorities: a write is safe once a majority has it.
  • Even with R + W > N, some edge cases remain in real systems (writes that fail half-way, clocks that disagree). Databases add tools such as read repair and version vectors.
  • Tune for the workload: many reads? Use a small R and a large W. Many writes? The opposite.
Words to know
Replica
One copy of the data, on its own server.
N, W, R
Number of copies; copies a write waits for; copies a read asks.
Stale read
A read that returns an older value than one already confirmed.
Quorum
The number of replicas that must agree, chosen so that groups overlap.
Eventual consistency
The replicas will agree in the end, but a read right now may be old.