skip to content

A distributed key-value store attaches a vector clock to every value it stores, one counter per replica. When two versions of the same key are compared during a read, how does the store use the vector clocks to decide whether one version happened-before the other, or whether the two are concurrent and represent a real conflict?

level: middleimportance: must knowfreq 70%

answer

  1. counter per replica
  2. pairwise dominance check
  3. concurrent = neither dominates
  4. detects, doesn't resolve
  5. unbounded growth risk

basics

~20 s

A vector clock is a list of counters, one per replica, attached to a value. Comparing two versions' lists shows whether one grew out of the other or both were written independently, though two clients' writes through one replica look ordered.

solid answer

~50 s

A vector clock is a map from replica/actor id to a monotonically increasing counter, stamped on a value each time it's written. To compare two versions, the store checks the counters pairwise: if every counter in version X is less than or equal to the corresponding counter in version Y, and at least one is strictly less, the store treats X as preceding Y, and Y simply supersedes it — no conflict. If neither vector dominates the other (some counters higher in X, others higher in Y), the writes were concurrent and represent a genuine conflict that must be preserved, typically by returning both as 'sibling' versions rather than picking one arbitrarily. A plain timestamp can't tell these cases apart, because it carries no information about what value each write was based on; the vector can, as long as no two concurrent writes go through the same replica and no entry was pruned.

go deeper

for a junior

Should grasp that a vector clock has one counter per replica and that comparing them can show whether one write came after another or whether neither saw the other.

for a middle

Should be able to explain the dominance rule (all ≤ and one <), why a dominated version is stale, that when neither dominates it's a real conflict the store keeps rather than resolves, and why a timestamp cannot tell these cases apart.

for a senior

Should discuss the cost of comparison/storage, the sibling-return behavior, how systems bound vector growth (per-node vs per-client entries, pruning) and what accuracy each bound gives up.

for a principal

Should be able to weigh vector clocks against alternatives (version vectors, CRDTs, server-side transactions) for a specific data model and justify the choice given churn characteristics and merge complexity budget.

## What a vector clock is A vector clock attached to a stored value is a small dictionary mapping each replica (or, in some implementations, each client) to an integer counter. When a node writes a new version of a key, it starts from the vector of the version the writer had read (the pointwise maximum of all of them, if the read returned several), increments its own counter, and stores that vector alongside the value. ## The comparison rule To compare two versions V1 and V2 during a read or during reconciliation, the store performs a pairwise comparison across every entry in both vectors: | Pairwise outcome | What the store concludes | |---|---| | V1's counter is less than or equal to V2's counter for every replica, and strictly less for at least one | V1 happened-before V2 — V2 was derived from V1 (or from something that dominates V1) — and V1 can simply be discarded as stale | | Some counters are higher in V1 and others are higher in V2 | Neither vector dominates the other: the two writes were made concurrently, each unaware of the other, and represent a genuine conflict rather than a simple 'newer supersedes older' relationship | These conclusions hold only when every source of concurrent writes has its own entry and no entry has been pruned; failure modes 1 and 2 below show what happens otherwise. ## Why timestamps alone are insufficient This mechanism exists because timestamps alone cannot express causality. A timestamp only tells you when a write happened according to some clock, not what data that write was based on — two writes with different timestamps might still be logically concurrent (neither client saw the other's result before writing), and blindly picking the later timestamp (LWW) can silently discard a change the other client never had a chance to account for. Vector clocks fix this by encoding, in the version itself, the causal history the writer had already observed: - if a client reads version V and then writes a new value, the new version's vector dominates V's vector, honestly reflecting 'this supersedes that.' - if two clients both read the same base version and write independently without seeing each other, through different replicas (or with an entry per client), neither of their resulting vectors dominates the other, and the system can detect that fact directly instead of guessing from timestamps. ## The trade-off The trade-off is metadata size and read/write complexity in exchange for conflict detection. Every value now carries a vector whose size grows with the number of distinct writers that have ever touched that key, and every read or reconciliation requires an O(n) pairwise comparison across the vector instead of a single scalar comparison. Detecting a true conflict doesn't resolve it — the store still has to decide what to do with two concurrent, undominated versions, which typically means returning both to the application (or storing both as 'siblings') and deferring the actual merge decision to code that understands the data's semantics. So vector clocks buy conflict detection, not conflict resolution; they trade LWW's silent-but-simple data loss for more complex sibling management. ## Failure modes 1. **Unbounded vector growth** — the characteristic failure mode. If a system tracks one counter per client (rather than per fixed set of replicas) and clients churn — mobile devices, ephemeral workers, load-balanced connections that rotate identities — the vector for a hot key can accumulate thousands of stale entries, bloating every read and write. Two ways to bound it each cost something: keeping one counter per replica or coordinating node instead of per client keeps the vector small but lets two clients' concurrent writes through the same node look ordered, so one of them is lost; pruning entries by age or by count costs accuracy (failure mode 2). Amazon's Dynamo paper documents a related growth, entries for extra coordinating servers during network partitions or server failures, and bounds it by dropping the clock's oldest (node, counter) pair once the clock reaches a threshold (say 10 pairs). 2. **Pruning that is too aggressive** — a second, subtler failure mode. A pruned vector loses history: a version can no longer be shown to descend from an older one, so the two look concurrent, and the stale version is kept and handed back as a conflict the application must settle again, where a careless merge can bring old data back. ## Dynamo in practice A concrete real-world instance is Amazon's Dynamo as its 2007 paper describes it: each object version carries a vector clock of (node, counter) pairs. On a read, the coordinator collects versions from the replicas queried, discards any version another version causally supersedes, and returns all the versions it deems causally unrelated; the client reconciles them and writes back a version that supersedes them. The paper's shopping cart reconciles by merging the versions, so an 'add to cart' is never lost, and the paper names the cost: deleted items can resurface. The paper calls these divergent or conflicting versions; other stores, Riak among them, call the concurrent versions they keep 'siblings'.

  • Once a store detects two versions are concurrent via vector clocks, what actually happens to them before the application sees a value?
    The store keeps both versions rather than picking one, returning them together as conflicting versions (often called 'siblings'). The read also returns a context, the pointwise maximum of their vectors; the client's next write carries it, so the new version descends from all of them and supersedes them. What the resulting value should be is left to the application or a defined merge function, because the store has no idea what the field means.
  • A read finds three versions of a key; how does the store decide which of them to return?
    It compares them pair by pair: a version another dominates (every counter at most the other's, one lower) is treated as its ancestor and left out of the answer; every version no other dominates is returned, as siblings when two or more remain, whatever order the pairs are compared in. The verdict is the store's: with one entry per replica, two clients' concurrent writes through one replica can look ordered, and a truncated vector can make an ancestor look concurrent.
  • How is a vector clock different from a simple per-write monotonic counter shared by all nodes?
    A counter literally shared by all nodes needs a coordinator or consensus to hand out its values. Per-node counters advanced past every value seen (scalar logical clocks) need neither and give one number per write, consistent with causality and totally ordered with a node-id tie-break; but a smaller number does not show that a write saw the other, so concurrent writes look ordered. A vector keeps a counter per node and shows which case it is, unless two concurrent writes share a node.

Like each author of a shared document keeping their own private page-count; comparing two drafts' page-counts per-author tells you instantly whether one draft strictly builds on the other, or whether two people wrote separate revisions at the same time without seeing each other's work.

  • Believes vector clocks pick a winner automatically
  • Thinks a vector clock is just a fancier timestamp
  • Doesn't know concurrent means neither vector dominates the other
  • Unaware that vector size can grow unbounded with client churn
  • Confuses vector clocks with sequence numbers/logical clocks with a single counter

context