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
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 entrydef send():
V[p] += 1
return list(V) # attach vector to messagedef 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
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.
Event
What happens
Rule applied
Vector
Lamport
1
P0 local
bump P0
[1,0,0]
1
2
P0 sends to P1
bump P0, attach [2,0,0]
[2,0,0]
2
3
P1 receives 2
max([0,0,0], [2,0,0]) = [2,0,0], bump P1
[2,1,0]
max(0, 2) + 1 = 3
4
P2 local
bump P2
[0,0,1]
1
5
P1 sends to P2
bump P1, attach [2,2,0]
[2,2,0]
4
6
P2 receives 5
max([0,0,1], [2,2,0]) = [2,2,1], bump P2
[2,2,2]
max(1, 4) + 1 = 5
7
P0 local
bump 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.
Case
Time
Space
Why
Local event
O(1)
O(1)
One increment of your own entry.
Send
O(n)
O(n) per message
Bump, then copy the whole vector into the message. Message size grows with n.
Receive
O(n)
O(1) extra
One max per entry, then one bump.
Compare two events
O(n)
O(1)
Check ≤ both ways. Four outcomes: before, after, equal, concurrent.
All concurrent pairs of m events
O(m² n)
O(m n)
Every pair compared. The lab does 21 pairs × 3 entries.
Version vector, per replica
O(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 contrast
O(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.
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?
A local event bumps just the process’s own position in its vector. Each entry V[j] tracks how many of process j’s events this process knows about, so a local event only advances the process’s own count.
2. How does a process update its vector when it receives a message?
On receive, each entry becomes the max of the local value and the message’s value — adopting all the events the sender knew about — and then the receiver bumps its own entry for the receive event.
3. Two events have vectors [2,1,0] and [1,0,2]. What is their relationship?
For [2,1,0] and [1,0,2]: the first is bigger in positions 0 and 1, the second is bigger in position 2. Neither dominates the other, so neither could have known about the other — they are concurrent.
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.