Versioning data and tunable quorums
medium 24 minWhen 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
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
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
criticalA clock that runs ahead beats writes that came later. Nobody gets an error.
Wrongkeep = max(versions, key=lambda v: v.time)Rightif compare(v, w) == "concurrent": keep = [v, w]Writing without the context
majorThe store cannot know that you saw the old value. It must treat your write as concurrent, and siblings pile up.
Wrongput(key, value)Rightvalue, context = get(key)put(key, change(value), context)Merging without tombstones
majorA union cannot tell not-yet-added from removed.
Wrongcart = a | bRightcart = (a.adds | b.adds) - (a.removed | b.removed)Forgetting to raise the counter after a merge
minorThe merged value would share a version with another value. A new write needs a new version.
Wrongclock = merge(c1, c2)Rightclock = 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.