Sub topic 9
Tracking Causality with Vector Clocks
This chapter assumes you know how replication and preference lists work, and that you have seen read and write quorums. Replicas accept writes independently, so the store needs a way to tell whether one version of a value replaces another or whether the two were written concurrently.
Why timestamps are not enough
Wall clocks on different machines drift. If node A writes at 10:00:00.005 by its clock and node B writes at 10:00:00.002 by its clock, you cannot tell which write happened first. You also cannot tell whether B had seen A's value before it wrote.
What the store needs is causality: did write X influence write Y? A vector clock records this. It is a map from node id to a counter, attached to each version of a value.
How a vector clock works
Each replica that coordinates a write increments its own counter in the clock and stores the result with the value.
- A client reads a key and receives the value and its clock, for example
{"a": 1}. - The client writes a new value and sends that clock back as context.
- The coordinator increments its own entry, producing
{"a": 2}.
Two clocks compare in one of four ways:
- Equal: every counter matches.
- Before: every counter in the first is less than or equal to the second's. The second version descends from the first, so it can replace it.
- After: the reverse of before.
- Concurrent: each clock has some counter larger than the other's. Neither version saw the other.
def compare(a: dict, b: dict) -> str:
"""Compare two vector clocks mapping node id -> counter."""
keys = set(a) | set(b)
a_le_b = all(a.get(k, 0) <= b.get(k, 0) for k in keys)
b_le_a = all(b.get(k, 0) <= a.get(k, 0) for k in keys)
if a_le_b and b_le_a:
return "equal"
if a_le_b:
return "before"
if b_le_a:
return "after"
return "concurrent"
print(compare({"a": 1}, {"a": 2})) # before
print(compare({"a": 2, "b": 1}, {"a": 1, "b": 2})) # concurrentA missing entry counts as zero, which is why the code uses a.get(k, 0).
A worked example
Two clients update the same cart during a network partition.
Compare {"a": 2} with {"a": 1, "b": 1}. The first has a larger a. The second has a larger b. The result is concurrent, so the store must not silently drop either value.
What the store does with the result
- If one clock is before the other, discard the older version.
- If the clocks are concurrent, keep both as siblings and return them on the next read. The next chapter covers how to merge them.
Common errors and costs
- Treating concurrent as ordered. Picking the larger sum of counters, or the latest wall-clock time, loses a write without warning.
- Dropping the context. If a client writes without sending back the clock from its read, the coordinator cannot tell what the client saw. The write often becomes a sibling, even when the client meant to overwrite.
- Unbounded growth. Each coordinator adds an entry. Over time clocks grow. Some stores truncate the oldest entries when the clock passes a size limit. This is safe for storage but can make old versions look concurrent, creating false siblings.
- Sibling explosion. Clients that repeatedly write without context create many siblings that nobody merges.
Exercise
Compare {"a": 3, "b": 1} with {"a": 2, "b": 2}. What is the result, and what should the store do?
Show answer
The result is concurrent. The first clock has the larger a (3 vs 2) and the second has the larger b (2 vs 1). Neither version descends from the other, so the store keeps both as siblings and returns them on the next read.
Exercise
A client writes a key without sending the clock from its earlier read. Why might this create a sibling?
Show answer
The coordinator has no record of which version the client saw. It cannot show that the new write descends from the stored version, so the two clocks may be concurrent.