Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Systems · Distributed

Who Caused What.

In a distributed system there is no single clock everyone agrees on, so “which event happened first?” is genuinely ambiguous. Vector clocks give a precise, decentralized answer for the events that do have a cause-and-effect relationship. Each of the n processes keeps a length-n vector counting events. Three rules: on a local event, bump your own entry; when you send a message, attach your vector; when you receive one, merge by taking the element-wise max, then bump your own entry. Compare two vectors afterward: if one is ≤ the other everywhere, the first “happened-before” the second; if neither dominates, the events are truly concurrent. Step through a scenario and watch causality light up.

happened-before without a global clock · merge by element-wise max · detect concurrency
vector clock

A per-process length-n array counting events each process has observed.

local event

A process does something on its own → it increments its own entry.

send / receive

Send attaches your vector; receive merges by element-wise max, then bumps self.

happened-before

A → B iff A’s vector ≤ B’s everywhere; if neither ≤, they are concurrent.

vectorclock.js — max on receive, bump on event
Ready
Three processes P0, P1, P2 on their own timelines, no shared clock. Each event shows the process’s vector clock. Messages (arrows) carry a vector; receiving merges it by max. Step through the events.
0/7
events
–
last kind
–
concurrent pairs

How it works

The element-wise max on receive is the whole trick. Your vector entry V[j] means “the number of events at process j that I know about.” When you receive a message, the sender knew about some events you didn’t, so you adopt the larger count for every process — max — folding their knowledge into yours; then you bump your own entry for the receive event itself. Now the comparison is exact: A → B (A happened-before B, i.e. A could have caused B) precisely when every entry of A’s vector is ≤ the corresponding entry of B’s. If some entry of A is bigger and some entry of B is bigger, neither could have known about the other, so they are concurrent — no causal order exists. This is strictly more informative than a single Lamport counter, which can order concurrent events but can’t tell you they were concurrent.

1

Local event: bump your own entry

When a process does something locally, it increments only its own position in its vector. Nothing else changes.

2

Send: attach your vector

Sending a message counts as a local event (bump your entry), and the message carries a copy of your updated vector to the receiver.

3

Receive: merge by max, then bump

On receive, set each entry to the max of your value and the message’s value (adopting everything the sender knew), then bump your own entry for the receive.

✓

Compare to order events

A happened-before B iff A’s vector ≤ B’s in every entry. If neither vector dominates the other, the two events are concurrent — there is no causal relationship between them.

Detects
happened-before
Detects
concurrency
Merge rule
element-wise max
vs Lamport
more precise

The code

# vector clock at process p (of n processes) V = [0] * n def local_event(): V[p] += 1 # bump own entry def send(): V[p] += 1 return list(V) # attach vector to message def receive(msg_V): for i in range(n): V[i] = max(V[i], msg_V[i]) # adopt sender's knowledge V[p] += 1 # A -> B (caused) iff all(A[i] <= B[i]); else concurrent

The picture: the lab’s seven events, drawn

vector after each event · arrows carry the sender's vector P0P1P2 1 local[1,0,0] 2 send[2,0,0] 3 recv[2,1,0] 4 local[0,0,1] 5 send[2,2,0] 6 recv[2,2,2] 7 local[3,0,0] 1 → 6 through two messages: [1,0,0] ≤ [2,2,2] 3 ∥ 4: neither vector ≤ the other, yet Lamport says 3 vs 1
The exact run the lab steps through. Events 3 and 4 are highlighted: P1 has heard from P0, P2 has heard from nobody, so neither knows about the other. A Lamport counter stamps them 3 and 1 and would let you think 4 came first; the vectors say they are concurrent. Read any box as a sentence: [2,2,2] at event 6 means “P2 knows of two events at P0, two at P1 and two of its own”. Follow the arrows backwards from a box and you reach exactly the events it counts.

Worked example: the lab run, step by step

The same seven events, with each rule applied in order and a Lamport counter alongside for contrast. Lamport uses one integer: +1 on a local event or send, max(own, message) + 1 on receive.

EventWhat happensRule appliedVectorLamport
1P0 localbump P0[1,0,0]1
2P0 sends to P1bump P0, attach [2,0,0][2,0,0]2
3P1 receives 2max([0,0,0], [2,0,0]) = [2,0,0], bump P1[2,1,0]max(0, 2) + 1 = 3
4P2 localbump P2[0,0,1]1
5P1 sends to P2bump P1, attach [2,2,0][2,2,0]4
6P2 receives 5max([0,0,1], [2,2,0]) = [2,2,1], bump P2[2,2,2]max(1, 4) + 1 = 5
7P0 localbump P0[3,0,0]3

Now read the answers off the vectors. Event 1 caused event 6: [1,0,0] ≤ [2,2,2] in every entry, and the chain is the two arrows. Events 3 and 4 are concurrent: 3 is bigger in entries 0 and 1, 4 is bigger in entry 2. Lamport stamps them 3 and 1, which looks like an order and is not one. Event 7 is concurrent with 6 too ([3,0,0] against [2,2,2]), though Lamport gives 3 and 5. Seven events make 21 pairs; 8 of them are concurrent: 1∥4, 2∥4, 3∥4, 3∥7, 4∥5, 4∥7, 5∥7, 6∥7. That is the 8 the lab reports when the run ends. Notice where the knowledge flowed: P2 learned about P0’s first two events without ever hearing from P0, because P1 carried them inside its own vector. Event 7 never travelled anywhere, so nobody else counts it.

Cost and trade-offs

With n processes, every timestamp is n integers. At 8 bytes an entry that is 24 bytes in the lab and 8,000 bytes with 1,000 processes, on every message.

CaseTimeSpaceWhy
Local eventO(1)O(1)One increment of your own entry.
SendO(n)O(n) per messageBump, then copy the whole vector into the message. Message size grows with n.
ReceiveO(n)O(1) extraOne max per entry, then one bump.
Compare two eventsO(n)O(1)Check ≤ both ways. Four outcomes: before, after, equal, concurrent.
All concurrent pairs of m eventsO(m² n)O(m n)Every pair compared. The lab does 21 pairs × 3 entries.
Version vector, per replicaO(r)O(r)Counts writes per replica r, not every event per client, so its size follows the server set, not the user count.
Lamport clock, for contrastO(1)O(1)One integer. Cause implies a smaller stamp, but a smaller stamp proves nothing and concurrency is invisible.

The size is the price of exactness, not an implementation accident: Charron-Bost proved that no vector of fewer than n integers can capture causality exactly for every run. Real systems shrink n instead, by counting per replica (a version vector) rather than per client or per event. The trade against Lamport is plain: pay n integers per message and you can detect conflicts; pay one integer and you can only order events, never prove that two of them were independent.

Common mistakes

Each of these produces vectors that look plausible and give wrong answers, which is why they survive code review. All five can be checked against the run above.

  • Skipping the bump after the merge

    If P1 merges [2,0,0] and stops, its receive carries [2,0,0], the same vector as the send. Two different events now compare as equal. The receive is an event and needs its own tick.

  • Attaching the vector before bumping on send

    Ship [1,0,0] instead of [2,0,0] and P1 ends at [1,1,0]. Compared with the send’s [2,0,0], neither dominates, so the send and the receive it caused look concurrent. Bump first, then copy.

  • Testing with strict less-than in every entry

    The rule is ≤ everywhere and different somewhere. Strict < everywhere rejects [2,0,0] → [2,1,0], because entries 0 and 2 are equal, and calls a real cause concurrent. Most pairs share an entry, so this fails constantly.

  • Treating concurrent as equal, or as an order

    Concurrent is a third answer, not a tie. A sort comparator that returns 0 for it breaks: in the lab 1∥4 and 4∥3, yet 1 → 3. And picking a winner by stamp or wall time silently drops one of two real writes. Keep both and merge them.

  • Pruning entries to cap the size

    Dropping an entry loses information either way. [1,0,5] and [2,0,3] are concurrent; drop the last entry and [1,0] ≤ [2,0], so the second looks like a descendant and overwrites the first. That is a lost update, not just an extra conflict.

Where this shows up in production

The mechanism rarely appears under its textbook name. Look for anything that must tell “this replaced that” apart from “these two happened independently”.

  • Dynamo-style key-value stores

    Amazon’s Dynamo paper and Riak keep a version vector with each value. On read, versions that are concurrent come back together as siblings and the application merges them. Dynamo’s shopping cart merged by union, which is why a deleted item could reappear.

  • Data race detectors

    Happens-before race detectors such as ThreadSanitizer keep a vector clock per thread. Releasing a lock plays the role of send and acquiring it plays receive. Two accesses to the same memory, at least one a write, with concurrent clocks: that is a race.

  • Causal broadcast and CRDTs

    Here the vectors count messages sent. A process holds back a message from process j until Vm[j] = V[j] + 1 and Vm[k] ≤ V[k] elsewhere, so nobody sees a reply before its question. The G-Counter in the CRDT Merge lab merges by the same element-wise max.

  • The interview angle

    “Why not timestamps?” Clocks drift, so last-write-wins on wall time can drop a concurrent write without a trace. Expect follow-ups on Lamport versus vector (ordering versus detection), how size grows with membership, and what to do with a detected conflict: siblings, a CRDT merge, or last-write-wins chosen on purpose.

Quick check

1. What does a process do to its vector on a local event?

2. How does a process update its vector when it receives a message?

3. Two events have vectors [2,1,0] and [1,0,2]. What is their relationship?

FAQ

What is a vector clock?

A mechanism for capturing the causal (happened-before) ordering of events with no shared clock. Each of n processes keeps a vector of n counters, bumps its own on each event, attaches it to messages, and merges incoming vectors by element-wise max. Comparing vectors reveals causal precedence or concurrency.

How do vector clocks differ from Lamport clocks?

A Lamport clock is one counter: A→B implies timestamp(A)

What are vector clocks used for?

Detecting conflicting concurrent writes in eventually-consistent stores (Dynamo, Riak use version vectors to flag conflicts for reconciliation), ordering messages, enforcing causal consistency, distributed debugging/snapshots, and as a basis for many CRDT designs.

What is the downside of vector clocks?

Each timestamp is O(n) in the number of processes, so vectors grow large at scale and can accumulate stale entries with dynamic membership. Practical systems use variants — version vectors, dotted version vectors, interval tree clocks — to bound the overhead while keeping causality tracking.

Keep going

Finished this one? 0 / 61 Labs done
NextCRDT Merge: No Conflict Next on the “Agreement between machines” ladder · rung 4 of 5

This idea in other formats

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs