ReviseAlgo Logo
Beginner8 min readFoundations of Distributed Systems

Logical Clocks

Ordering global transactions without physical hardware metrics — covering Lamport Timestamps (sequential integer causality), Vector Clocks (integer array concurrent update detection), and sequence-tracking algorithms.

What you'll learn

  • Lamport Timestamps — Rules
  • Lamport Timestamps — Limitation
  • Vector Clocks — Structure
  • Vector Clocks — Detecting Concurrency
  • Dotted Version Vectors
  • Version Vectors vs. Vector Clocks

TL;DR

Ordering global transactions without physical hardware metrics — covering Lamport Timestamps (sequential integer causality), Vector Clocks (integer array concurrent update detection), and sequence-tracking algorithms.

Visual System Topology

Logical Clocks Execution Topology

Inbound Node Ingests request
Logical Clocks Engine Processes operations
Target Replica Updates state

Concept Overview

Logical clocks are mechanisms for ordering events in distributed systems without relying on synchronized physical clocks. They capture the causal structure of a distributed computation: which events caused which other events, and which events happened concurrently with no causal relationship.

Two fundamental implementations: Lamport Timestamps (scalar integer clocks that preserve the happens-before ordering) and Vector Clocks (arrays of integers, one per process, that fully capture causal history and can detect concurrent events).

Logical clocks are foundational to distributed databases, collaborative applications, and event sourcing systems. They are the mechanism behind DynamoDB's conflict detection, Riak's concurrent write handling, and distributed version control systems like Git.

Key Architectural Pillars

1

Lamport Timestamps — Rules

Three rules: (1) Before each local event, increment your counter. (2) Include your counter in every message you send. (3) On receiving a message with timestamp T, set your counter = max(local_counter, T) + 1. This ensures that if A happens-before B, then Lamport(A) < Lamport(B) — but NOT the reverse.

Example: P1 has counter 3, sends message to P2 with counter 1. P2 receives: counter = max(1, 3) + 1 = 5. P2's next event will have timestamp 6.
2

Lamport Timestamps — Limitation

Lamport timestamps only partially capture causality. If L(A) < L(B), A may or may not have caused B. Two concurrent events (no causal relationship) can still have different Lamport timestamps. You cannot use Lamport timestamps alone to detect concurrency.

Example: P1 writes X with timestamp 5. P2 independently writes X with timestamp 3. Lamport says P2 happened first, but these are actually concurrent events — neither caused the other.
3

Vector Clocks — Structure

Each process maintains an array V of size N (one slot per process). On local event: V[self]++. On send: attach full V array. On receive(V_msg): for each i, V[i] = max(V[i], V_msg[i]); then V[self]++. Comparing two vectors: V(A) < V(B) iff all elements of V(A) ≤ V(B) and at least one is strictly less.

Example: Three processes. P1=[2,1,0] sends to P3=[0,0,3]. P3 updates: [max(2,0), max(1,0), max(0,3)] + increment own = [2,1,4].
4

Vector Clocks — Detecting Concurrency

Events A and B are concurrent (no causal relationship) iff V(A) ≤ V(B) is false AND V(B) ≤ V(A) is false. This is the key advantage over Lamport timestamps. Concurrent events are exactly the cases where conflict resolution is needed.

Example: V(A)=[2,1,0] and V(B)=[0,2,0]: Neither is ≤ the other. These events are concurrent — they happened independently with no causal relationship, representing a potential write conflict.
5

Dotted Version Vectors

An optimization over vector clocks used in Riak and some versions of DynamoDB. Reduces the size of the causal context that needs to be stored by tracking only the "dot" (which replica originated the write) alongside the causal history.

Example: Riak uses dotted version vectors to track which replica made a specific write, enabling precise sibling identification during conflict resolution.
6

Version Vectors vs. Vector Clocks

Version vectors track the version of data items across replicas (for conflict detection at the data level). Vector clocks track causal relationships between events (for ordering events). The distinction matters: version vectors attach to data; vector clocks attach to events.

Example: DynamoDB's version vectors track "this item has been written to replica 1 twice and replica 2 once." Vector clocks in a collaborative editor track "user A's edit caused user B's edit."

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In
Logical Clocks - Module 1: Foundations of Distributed Systems | System Design | Revise Algo