Vector Clocks and Causality
You'll learn: How vector clocks track the causal order of events across distributed replicas to detect conflicts in eventual consistency systems.
The Problem: Detecting Causality
In eventual consistency (lessons 531–538), replicas accept writes independently and sync later. But how do we know if two updates happened concurrently (true conflict) or one causally followed the other (no conflict)? Simple timestamps fail because clocks drift across servers.
Vector clocks solve this by giving each replica its own logical counter.
How Vector Clocks Work
A vector clock is a list of counters, one per replica:
Replica A: [A:1, B:0, C:0]
Replica B: [A:0, B:1, C:0]
When Replica A writes, it increments its own counter: [A:2, B:0, C:0].
When replicas sync, they merge vector clocks by taking the maximum of each position. If all counters in vector clock V1 are ≤ V2, then V1 happened-before V2 (causal order). If neither dominates, they're concurrent — a conflict requiring resolution.
Detecting Conflicts
- Event
[A:2, B:1, C:0]happened-before[A:3, B:2, C:1](no conflict) - Events
[A:2, B:1, C:0]and[A:1, B:2, C:0]are concurrent (conflict!)
Vector clocks tell you when to apply conflict resolution (like CRDTs from lesson 538 or last-write-wins), not how to resolve it.
Why It Matters
Without causality tracking, you can't distinguish between "Alice updated after seeing Bob's change" vs. "Alice and Bob updated simultaneously." Vector clocks preserve the happens-before relationship across distributed writes.
Key Takeaway:
Vector clocks use per-replica counters to track causality, enabling systems to detect concurrent updates and apply appropriate conflict resolution in eventual consistency models.