AI Hints
Home›System Design Tutorial›Quorums and Patterns
System Design · Lesson 112 of 120

Key-Value Store: Replication

Quorum reads and writes.

⏱ 2 min read▶ Video at 2:16:04🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

Each key is stored on three nodes. When we write, must all three confirm? When we read, must we ask all three? A quorum lets us choose.

Key-Value Store: Replication
From the video: Key-Value Store: Replication
  • Three copies of each key
  • Wait for all?
  • A quorum

N, W and R

Three numbers describe a quorum. N is the number of copies, here three. W is how many must confirm a write. R is how many we read from. If W plus R is greater than N, every read overlaps with the latest write on at least one node, so reads see the newest value.

keyvalue
Ncopies, e.g. 3
Wmust confirm a write
Rnodes we read from
W + R > Nreads see the latest

Why the overlap works

Let's check it. Three replicas. A write with W equals two updates only two of them, leaving one stale. A read with R equals two asks two replicas, and keeps the newest version. Run it. Whichever pair we read from, at least one is fresh, so we always get version two.

quorum.pypython
from itertools import combinations

replicas = {"n1": (1, "old"), "n2": (1, "old"), "n3": (1, "old")}
for node in ["n1", "n2"]:              # W = 2
    replicas[node] = (2, "new")

for pair in combinations(replicas, 2):  # R = 2
    newest = max(replicas[n] for n in pair)
    print(pair, "->", newest)
Output
('n1', 'n2') -> (2, 'new')
('n1', 'n3') -> (2, 'new')
('n2', 'n3') -> (2, 'new')

Tuning the quorum

Different settings suit different needs. W equals three and R equals one makes reads fast, but writes fail if any node is down. W equals one and R equals three does the opposite. W equals two and R equals two is the common balance. And W plus R less than or equal to N is fastest, but reads may be stale.

SettingBehaviour
W=3, R=1fast reads, fragile writes
W=1, R=3fast writes, slow reads
W=2, R=2balanced, consistent
W+R ≤ Nfastest, may be stale

Summary

Quorums let a key value store tune consistency. When W plus R is greater than N, reads always overlap the latest write. Next, the patterns that kept coming back in every case study.

Key takeaways
  • N copies
  • W + R > N
  • Tune per use
Next up: Case Study Patterns — what kept coming back