AlgoViz

Consistency models

hard 30 min

A 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

Our systemx = 1ClientReplica 1x=0, leaderReplica 2x=0Replica 3x=0

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

featurepromise
coinslinearizable

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

    major

    A write that reached one copy can be seen by one reader and missed by the next.

    Wrong
    return newest(read_majority(key))
    Right
    v = newest(read_majority(key))write_majority(key, v)     # write back firstreturn v
  • Rejecting a newer client

    major

    A client's clock may include events from other keys. Compare it with what the replica has applied, not with one key's version.

    Wrong
    if not client_clock <= entry.clock:    raise Stale
    Right
    if not client_clock <= replica.applied:    wait_or_redirect()
  • Ordering by wall clock

    critical

    A clock that runs ahead makes an old write win, silently.

    Wrong
    winner = max(versions, key=time)
    Right
    winner = merge(versions)    # or one leader gives the order
  • Strong everywhere

    minor

    Like counts read from a majority cost a cross-region trip and bring nothing.

    Wrong
    likes = read_majority("likes")
    Right
    likes = 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.