Vector clock
The Fundamental Challenge
In a distributed system like Amazon Dynamo (or Cassandra/Riak), we want High Availability. This means the system must accept writes even if the network is broken and nodes cannot talk to each other.
However, if two different nodes accept writes for the same shopping cart at the same time (while disconnected), we end up with two different versions of the truth.
Why Timestamps Fail
You might think: “Just keep the one with the latest timestamp!” (Last Write Wins).
The Trap: Server clocks are unreliable. Server A might think it is 12:00:01 while Server B thinks it is 12:00:00.
The Consequence: If you use timestamps, a customer might add an item to their cart, and the system might silently delete it because a “later” timestamp from a lagging server overwrote it.
Example of Timestamp Failure
Time: 12:00:00 - User adds "Laptop" to cart on Server A (clock shows 12:00:01)
Time: 12:00:05 - User adds "Mouse" to cart on Server B (clock shows 11:59:59 - clock drift!)Last Write Wins uses timestamps:
- Server A timestamp: 12:00:01
- Server B timestamp: 11:59:59
- System keeps: "Laptop" (discards "Mouse")
- Result: User's mouse disappeared!The Solution: Vector Clocks
Instead of using wall-clock time (“It is 12:00 PM”), we use Logical Time (“This is the 3rd update Node A has seen”).
A Vector Clock is simply a list of counters that tracks history.
- Format:
[NodeA: 1, NodeB: 2] - Meaning: “This version of the data is the result of 1 update on Node A and 2 updates on Node B.”
2. Vector Clock Components
What is a Vector Clock?
A vector clock is a data structure that tracks causality:
Vector Clock = { node_id → counter }Example: [A:3, B:2, C:1]
- Node A has performed 3 operations
- Node B has performed 2 operations
- Node C has performed 1 operationCore Concepts
1. Causality
- Purpose: Track which events happened before others
- Mechanism: Each node increments its own counter on every write
- Example: If version V1 has
[A:1]and V2 has[A:2], then V1 happened before V2
2. Concurrency
- Purpose: Detect when two updates happened independently
- Mechanism: Compare all counters between two vector clocks
- Example:
[A:1, B:1]and[A:1, C:1]are concurrent (neither dominates)
3. Conflict Detection
- Purpose: Identify when manual reconciliation is needed
- Result: When two versions are concurrent, both are kept as “siblings”
- Resolution: Application layer decides how to merge
Operations
Increment
When a node performs a write:
1. Take current vector clock
2. Increment own counter
3. Attach new vector clock to dataCompare
To determine relationship between two vector clocks:
Clock A vs Clock B:If all counters in A ≤ B: A is ancestor (B is newer)
If all counters in A ≥ B: A is descendant (A is newer)
Otherwise: Concurrent (conflict!)🎯 Scenario 1: The Happy Path (Sequential Updates)
A user is shopping for Black Friday. The network is healthy. All requests are routed to Node A (the Coordinator).
Steps
- Write 1: User creates cart and adds “Kindle”
- Node A handles it
- Creates first version with Vector Clock:
[A:1]
2. Write 2: User adds “Echo Dot”
- Node A handles it
- Sees old clock
[A:1], increments own counter to 2 - New Clock:
[A:2]
3. Check: Since 2 > 1, system knows Version 2 is child of Version 1
- No conflict
Key Takeaways
- Sequential updates on same node simply increment that node’s counter
- Newer versions always have higher counters for the updating node
- Easy to determine causality: all counters increase monotonically
- No conflicts occur in sequential execution
🔀 Scenario 2: The Fork (Concurrent Updates)
A network partition occurs! Node B and Node C are isolated from each other, but both are still running. The user (or their phone app) talks to both.
Steps
- Base State: Both nodes start with last known cart
- Cart:
{Kindle, Echo Dot} - Clock:
[A:2]
2. Branch 1 (The Phone): User on phone adds “Headphones”
- Request hits Node B
- Node B takes
[A:2], adds itself[B:1] - New Clock:
[A:2, B:1] - Cart:
{Kindle, Echo Dot, Headphones}
3. Branch 2 (The Laptop): User on laptop adds “Charger”
- Request hits Node C
- Node C takes
[A:2], adds itself[C:1] - New Clock:
[A:2, C:1] - Cart:
{Kindle, Echo Dot, Charger}
4. The Conflict: System now has two valid “latest” versions
- Version X:
[A:2, B:1, C:0](implicit zero for C) - Version Y:
[A:2, B:0, C:1](implicit zero for B)
Key Takeaways
- Network partitions cause concurrent writes on different nodes
- Each node increments its own counter independently
- Neither version dominates the other (B:1 > B:0 but C:0 < C:1)
- System must keep both versions as siblings
- Conflicts are detected, not resolved automatically
🔗 Scenario 3: The Reconciliation (The Merge)
The network heals. The user refreshes their cart. The system reads the data and finds two conflicting versions (siblings).
Steps
- Read: Node A reads database and sees both objects
- Compare Clocks:
- Clock 1:
[A:2, B:1, C:0](implicit zero for C) - Clock 2:
[A:2, B:0, C:1](implicit zero for B)
3. Logic: Does Clock 1 beat Clock 2?
- No (B:1 > B:0, but C:0 < C:1)
- Result: They are Concurrent
4. Return to App: Database returns both shopping carts to application
Get Saahid’s stories in your inbox
Join Medium for free to get updates from this writer.
5. Semantic Reconciliation: Application logic (client side) merges
- Looks at items: “User wanted Headphones AND Charger”
- Merges sets:
{Kindle, Echo Dot, Headphones, Charger}
6. Write Back: Application writes merged object to Node A
- Node A creates new clock dominating both previous ones
- Final Clock:
[A:3, B:1, C:1]
Key Takeaways
- System detects conflicts automatically using vector clocks
- Application receives all conflicting versions (siblings)
- Application logic performs semantic merge based on domain knowledge
- Merged version has vector clock that dominates all siblings
- Final write creates new version with incremented coordinator counter
📋 The Rules of Comparison
How does the computer strictly decide if there is a conflict? It compares the counters for every node ID.
Comparison Rules
RelationshipRuleMeaningActionDescendantAll counters in A ≤ BA is an older version of BOverwrite: Safely discard A, keep BAncestorAll counters in A ≥ BA is a newer version of BOverwrite: Safely discard B, keep AConflict (Sibling)A has some counters > B, BUT B has some counters > AThey branched off separatelyKeep Both: Return both to app
Example Checks
Example 1: Clear Descendant
Clock A: [Node1: 1, Node2: 1]
Clock B: [Node1: 1, Node2: 2]Check: 1 ≤ 1 and 1 ≤ 2
Result: B is newer (A is ancestor)
Action: Discard A, keep BExample 2: Conflict
Clock X: [Node1: 1, Node2: 2]
Clock Y: [Node1: 2, Node2: 1]Check:
- Node1: 1 < 2 (favors Y)
- Node2: 2 > 1 (favors X)Result: Conflict (concurrent updates)
Action: Keep both X and Y as siblings
Example 3: Clear Ancestor
Clock A: [Node1: 3, Node2: 2, Node3: 1]
Clock B: [Node1: 2, Node2: 1, Node3: 1]Check: 3 ≥ 2 and 2 ≥ 1 and 1 ≥ 1
Result: A is newer (B is ancestor)
Action: Discard B, keep A🌍 Real World Use Cases
A. Shopping Carts (Amazon Dynamo)
Use Case: User adds items to cart from multiple devices simultaneously
Why Vector Clocks:
- High availability is critical (can’t block shopping during network issues)
- Cart merging is simple: union of items from all versions
- False positives (showing duplicate items) are acceptable
Implementation:
- Each cart update increments the handling node’s counter
- On read, merge all sibling carts
- User sees all items they added from any device
B. Distributed Databases (Riak, Cassandra)
Riak: Uses vector clocks for conflict detection on every key
Implementation:
- Each PUT operation updates vector clock
- GET returns all siblings if conflicts exist
- Application chooses resolution strategy:
- Last-write-wins (simple but loses data)
- Custom merge logic (e.g., CRDT-like merging)
Cassandra: Uses timestamps (Last-Write-Wins) by default, but allows application-level conflict resolution
C. Collaborative Editing (Conceptual)
Problem: Multiple users editing same document offline
Vector Clock Application:
- Track which operations each user has seen
- Detect concurrent edits
- Present conflicts to users for manual resolution
Note: Modern systems use CRDTs (Conflict-free Replicated Data Types) which automatically merge without conflicts
D. Distributed File Systems (Ceph, GlusterFS)
Use Case: File metadata synchronization across replicas
Why Vector Clocks:
- Detect when file modifications happened concurrently
- Prevent accidental overwrites
- Allow administrators to resolve conflicts manually








