Consistency models
hard 30 minA consistency model is the promise a replicated store makes about what a read may return.
See it run
The same write under five promises: what each reader may see, and who has to wait.
Operations
Linearizable
Write to a majority, read through a leader that checks with a majority.
Step 1 of 5
Write request
A client sends x = 1. All replicas hold 0. Replica 1 leads.
Step 1: Write request
def write(key, value): entry = leader.append(key, value) wait_until(stored_on(entry) >= majority) return "done"def read(key): leader.confirm_still_leader() # ask a majority return leader.get(key)- Waits for
- A majority
- May be stale
- Never
- Eventual
- waits for nobody
The check before the read stops an old leader from answering with an old value.
In the real world
The same consistency models doing real jobs. Pick one and step through it.
Four features, four promises.
Step 1 of 4
Coins
| feature | promise |
|---|---|
| coins | linearizable |
Buying with coins must not spend them twice: linearizable on the balance.
Step 1: Coins
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Calling a quorum linearizable
majorA write that reached one copy can be seen by one reader and missed by the next.
Wrongreturn newest(read_majority(key))Rightv = newest(read_majority(key))write_majority(key, v) # write back firstreturn vRejecting a newer client
majorA client's clock may include events from other keys. Compare it with what the replica has applied, not with one key's version.
Wrongif not client_clock <= entry.clock: raise StaleRightif not client_clock <= replica.applied: wait_or_redirect()Ordering by wall clock
criticalA clock that runs ahead makes an old write win, silently.
Wrongwinner = max(versions, key=time)Rightwinner = merge(versions) # or one leader gives the orderStrong everywhere
minorLike counts read from a majority cost a cross-region trip and bring nothing.
Wronglikes = read_majority("likes")Rightlikes = nearest.get("likes")
Interview tips
How to explain it
- The contract about what reads may return.
- Stronger means more waiting.
- Choose the weakest that users accept.
Memory trick
- Some Students Can Read Everything.
- Strong, Sequential, Causal,
- Read your writes, Eventual.
Go stronger for
- Locks and balances.
- Seats and stock.
- Unique names.
Go weaker for
- Counts and feeds.
- Catalogue pages.
- Anything that merges.