← Data structures

Vector clock

Text, space and time

A small array with one counter per node, attached to every version of a value, that records how many updates each node had seen when it wrote. It exists because wall clocks drift, so the only reliable way to say "this version came after that one" is to count events rather than read a clock.

What it costs

OperationCostNote
Local writeO(1)Increment your own slot in the vector and attach it to the version.
Compare two versionsO(N)N is the number of nodes. Element-wise: A ≤ B everywhere means A happened before B.
Merge on syncO(N)Take the element-wise maximum of both vectors, then increment your own slot.
Detect conflictO(N)Neither vector is ≤ the other: the writes were concurrent and need resolution.

Memory: One integer per participating node per stored version: 3 devices × 8 bytes = 24 bytes on each message version. With open membership (every client ever seen) vectors grow unbounded, so systems prune old entries or use a fixed set of replicas.

Two devices edit offline; the vectors reveal the conflict

Each write increments the writer’s own slot. [1,0,0] and [0,1,0] are each ahead in one slot, so neither happened before the other: the server keeps both as siblings instead of overwriting.

How it works

  1. Every node (a device, a replica) has a slot in the vector. A fresh value starts at [0, 0, 0] for three nodes: phone, laptop, server.
  2. When a node writes, it increments its own slot and stamps the new version with the whole vector. The phone editing a draft produces [1, 0, 0]; editing again gives [2, 0, 0].
  3. When a node receives a version from another, it merges clocks by taking the element-wise maximum, then increments its own slot before writing. The laptop receiving [2, 0, 0] and then editing produces [2, 1, 0].
  4. To compare two versions: if every slot of A is less than or equal to the matching slot of B, then A happened before B (B knew about A). [2, 0, 0] ≤ [2, 1, 0], so the laptop’s edit supersedes the phone’s and can replace it.
  5. If some slots are greater in A and others greater in B, neither saw the other: the writes were concurrent, which is a real conflict. [3, 0, 0] (phone edited again offline) versus [2, 1, 0] (laptop edited): phone is ahead in slot 0, laptop in slot 1. Keep both and let the application merge or ask the user.
  6. A plain timestamp cannot tell these cases apart: with clocks 200 ms skewed, last-writer-wins would silently drop one edit. The vector records causality (what each writer had seen) instead of time.

Why it is shaped this way

  • One counter per writer is the minimum information needed to know whether a writer had seen another writer’s update; a single global counter cannot express "neither saw the other".
  • Comparison is element-wise so it needs no coordination and works offline, which is the whole point for devices that sync later.

Worked example

A chat message edited from two devices

  • Alice edits message 1041 on her phone while offline: version [1, 0, 0], text "see you at 7". The server and laptop still hold [0, 0, 0], "see you at 6".
  • Before the phone syncs, Alice edits the same message on her laptop (online): laptop merges server’s [0, 0, 0] with its own and writes [0, 1, 0], "see you at 6:30". Server stores it.
  • Phone comes online and pushes [1, 0, 0]. Server compares with [0, 1, 0]: slot 0 says phone is ahead, slot 1 says laptop is ahead. Neither dominates: concurrent edits. The server keeps both versions as siblings and flags the conflict; the app shows Alice both texts and she picks one, producing [1, 1, 1] from the merge plus the server’s increment.
  • Compare the alternative: timestamps. The phone’s clock is 3 minutes fast, so its edit "wins" last-writer-wins and the laptop’s 6:30 silently disappears. Vector clocks make the loss visible instead.
  • Size: 3 slots × 8 bytes = 24 bytes per version. Across 500K messages a second that is 12 MB/s of clock metadata, negligible against the message bodies.

When it is the wrong choice

  • A single writer per key (a per-conversation sequence assigned by one server): a plain increasing counter gives total order with one integer.
  • Thousands of writers with open membership (every browser ever seen): vectors grow one slot per writer. Use dotted version vectors with pruning, or route writes through a fixed set of replicas.
  • When the application cannot do anything useful with a conflict: detecting it only helps if you can merge (a CRDT) or ask the user. Otherwise last-writer-wins with a hybrid logical clock is simpler and honest about losing writes.

Interviewer follow-ups

Answer out loud before you open each one.

Why not just use timestamps and let the last write win?

Because clocks on different machines differ by milliseconds to minutes, and last-writer-wins silently discards whichever write had the unluckier clock. A vector clock records what each writer had seen, so you can tell a genuine overwrite from two edits that never saw each other and handle the second case deliberately.

What does it mean when neither vector is less than or equal to the other?

The two writes were concurrent: each writer made its change without having received the other’s. There is no correct automatic winner. The system keeps both as siblings and resolves by merging (if the data type allows), by application rules, or by asking the user.

How do vector clocks grow, and how do you keep them small?

One slot per distinct writer, forever. With a fixed set of replicas that is fine; with every client device as a writer it balloons. Fixes: let only replicas write (clients send to a replica, which stamps with its own slot), prune slots that have not changed in a long time, or use dotted version vectors that track the latest event per node compactly.

How does a vector clock relate to a Lamport clock?

A Lamport clock is a single counter that every node bumps to max(seen) + 1 on each event; it orders events consistently with causality but cannot detect concurrency, because two concurrent events can get any two numbers. A vector clock keeps one counter per node so it can say "concurrent" when neither dominates.

Where it shows up

Building blocks
Accept a short delay for scale · Do not trust the client clock · Order events within a stream · Deliver to users who are offline
Problems
Chat App (WhatsApp / Messenger) · Distributed Key-Value Cache (Redis / Memcached-like)