Logical Clocks

Updated June 3, 2026
M
Magic Magnets Team
7 min read

Imagine a courtroom where the stenographer’s watch is broken. How can they prove that the lawyer asked a question before the witness answered?

They don't need a watch to know the order. The witness couldn't possibly answer a question that hadn't been asked yet. The answer is causally dependent on the question.

This is the core idea behind Logical Clocks.

The Core Concept

Because physical clocks across different servers can never be perfectly synchronized (due to clock drift and network delays), we can't use timestamps like 14:32:01 to determine the true order of events in a distributed system.

In 1978, a brilliant computer scientist named Leslie Lamport realized something profound: We don't actually care exactly when an event happened. We only care what order things happened in.

A Logical Clock is a mechanism for capturing chronological and causal relationships in a distributed system without relying on physical time.

The "Happens-Before" Relationship

Logical clocks are built on a concept called the "happens-before" relationship (often written as ->).

We can confidently say Event A happens before Event B (A -> B) if:

  1. Same Node: Event A and Event B happen on the same server, and A occurred before B locally.
  2. Message Passing: Event A is the sending of a message from Server 1, and Event B is the receipt of that exact same message on Server 2. (You must send a letter before it can be received).
  3. Transitivity: If A happens before B, and B happens before C, then A happens before C.

If two events don't fall into these rules (e.g., two servers logging errors at roughly the same time without talking to each other), they are considered Concurrent. We don't know which happened first, and frankly, the system doesn't care because they don't affect each other.

Quiz Time

Server 1 sends a message to Server 2. Server 2 then does a local write. Using the happens-before relationship, which statement is true?

Quiz Time

Two events on different servers have no messages between them. What is their happens-before relationship?

Real-World Examples

Why do we need this? Imagine a collaborative document editor (like Google Docs).

  • User 1 makes a word bold.
  • User 2 deletes the word.

If we rely on physical time, and User 2 has a slow clock on their laptop, the system might think the deletion happened before the bolding. The result? A crash, or a phantom bold word reappearing.

By using Logical Clocks, the system tracks that User 2 saw the bold word before they hit delete. Therefore, Bolding happens-before Deletion, ensuring the final document state is correct regardless of whose laptop clock is wrong.

Implementations

Logical clocks are a concept. To actually use them in code, we use specific algorithms:

  1. Lamport Timestamps: The simplest form, using a single integer counter. It guarantees that if A happens before B, A's timestamp is smaller.
  2. Vector Clocks: A more advanced array of counters that can perfectly detect if two events were concurrent or if one caused the other.

Summary

  • Physical clocks are unreliable for ordering events across different servers.
  • Logical clocks ignore physical time (seconds/minutes) entirely.
  • They track the order of events based on causality (the "happens-before" relationship).
  • If two events don't causally affect each other, they are concurrent.
  • They are essential for collaborative editing, database replication, and conflict resolution.

Lamport Timestamps

How helpful was this content?

Comments

0/2000

Sign in to join the discussion

Saved on this device only

Sign in to sync progress across devices