← Lessons

quiz vs the machine

Platinum1880

Concurrency

The Vector Clock Comparison

Tracking causality across nodes to tell ordered events from concurrent ones.

6 min read · advanced · beat Platinum to climb

Knowing what happened before what

In a distributed system there is no single clock to order events. A vector clock gives each event a vector of counters, one per node, that captures causal history. Comparing two vectors tells you whether one event happened before another or whether they are concurrent.

How it updates

  • On a local event, a node increments its own entry.
  • When sending a message, it attaches its current vector.
  • On receiving, the node takes the element wise maximum of its vector and the message vector, then increments its own entry.

This way a vector records every event known to have causally preceded the current one.

Comparing two vectors

Given vectors A and B, compare them element by element.

  • If every entry of A is less than or equal to B and at least one is strictly less, then A happened before B.
  • If neither dominates the other, the events are concurrent, meaning neither could have caused the other.

Concurrency is the important signal. Concurrent updates to the same key are a genuine conflict the application must resolve.

Key idea

A vector clock records per node counters so comparing two vectors reveals whether events are causally ordered or concurrent, and concurrent updates flag conflicts the application must resolve.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a vector clock capture?

2. How do two vectors indicate concurrent events?

3. What does a node do on receiving a message?