Concept
Causal Ordering with Vector Clocks
The power of vector clocks is that they allow precise causal reasoning. Given two versions of a value — say from two replicas — you can determine exactly one of four relationships.
The Comparison Algorithm
def compare(a, b):
a_le_b = all(a[n] <= b[n] for n in nodes)
b_le_a = all(b[n] <= a[n] for n in nodes)
if a_le_b and b_le_a:
return "EQUAL"
elif a_le_b:
return "BEFORE"
elif b_le_a:
return "AFTER"
else:
return "CONCURRENT"
Interpreting Results in Dynamo
| Result | Meaning | Action |
|---|---|---|
| BEFORE | A is an older version of B | Discard A, keep B |
| AFTER | A is a newer version of B | Discard B, keep A |
| CONCURRENT | Genuine conflict | Store both; surface to client |
| EQUAL | Duplicate write | Keep either |
Concurrent Writes in Dynamo
Concurrent writes happen when two clients write to the same key at roughly the same time via different coordinators. Neither write was aware of the other. Dynamo stores both versions and returns them both to the next reader. The application (or a merge function) then resolves the conflict.
Amazon's shopping cart, for example, merges conflicting carts by taking the union of items — you might see duplicate items, but you will never lose an item due to a conflict.
main.py
python1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
"""
Vector Clock Conflict Detection
Commands:
CLOCK <name> [n1:x,n2:y,...] - store a named clock
COMPARE <a> <b> - print BEFORE, AFTER, CONCURRENT, or EQUAL
"""
import sys
import re
def parse_clock(s: str) -> dict:
"""Parse '[n1:2,n2:1,n3:0]' into {'n1':2,'n2':1,'n3':0}."""
s = s.strip('[]')
clock = {}
for part in s.split(','):
if ':' in part:
node, val = part.split(':')
clock[node] = int(val)
return clock
def compare_clocks(a: dict, b: dict) -> str:
"""Return BEFORE, AFTER, CONCURRENT, or EQUAL."""
# TODO:
# 1. Check if a <= b in every component (a_le_b)
# 2. Check if b <= a in every component (b_le_a)
# 3. Return the appropriate relationship
pass
def main():
clocks = {}
for line in sys.stdin:
line = line.strip()
if not line:
continue
Sign in to run and submit code.