← Lessons

quiz vs the machine

Platinum1800

System Design

Vector Clocks

Telling whether two updates are ordered or genuinely concurrent.

6 min read · advanced · beat Platinum to climb

Why wall clocks fail

In a distributed system you cannot trust wall clock time to order events, because clocks drift and messages cross at odd moments. A vector clock captures causality instead: which event truly happened before which.

How it works

Each node keeps a vector with one counter per node. The rules are simple:

  • Before a local event, a node increments its own counter.
  • When sending a message it attaches its whole vector.
  • On receiving, a node takes the element wise maximum of its vector and the incoming one, then increments its own counter.

Comparing two vectors

Given two vectors X and Y:

  • If every entry of X is less than or equal to Y and at least one is strictly less, then X happened before Y.
  • If neither dominates the other, the events are concurrent, which usually signals a conflict.

Systems use this to detect conflicting writes that need merging rather than blindly picking the latest. The cost is that the vector grows with the number of nodes, so large clusters prune or cap entries.

Key idea

Vector clocks track causality so a system can tell ordered updates apart from genuinely concurrent ones.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a vector clock let you detect that a wall clock cannot reliably?

2. On receiving a message, how does a node update its vector?

3. What does it mean when neither of two vectors dominates the other?