Fundamentals

Vector Clocks

Understanding vector clocks for tracking causality in distributed systems.

3 min readConcepts · System Design

What are Vector Clocks?

Vector Clocks are a mechanism for tracking causality and detecting concurrent events in distributed systems. Each node maintains a vector of logical timestamps — one entry per node in the system.


The Problem with Physical Clocks

Physical clocks drift and can't reliably determine event ordering across nodes.


How Vector Clocks Work

Each node maintains a vector: [A:count, B:count, C:count]

Rules

  1. Before sending: Increment own entry
  2. On receive: Merge vectors (take max of each entry), then increment own entry

Example


Comparing Events

Given vectors V1 and V2:

RelationshipConditionMeaning
V1 < V2All entries in V1 ≤ V2, at least one strictly lessV1 happened before V2
V1 > V2All entries in V1 ≥ V2, at least one strictly greaterV1 happened after V2
V1 ‖ V2Neither ≤ nor ≥Concurrent (conflict!)

Example

Event X: [A:2, B:1, C:0]
Event Y: [A:1, B:2, C:0]

X[A]=2 > Y[A]=1, but X[B]=1 < Y[B]=2
→ Neither dominates → Concurrent!
→ Application must resolve conflict

Conflict Detection

Resolution Strategies

StrategyHowUsed By
Last-Write-WinsUse wall clock as tiebreakerCassandra
Keep bothReturn siblings to clientRiak
Application logicDomain-specific mergeShopping carts
CRDTsAuto-mergeable data typesRedis, Riak

Vector Clocks vs Lamport Clocks

AspectLamport ClockVector Clock
StructureSingle counterVector of counters
Detect causalityIf L(a) < L(b), maybe a→bIf V(a) < V(b), definitely a→b
Detect concurrencyCannotCan
SpaceO(1)O(N) per event

Lamport clocks tell you "a might have happened before b." Vector clocks tell you "a definitely happened before b" or "a and b are concurrent."


Real-World Usage

SystemHow Used
DynamoDBDetects conflicting writes
RiakSibling detection and resolution
VoldemortConflict detection
CockroachDBHybrid logical clocks (related)

Limitations

IssueMitigation
Vector grows with nodesUse dotted version vectors
Space overhead O(N)Prune old entries
Complex merge logicUse CRDTs instead
Clock comparison costOptimize for common case

Interview Tips

  • Explain why physical clocks fail in distributed systems
  • Know the increment and merge rules
  • Demonstrate how to detect concurrent events
  • Compare with Lamport clocks (vector clocks detect concurrency)
  • Mention use in DynamoDB and Riak

Finished reading?

Mark it done to track your progress in Concepts.