Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Systems · Consistency

R plus W.

Dynamo-style stores don’t force you to choose strong or eventual consistency once — they let you tune it per operation. With N replicas of a key, a write is acknowledged once W replicas store it, and a read gathers answers from R replicas and returns the newest. The one rule that decides everything: if R + W > N, the read set and the write set are forced to overlap in at least one replica — so a read is guaranteed to see the latest acknowledged write (strong consistency). Let R + W ≤ N and the sets can miss each other, so a read may return a stale value. Slide R and W, then write and read, and watch the overlap decide.

tunable consistency · R+W>N ⇒ read sees latest write · latency vs consistency, per operation
N

The replication factor — how many copies of each key exist.

W

Write quorum — how many replicas must acknowledge a write before it’s "done".

R

Read quorum — how many replicas a read consults, returning the newest answer.

the rule

R + W > N forces read and write sets to overlap → strongly-consistent reads.

quorum.js — overlap or go stale
Ready
N=5 replicas of a key. A write updates the W left-most replicas; a read consults the R right-most and returns the newest version it sees. Adjust R and W, then write and read. R+W>N means the sets overlap.
3+3
R + W
0
overlap
–
last read

How it works

The guarantee is pure pigeonhole. A write is stored on some set of W replicas; a read consults some set of R replicas. If R + W > N, those two sets can’t fit into the N replicas without sharing at least one — so the read is guaranteed to touch a replica that has the latest write, and by returning the newest version it sees, it returns that write. If R + W ≤ N, you can choose disjoint read and write sets, and then a read might consult only replicas that never saw the new write, returning a stale value until background replication catches up. This is why the two common presets are W=N, R=1 (fast reads, slow durable writes) and R=N, W=1 (fast writes, slow reads), and why R=W=⌈(N+1)/2⌉ (a majority each) balances the two while still guaranteeing overlap.

1

Pick N, R, and W

N replicas hold each key. Choose how many must acknowledge a write (W) and how many a read consults (R). Higher W means more durable but slower writes; higher R means fresher but slower reads.

2

Write reaches W replicas

A write is considered successful once W replicas store the new version. The other N−W get it later via background replication.

3

Read gathers R replicas, takes the newest

A read asks R replicas and returns the highest version among them. Whether that’s the latest depends on whether the read set overlaps the write set.

✓

R + W > N guarantees freshness

When R + W > N the read and write sets must overlap, so a read always sees the latest acknowledged write. When R + W ≤ N, reads can be stale until replication catches up.

Strong reads
R + W > N
Fast reads
W=N, R=1
Fast writes
R=N, W=1
Balanced
R=W=majority

The code

# quorum read/write over N replicas def write(key, value): version = now() acks = send_to_all(key, value, version) wait_for(acks, count=W) # done when W replicas ack def read(key): replies = read_from_any(key, count=R) # gather R answers return max(replies, key=lambda r: r.version) # newest wins # if R + W > N, the R read replicas and W write replicas must # share at least one node -> the read sees the latest write.

The picture: why the sets must share a replica

R=3, W=3: 6 > 5, sets share r2, read gets v2 write set r0 v2 r1 v2 r2 v2 r3 v1 r4 v1 read set R=2, W=3: 5 = 5, no shared replica, read gets v1 r0 v2 r1 v2 r2 v2 r3 v1 r4 v1 read set shared replicas ≥ R + W − N 6 − 5 = 1 shared (top) · 5 − 5 = 0 (bottom)
The same layout the lab uses: writes land on the leftmost W replicas, reads ask the rightmost R. In the top run there are only 5 slots for 3 + 3 picks, so at least one replica, r2, is in both sets, and it holds v2. In the bottom run 3 + 2 picks fit side by side, and the read sees only replicas the write never reached. Real systems do not choose sides like this, so a low-quorum read is sometimes fresh and sometimes stale. Nothing guarantees which.

Worked example: how often does a read miss?

Take N = 5. A write has just reached exactly W replicas, and background replication has not caught up. A read picks R replicas at random. It misses only if every replica it picks is outside the write set, so the miss rate is C(N−W, R) / C(N, R). For W = 3, R = 2 that is C(2,2) / C(5,2) = 1/10.

RWR + WShared replicas, at leastChance a read missesWrites surviveReads survive
11204/5 = 80%4 down4 down
22403/10 = 30%3 down3 down
23501/10 = 10%2 down3 down
336102 down2 down
156100 down4 down
516104 down0 down

Two things show up. First, the cliff at R + W = N + 1. Going from (2, 3) to (3, 3) costs one more replica per read and takes the miss rate from 10% to zero, because the guarantee is pigeonhole, not probability. Second, the last two columns. A write needs W live replicas, so it survives N − W failures, and a read survives N − R. The balanced (3, 3) row is the only strong setting where both reads and writes survive two failures.

Trade-offs: latency and availability per setting

Say the five replicas answer in 2, 3, 4, 9 and 40 ms (illustrative). An operation waits for its k-th fastest reply, so Time is the k-th number in that list. Space here is how many replicas can be down before the operation fails. Storage is always N copies.

CaseTimeSpaceWhy
W=5, R=1write 40 ms, read 2 mswrites: 0 down, reads: 4 downFast reads, but one slow or dead replica stalls every write.
W=1, R=5write 2 ms, read 40 mswrites: 4 down, reads: 0 downFast writes, but a write acked by one replica is lost if that replica dies.
W=3, R=3write 4 ms, read 4 ms2 down for bothMajorities. Neither side waits for the slow tail, and both stay strong.
W=2, R=2write 3 ms, read 3 ms3 down for bothFastest and most available, but reads can be stale (30% above).

Waiting for the k-th reply rather than the last is why quorums hide tail latency. In this example the 40 ms replica only hurts the settings that need all five. Some clients go further and send the read to all N replicas but return after the first R answers, which trades extra network traffic for a lower tail. The dial can also differ per request: a shopping cart read can use R = 1 while a balance check uses a majority.

Common mistakes

  • Treating R + W > N as linearizable

    A read that runs while a write is half done can see the new value, and a later read can then see the old one, because the write has reached some replicas but not yet W. Getting real linearizability needs the reader to write back the newest value before returning (read repair on the read path).

  • Forgetting sloppy quorums

    With hinted handoff, a Dynamo-style store accepts a write on stand-in nodes outside the key’s home N during an outage. Then the read and write sets can miss each other even when R + W > N. Know whether your store does this before counting on the overlap.

  • Versioning by wall clock

    The page’s code stamps writes with now() for simplicity. In production, clock skew can give a newer write an older stamp, and last-write-wins then silently throws it away. Use logical versions, vector clocks, or a single writer per key.

  • Assuming a failed write left no trace

    If W = 3 and only 2 replicas acked, the client sees an error, but those 2 replicas still hold the value and may serve it later. Retries must be idempotent, because a “failed” write can come back.

  • Choosing an even N

    A majority of 4 is 3, so N = 4 survives one failure, the same as N = 3, while storing an extra copy. Odd sizes buy fault tolerance. Even ones mostly buy cost.

Where this shows up in production

  • Cassandra and Riak

    Both expose the dial per request. Cassandra calls it consistency level (ONE, QUORUM, ALL, LOCAL_QUORUM), and Riak uses n_val, r and w. Watch out with LOCAL_QUORUM: a write in one data centre and a read in another do not overlap.

  • Consensus logs

    Raft and Paxos commit with a majority and elect with a majority. Any two majorities of N share a node, which is the same pigeonhole as R + W > N, applied to leaders and log entries. The Raft lab on this site plays that out.

  • Repairing the replicas a quorum skipped

    The N − W replicas a write did not wait for catch up through read repair (fixing stale copies a read noticed) and anti-entropy, where Dynamo-style stores compare Merkle trees to find the ranges that differ.

  • The interview question

    “N = 3. Reads must see the latest write, and both reads and writes must survive one node down.” The answer is R = W = 2: 2 + 2 = 4 > 3, and each operation needs only 2 of 3. Then name the catches above: sloppy quorums and clock-based versions.

Quick check

1. What does R + W > N guarantee?

2. With N=5, which setting makes writes fast but reads potentially slow?

3. What happens when R + W ≤ N?

FAQ

What is a quorum in distributed systems?

The minimum number of replicas that must participate in an operation. With N copies, a write quorum W is how many must ack a write and a read quorum R how many a read consults. Tuning R and W trades off consistency, availability, and latency; R + W > N guarantees reads see the latest write.

Why does R + W > N give strong consistency?

Pigeonhole: the R read replicas and W write replicas can’t both fit in N without sharing one when R + W > N. That shared replica has the latest write, and a read returns the newest version it sees — so it returns that write. Below the threshold the sets can be disjoint and miss recent writes.

What are common R/W configurations?

W=N, R=1: fast reads, slow/durable writes. R=N, W=1: fast writes, slow reads. R=W=majority balances both while keeping R+W>N (a common default). Lower R+W (≤N) trades consistency for latency and availability — eventual consistency.

How do quorums relate to CAP and eventual consistency?

Quorum tuning exposes the CAP trade-off as a dial (Dynamo, Cassandra, Riak). R + W > N leans consistent (CP-like); relaxing it leans available and low-latency but allows stale reads (AP-like, eventual). Set per operation, one system can serve both consistency modes.

Keep going

Finished this one? 0 / 61 Labs done
NextVector Clocks: Who Caused What Next on the “Agreement between machines” ladder · rung 3 of 5

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs