Systemsfor Aravinda
0 / 78 core
Week 4 · Distributed systems

Quorums and leaders

2 hours · core path

Read first: Replication, Consistency models

In one sitting

Pick N replicas, read R of them, and write W of them. If R plus W is greater than N, a read set and a write set share a node, so a reader can see the latest write when those quorums land on the nodes that hold it. Sloppy quorums, retries, and lost replies break that story without breaking the arithmetic.

You have N copies of a value. A quorum is the smallest set of those copies you refuse to proceed without. The number is the whole design: too small and a reader misses a write, too large and one dead disk stops the product.

Core concepts

Call the replica count N. A write is acknowledged after W replicas have stored it. A read consults R replicas and then decides what the value is. You choose the three numbers. They are not a mood.

If R + W is greater than N, every read set intersects every write set. Two sets that did not intersect would contain at most N replicas, so their sizes would add to N or less. The inequality forbids that. The smallest overlap is R + W - N, which is a derivation from the sizes, not a measurement. For the interview default, N is 3, R is 2, and W is 2. Then 2 + 2 - 3 is 1. A reader and a writer share at least one replica.

In the happy case that shared replica holds the latest write, so the reader can see it. “Happy case” is doing real work in that sentence. The write quorum has to be a subset of the N nodes you think hold the key. The read quorum has to touch those same nodes. The coordinator has to notice which of the R answers is newer. Overlap puts the latest value on at least one of the nodes you asked. It does not pick the answer for you. If you return the first reply and ignore the rest, you can miss the overlapping copy that had the new value.

That overlap is not linearizability. Linearizability needs each operation to take effect at one point between its start and its finish, in real time. A quorum gives you a chance to observe the latest stored write when the quorums hit the nodes that hold the data. Sloppy quorums, retries, and lost replies take the chance away, and they are normal failure behavior, not a footnote.

Sloppy quorums and hinted handoff

A strict quorum uses the preferred replicas, the N nodes that are supposed to own the key. If too many of them are down, W of them are not reachable, and a strict write fails.

A sloppy quorum keeps the write alive by storing it on fallback nodes that are not in that preferred set. The write still reaches W processes. The client gets success. The overlap proof no longer applies, because the proof was about subsets of the same N. A later read of the preferred N can miss the write completely.

Hinted handoff is the repair. The fallback remembers which preferred node should have taken the write, and it sends the write home when that node returns. Until the hint lands, you bought availability and gave up the guarantee that a read quorum sees the write. After the hint lands, the preferred nodes catch up. If a hint is lost, you need some other way to notice the divergence. The Dynamo paper uses Merkle trees for that comparison and gossip for membership. Those are part of the Amazon Dynamo story. This page is the quorum.

Say the name of the paper when you use these words. Dynamo, SOSP 2007, is the shopping-cart store that wanted to stay writable during a partition. DynamoDB is a later product. It uses a primary per partition and conditional writes. Describing DynamoDB and calling it the paper is a wrong answer.

The paper also used vector clocks so a client could see that two writes were concurrent and merge them. The pain they wrote down is that the clocks grow and the client has to merge. That machinery lives on the clocks page. A quorum does not, by itself, tell you which of two overlapping values happened first.

Leaders are a different bet

A quorum system has no single writer. Any coordinator that can gather W replicas can accept a write. Order is something you reconstruct afterward, with versions, clocks, or a merge.

A leader system picks one writer. Every write goes to the primary. Replication then copies it to the backups. Synchronous copies stall the write on a slow backup. Asynchronous copies return now and can lose the tail on failover. A read of a backup can be stale, which is why read-your-writes needs the primary, a session token, or a version check.

Failover is the dangerous moment. Two primaries is split brain: both accept writes, and you no longer have one order. A lease or a generation number is what stops the old primary. The storage records the highest generation it has accepted and rejects a write that arrives with an older one. A Raft term is that generation when the leader was elected. The term is checked on the write. “I used to be leader” is not checked by anyone if you forget to send it.

The bully algorithm is a poor answer for this election. It picks the highest identifier that answers. It does not hand the storage a token that makes the previous winner’s writes illegal. A process that paused can wake up, still believing it won, and write. Clocks alone are a poor answer for the same reason. Skew can make two nodes both believe their lease is still valid. The disk has to enforce the token. The clock is not the disk.

A quorum read and a leader-lease read spend that token differently. A quorum read contacts R replicas on every read, waits for the slower of the responses it needs, and keeps the newest version. There is no timer. Any replica that stored the write can be in the set. A leader lease lets the leader answer from its local log without gathering R replies, for as long as the lease says it is the only leader. The read is one round trip, and the tail is one node instead of the max of R. When the lease ends, that local answer is no longer a stand-in for a quorum. The next leader has to wait out the lease, and the old leader’s token has to fail at the storage, or both of them serve a read that claims to be current. You take the lease when you already have one ordered log and you want the thinner tail. You take the quorum read when any replica may have accepted the write and you do not want a timer between the client and the latest value.

A Raft majority and a Dynamo-style quorum are easy to blur because both say “more than half” in the N = 3, W = 2 case. They are different promises. A Raft majority stores one log prefix under one leader in one term, and a commit means a later leader must carry that entry. A leaderless quorum is any W of the N replicas, possibly chosen again on the next request, with no log matching behind them. Use the overlap math for the leaderless case. Use the term and the log when you have a leader. Do not cite one as the proof of the other.

CAP shows up here as a behavior, not a label. During a partition, a strict quorum that cannot gather W refuses the write. The side that is up still errors. A sloppy quorum answers anyway and reconciles later. Those are the two sides of the partition. On a healthy day the else branch is still there: waiting for a far replica costs a round trip even when every node is alive.

A worked example

Three replicas, A, B, and C. One key. N is 3, R is 2, W is 2. You are walking a single write of a new value, then a read, with C down. Assumptions, so the arithmetic has a home:

  • A and B are up. C is unreachable. You do not yet know whether C is dead or partitioned. You only know the coordinator cannot use it.
  • The three replicas are in one datacenter. Dean’s same-datacenter round trip, 0.5 ms, is the cost of contacting one of them. You contact them in parallel, so a quorum waits for the second response, not for the sum, and not for the third once two have answered.
  • Volume, so “the writes still succeed” has a size: you assume 1,000,000 writes a day. That is an interview assumption, not a measured product.
  • The client sends one value, v2. There is no second concurrent writer in this first walk.
N = 3
R = 2
W = 2
R + W = 4, which is greater than 3
minimum overlap = R + W - N = 1

C is down
live replicas = A, B
write quorum = A and B, size 2
read quorum = A and B, size 2
overlap = A and B, which is more than the minimum of 1

1 day = 86,400 seconds
shortcut: a day is about 10^5 seconds
1,000,000 / 86,400 = about 12 writes per second
those 12 still commit, because W is 2 and 2 replicas are up

if W were 3, every one of those writes would fail until C returned
if R were 3, the write could land and the read could not

Walk the messages. The coordinator sends v2 to A and to B. Both store it. C never answers. W is satisfied, so the coordinator tells the client the write succeeded. The network piece of that wait is about one 0.5 ms round trip, the slower of A and B. You do not add a second 0.5 ms for the second replica if the calls are in flight together. You also do not wait out a timeout for C once A and B have answered.

A reader needs two replicas. The only live pair is A and B. Both hold v2. The overlap is the entire pair. The reader returns v2. One node down did not stop the write or the read, and the reader saw the write. That is the interview sentence for this configuration.

The inequality is strict, and it is worth showing the configuration that looks one step away and fails. W = 1 and R = 2 gives 1 + 2 = 3, which is not greater than N. A write that landed only on C, while C was briefly up, is invisible to a read of A and B. Particular reads still get lucky when the write happened to hit A. The guarantee was about every write and every read. Lucky is not the guarantee.

Now break the happy case in the two ways the definition warned you about.

Sloppy quorum. Change the outage. A and B are both down. Only C is up from the preferred set. A strict W of 2 cannot be formed. The coordinator writes to C and to D, a fallback that is not one of the three preferred replicas, and it tells the client success. Later A and B return, and D has not handed the write off yet. A reader who asks A and B, a legal read quorum of the preferred N, sees the old value on both. R + W greater than N did not save them. The write quorum was not inside the N the reader used. Hinted handoff is the promise that D will copy the write to A or B later. Until that copy lands, the cart can forget an add that the client was told succeeded. That is the availability you bought.

Lost reply, then a retry. Go back to A and B up, C down. Both A and B store v2. The ack from the coordinator to the client is lost. The client’s timeout fires. The client believes the write failed. A different reader asks A and C, gets v2 from A, and observes a write that the writer was told did not happen. Linearizability has nowhere clean to put an operation that failed and is nevertheless visible to a later read. Then the client retries. If the retry is a different value, call it v1, and the coordinator does not condition the write on the version it finds, a new quorum can store v1 on top of v2. A later reader can observe v2 and then v1. The overlap of each individual quorum still happened. The history is not linearizable. The labels below are order, not durations.

1  A and B store v2
2  the ack to the client is lost
3  the client observes failure
4  a read of A and C returns v2
5  the failed write is visible
6  the client retries with v1 and no condition
7  a new quorum stores v1 after v2

The fix for that retry is a condition or an idempotency key, not a larger W. Writing the same value to more replicas does not make a late retry harmless.

A leader handles the same outage differently, and you should be able to say how. One primary, generation 7, pauses for longer than its lease. A new primary is elected with generation 8 and writes. Generation 7 wakes up and writes with the old generation. Storage that remembers the highest generation rejects 7. Storage that trusts the writer’s clock accepts both, and you have split brain with extra steps. The generation is the fence. The lease only told the rest of the cluster when they were allowed to elect.

flowchart TB
  coord[Coordinator]
  nodeA[Replica A holds the write]
  nodeB[Replica B holds the write]
  nodeC[Replica C is down]
  coord --> nodeA
  coord --> nodeB
  coord --> nodeC

The diagram is the first walk, not the sloppy one. Two arrows succeed, the third is down, and W is still met. If you cannot say which arrows you are willing to drop, you do not have N, R, and W yet.

Tradeoffs

Choice What stays true What breaks
W = N and R = 1 A finished write is on every replica, so one read sees it One down replica blocks every write
W = 1 and R = N A write succeeds if any replica is up One down replica blocks every read, and you must reconcile N answers
N = 3, R = 2, W = 2 One replica can be down for both reads and writes You have to compare versions across the two answers
Strict quorum only The overlap proof applies to the preferred N The write fails when the preferred nodes cannot form W
Sloppy quorum plus hinted handoff The write succeeds on fallbacks Readers of the preferred N can miss it until the hint lands
Single leader and a fencing token One order, and the old leader’s writes die The side that cannot reach the leader cannot write
Bully election or a bare clock A name for who might be primary No token the storage can reject, so two writers survive

Contacting R replicas in parallel makes the wait the R-th response. Contacting them one after another makes the wait the sum, which is a self-inflicted queue. The table does not save a coordinator that does the calls in series and then blames the quorum.

Pitfalls

  • Treating R + W greater than N as a proof of linearizability. The inequality is about sets of replicas. Linearizability is about real time, lost acks, and retries.
  • Returning the first of the R replies. The overlapping replica may be the slower one. You waited for it and then threw it away.
  • Using a sloppy quorum and then advertising the strict overlap. Until hinted handoff finishes, a preferred read quorum can miss the write.
  • Answering “Dynamo” and then describing a primary per partition. That is DynamoDB. The paper is leaderless, with sloppy quorums and client merges.
  • Electing a primary with the bully algorithm, or with the highest clock, and skipping the fencing token.
  • Setting W = 1, R = 1, N = 3 because both calls are fast, and discovering that most reads miss most writes. 1 + 1 is not greater than 3.
  • Waiting for all N when you only need R or W. One slow replica then sets every latency, which is the opposite of why you picked a quorum.

In the interview

Say the three numbers in the first minute. “N is 3, R is 2, W is 2. One replica is down. The write still lands on the other two, the read uses those same two, and the overlap is the pair. Minimum overlap in general is R + W - N, which is 1 here.” The bottleneck is the slower of the two live replicas, about a 0.5 ms round trip if they are in one building, not a timeout spent on the dead one. The tradeoff is the next sentence: a sloppy quorum would have accepted the write with two preferred nodes down, and a reader of those preferred nodes could miss it until hinted handoff.

Skip Merkle trees and gossip unless they ask how Dynamo finds a replica the hint never reached. Skip a proof that quorum overlap is linearizability. It is not, and saying so is the point.

The question that usually follows is the lost ack, or the split brain. For the ack: the write can be stored, the client can be told it failed, and a retry without a condition can roll the value backward. For the leader: the old primary needs a generation the storage rejects. A clock is not that generation.

Check yourself

N is 3, R is 2, W is 2, and replica C is down. Can a write and a later read both succeed, and do they overlap?

Yes, if A and B are up. The write quorum is A and B. The read quorum is A and B. They overlap in both replicas, which is more than the minimum of R + W - N = 1. About 12 writes a second, if you assumed a million writes a day, still succeed. W = 3 would fail every one of them until C returned. The reader still has to look at both answers and keep the newer value.

Why is R plus W greater than N not the same statement as linearizability?

The inequality says a read set and a write set of the same N share a replica, so in the happy case the reader can observe the latest write stored on those nodes. Linearizability also requires a real-time point for the operation, and it fails when the ack is lost, when the client retries a different value, or when a sloppy quorum stored the write outside the set the reader consults. Each of those can happen while every individual quorum had the size you asked for.

Preferred replicas A and B are down. The write went to C and fallback D. What can a reader of A and B see before hinted handoff?

The old value. A and B are a legal read quorum of the preferred N, and neither of them has the write. The overlap proof does not apply because the write quorum was not a subset of that N. Hinted handoff is D later copying the write home. Until the copy lands, the success you returned to the client is not visible to those readers. That is the availability the sloppy quorum was for.

A write is stored on a quorum and the ack is lost. The client retries with an older value. What goes wrong?

The client observed failure, and a later read can still see the stored value, so the operation took effect and was also reported as a failure. The retry then installs the older value on a new quorum if nothing conditions the write. A reader can observe the new value and then the old one. The repair is a version condition or an idempotent retry, not a larger W.

Why is the bully algorithm, or the highest clock, a poor way to choose a leader?

The bully algorithm picks the highest identifier that answers, and a clock picks whoever thinks its lease is still valid. Neither gives the storage a generation it can reject. A paused leader can wake up and write beside the new one. A lease may tell the cluster when to elect. The fencing token, or a Raft term checked on every write, is what makes the old leader’s writes fail.

Based on: Amazon Dynamo, SOSP 2007, DeCandia and others; Gilbert and Lynch; Raft, Ongaro and Ousterhout; Designing Data-Intensive Applications, Kleppmann and Riccomini.