Course contentsShow
← System Design
Lesson 367 of 1,91910. NoSQL: Key-Value StoresFree lesson

Vector Clocks and Conflict Detection

Tracking causality and detecting concurrent writes in eventually consistent distributed stores.

Vector Clocks and Conflict Detection

What You'll Learn

You'll understand how distributed key-value stores track causality between writes to detect when concurrent updates conflict.

The Problem: Who Wrote What When?

In eventually consistent systems with multiple replicas, the same key can be updated simultaneously on different nodes. Without careful tracking, the system can't tell which write happened "after" another — or if two writes happened concurrently (neither knew about the other). Simply using wall-clock timestamps doesn't work because server clocks drift and can't reliably order distributed events.

How Vector Clocks Work

A vector clock is a data structure that tracks the version history per replica. Each replica maintains a counter, and every write increments that replica's counter in the vector.

Example:

  • Replica A writes: {A:1}
  • Replica B writes independently: {B:1}
  • Replica A writes again: {A:2}

When comparing versions:

  • If all counters in version X are ≤ version Y, then X happened before Y
  • If neither version dominates, they're concurrent — a conflict exists

Analogy: Think of vector clocks like each family member (replica) marking their changes in a shared notebook. If Mom's version shows (Mom:3, Dad:2) and Dad's shows (Mom:2, Dad:3), neither saw the other's latest change — they edited concurrently.

Conflict Detection

When a read finds multiple versions that are concurrent (neither is strictly newer), the system returns all conflicting versions to the client. The application must reconcile them (last-write-wins, merge, or custom logic).

Key Takeaway: Vector clocks enable distributed key-value stores to detect concurrent writes by tracking causality per replica, allowing the system to identify conflicts that require resolution rather than silently losing updates.