Course contentsShow
← Computer Science
Lesson 2702 of 2,87260. Distributed Data and CommunicationFree lesson

Vector Clocks for Causality Tracking

Implementing vector clocks to detect causal relationships and concurrent updates across distributed replicas.

Vector Clocks for Causality Tracking

What you'll learn: How vector clocks enable distributed replicas to determine which updates caused others and which happened concurrently, making conflict resolution smarter than simple timestamps.

The Core Challenge

In eventual consistency systems, multiple replicas accept writes independently. When they sync, we need to know: Did update A happen before B (causally related), or did they happen concurrently (potential conflict)? Unlike Last-Write-Wins which just picks the "latest" timestamp, vector clocks preserve the causal history of each update.

How Vector Clocks Work for Tracking

Each replica maintains a vector (array) of logical clocks—one counter per replica in the system. When replica i performs a write:

  1. It increments its own counter: VC[i]++
  2. It attaches the entire vector to that write

When comparing two updates with vectors VA and VB:

  • A caused B if every element VA[i] ≤ VB[i] and at least one is strictly less
  • B caused A if every element VB[i] ≤ VA[i] and at least one is strictly less
  • Concurrent if neither condition holds (some counters are higher in VA, others in VB)

Detecting Causal Relationships

Imagine replica 1 writes {name: "Alice"} with vector [1,0,0]. Replica 2 reads this, then writes {age: 30} with vector [1,1,0]. The system can see [1,0,0] < [1,1,0]—the second write causally depends on the first.

But if replica 1 writes [2,0,0] and replica 3 writes [0,0,1] simultaneously, neither vector dominates—they're concurrent conflicts requiring application-level resolution.

Key Takeaway:
Vector clocks capture the happens-before relationships across replicas, allowing systems to distinguish causal updates from true conflicts, enabling smarter merge strategies than timestamp-based approaches.