AlgoViz

Versioning data and tunable quorums

medium 24 min

When several machines accept changes to the same key, a store needs versions to tell newer from conflicting.

See it run

What each node holds, what its version says, and what happens when two versions meet.

Operations

Concurrent writes

Two clients change the same key at two nodes before the nodes have talked.

Step 1 of 5

Initial state

Our systemClient 1Client 2Node A[A:1]Node B[A:1]

Node A

A

Node B

A

Both nodes hold the value A with version [A:1].

Step 1: Initial state

def compare(v, w):    before = all(v[n] <= w[n] for n in nodes)    after  = all(w[n] <= v[n] for n in nodes)    if before and after: return "same"    if before: return "v is older"    if after:  return "w is older"    return "concurrent"
Loses data
No, both found
Client work
Merge
Without versions
one is lost silently

Concurrent does not mean at the same second. It means that neither writer had seen the other.

In the real world

The same versioning data and tunable quorums doing real jobs. Pick one and step through it.

Both open the same product, and both save. A version number catches it.

Step 1 of 4

Both open it

Our systemreadAdmin 1Admin 2Databaseversion 4Audit logno entries

Database

price 10

Audit log

-

The price is 10, version 4. Both admins see that.

Step 1: Both open it

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Ordering by wall clock

    critical

    A clock that runs ahead beats writes that came later. Nobody gets an error.

    Wrong
    keep = max(versions, key=lambda v: v.time)
    Right
    if compare(v, w) == "concurrent":    keep = [v, w]
  • Writing without the context

    major

    The store cannot know that you saw the old value. It must treat your write as concurrent, and siblings pile up.

    Wrong
    put(key, value)
    Right
    value, context = get(key)put(key, change(value), context)
  • Merging without tombstones

    major

    A union cannot tell not-yet-added from removed.

    Wrong
    cart = a | b
    Right
    cart = (a.adds | b.adds) - (a.removed | b.removed)
  • Forgetting to raise the counter after a merge

    minor

    The merged value would share a version with another value. A new write needs a new version.

    Wrong
    clock = merge(c1, c2)
    Right
    clock = merge(c1, c2)clock[me] += 1

Interview tips

  • How to explain it

    • Versions say what a write was based on.
    • Newer replaces older; concurrent needs a rule.
    • Three rules: keep one, keep both, merge.
  • VCRC

    • Vector clocks track causes.
    • Concurrent writes are detected.
    • Resolve, and configure N, R and W.
  • Easy cases

    • One deciding store: a version number.
    • Adds only: counters and sets that merge.
    • Written once: last write wins is fine.
  • Be careful with

    • Deletes in merged data.
    • Clocks across machines.
    • Money: use a transaction.