← Lessons

quiz vs the machine

Platinum1820

Databases

Vector Clocks for Conflict Detection

Per replica counters reveal whether two versions are ordered or truly concurrent.

6 min read · advanced · beat Platinum to climb

The Ordering Problem

In a leaderless store, two replicas may each accept a write without knowing about the other. A plain timestamp cannot tell whether one write truly happened after another or whether they happened concurrently. A vector clock answers this.

How a Vector Clock Works

A vector clock is a set of counters, one per replica. When a replica processes a write it increments its own counter. The version it produces carries the whole vector. To compare two versions you compare every counter.

  • If every counter in version A is at least as large as in B and at least one is strictly greater, then A descends from B and supersedes it.
  • If neither dominates the other, the versions are concurrent and represent a genuine conflict.

Resolving Conflicts

When versions are concurrent the system keeps both as siblings and asks the application or a merge rule to reconcile them, for example by taking a union of a shopping cart.

Key idea

Vector clocks track a counter per replica so comparing two versions reveals whether one supersedes the other or whether they are concurrent and must be reconciled as conflicting siblings.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a vector clock track?

2. When are two versions considered concurrent?

3. How does a system typically handle concurrent versions?