Vector Clocks and Conflict Resolution
You'll learn how distributed key-value stores use vector clocks to determine which version of data came first and resolve conflicts when multiple nodes update the same key simultaneously.
The Causality Problem
When multiple replicas accept writes independently (as you saw with eventual consistency), how do we know which update happened "before" another? Wall-clock timestamps aren't reliable—clocks drift, and network delays vary. We need a way to track causality: did update B happen because of update A, or did they happen concurrently without knowing about each other?
What Are Vector Clocks?
A vector clock is a list of counters, one per node in the system. Each time a node modifies data, it increments its own counter. When nodes exchange data, they merge vector clocks by taking the maximum value for each position.
Think of it like a scoreboard: if Node A's clock is [A:2, B:1, C:0] and Node B's is [A:1, B:3, C:1], we can see Node A has processed 2 events, Node B has processed 3, etc.
Detecting Conflicts
Vector clocks let us compare two versions:
- If all counters in version X are ≤ version Y, then X happened before Y (Y can safely replace X)
- If neither version's counters are all greater, they're concurrent conflicts that need resolution
Resolving Conflicts
When conflicts occur, the system can:
- Return both versions to the client (let the application merge them)
- Use "last write wins" with additional metadata
- Apply domain-specific merge logic (e.g., union of shopping cart items)
Key Takeaway: Vector clocks track causality across distributed replicas by maintaining per-node event counters, enabling systems to detect concurrent writes and delegate conflict resolution to applications or merge strategies rather than blindly overwriting data.