Sub topic 9
Ordering events: happens-before and vector clocks
This chapter assumes you know what replication is, have read a simple Python function, and understand that machines in a network do not share a clock. Consistency models such as causal consistency depend on knowing which events are ordered and which are not. Logical clocks give you that.
Why wall clocks are not enough
Each machine's physical clock drifts. Two timestamps from different machines cannot reliably tell you which event came first. Distributed systems therefore track order by the messages that flow between nodes, not by the time of day.
Happens-before
Leslie Lamport defined the happens-before relation, written a → b. It holds in three cases:
aandboccur on the same node, andacomes first.ais the sending of a message andbis the receipt of that same message.- There is some
cwitha → candc → b(transitivity).
If neither a → b nor b → a holds, the events are concurrent. Concurrent does not mean simultaneous. It means neither event could have influenced the other.
Here a1 → b1, because a message links them. Event c1 is concurrent with both, since no message connects it to them.
Lamport clocks and their limit
A Lamport clock is a single counter per node. A node increments it on each event. When it receives a message, it sets its counter to the larger of its own value and the message's value, then adds one. This guarantees that if a → b, then L(a) < L(b).
The reverse does not hold. If L(a) < L(b), you cannot conclude that a → b. The two events may be concurrent. One number cannot separate "ordered" from "concurrent".
Vector clocks
A vector clock keeps one counter per node. Each node increments its own entry on a local event. On receiving a message, it takes the per-entry maximum of the two vectors, then increments its own entry. Comparing two vectors then tells you the exact relationship.
ahappened beforebif every counter inais less than or equal to the matching counter inb, and the clocks are not equal.- If neither vector dominates the other, the events are concurrent.
def compare(a, b):
"""Compare two vector clocks given as dicts of node -> counter."""
keys = set(a) | set(b)
le = all(a.get(k, 0) <= b.get(k, 0) for k in keys)
ge = all(a.get(k, 0) >= b.get(k, 0) for k in keys)
if le and ge:
return "equal"
if le:
return "before"
if ge:
return "after"
return "concurrent"
print(compare({"A": 1}, {"A": 2})) # before
print(compare({"A": 2, "B": 0}, {"A": 1, "B": 1})) # concurrentIn the second call, A has seen two events on node A but none on B. The other clock has seen one on each. Neither knows everything the other knows, so the writes are concurrent.
Common errors
- Reading Lamport order as causal order. A smaller Lamport timestamp does not prove one event caused the other.
- Treating concurrent as a tie to break arbitrarily. A store that sees concurrent writes must resolve them, for example by keeping both versions or merging them. Picking one silently can lose data.
- Ignoring vector size. A vector has one entry per node (or per client), so it grows with the number of participants. Real systems prune entries or use variants.
Exercise
Node A holds the clock {"A": 3, "B": 1} and node B holds {"A": 2, "B": 2}. Are the events ordered or concurrent? Explain.
Show answer
Concurrent. A's clock is larger on entry A (3 against 2), and B's clock is larger on entry B (2 against 1). Neither dominates the other, so neither event could have seen the other.