Concept · Foundations

Time Synchronization & Clocks

01

Why this matters

Two machines, separated by a network, disagree on the order of events. "Event A at 12:00:00.500" on server 1. "Event B at 12:00:00.400" on server 2. Did B come before A? Only if the clocks agree, and they never perfectly do. Clocks drift, jump, lag, and sometimes go backwards. Any system that reasons about "what happened first" is building on unstable ground unless it's explicit about which clock, and why.

Time is the stealth cause of most distributed-systems bugs. Understanding the distinction between physical and logical clocks, and which to use when, is foundational.

02

Three kinds of clock

Wall clock (NTP-synced)

Good for display, bad for ordering

Real-world time via NTP (~10ms accuracy typical, 100ms worst-case). Drifts, jumps on corrections, can leap backward. Use for timestamps shown to users, NOT for deciding event order.

Monotonic clock

Intra-process duration

OS-provided, only ever increases. Great for "how long did this operation take?" (timeouts, benchmarks). Not comparable across machines — each starts at process launch.

Logical clocks

Causal ordering without real time

Lamport timestamps, vector clocks, or Hybrid Logical Clocks. Track "event A caused event B" without trusting wall clocks. What any correctness-critical distributed algorithm should use.

03

Lamport timestamps — the minimum viable logical clock

Every node maintains a counter t.

  • Local event → t = t + 1.
  • Send a message → include current t.
  • Receive a message with timestamp t't = max(t, t') + 1.

Result: if event A happened-before event B (causally), then A's timestamp < B's timestamp. The converse isn't true — smaller-numbered doesn't prove causal precedence, just potential precedence. Lamport timestamps give you a total order; vector clocks give you a partial order with precise causal comparisons.

Lamport Timestamps — Causality Across 3 Nodes SVG
A B C 1 2 5 2 max(1,1)+1 3 4 max(0,3)+1 If A happened-before B, then A.ts < B.ts (one direction only)
~10 ms
NTP typical drift
~100 ms
NTP worst-case
~5 ms
Spanner TrueTime ε bound
< 1 μs
PTP in a controlled LAN
04

Deep dive — Spanner's TrueTime, the "give up" solution

Google Spanner wanted globally-consistent transactions — strict serial order of commits across the planet. Logical clocks alone don't provide this (they don't connect to real time). NTP is too loose (100ms is an eternity).

Google built TrueTime: every datacenter has GPS receivers AND atomic clocks, redundantly. Every machine queries a local time master which reports [earliest, latest] — a bounded uncertainty interval. Typical bound: ε ≈ 5ms worldwide, guaranteed.

Spanner uses TrueTime to implement commit wait: before acknowledging a write at timestamp T, wait until local TrueTime's "earliest" exceeds T. Ensures that any observer polling after the ack sees T in their past. Provides external consistency — a globally-linearizable order of commits — at the cost of 5–10ms extra latency per commit.

Nobody else has this because nobody else wants to install atomic clocks in every datacenter. CockroachDB mimics it with Hybrid Logical Clocks (NTP + logical counter) — almost-as-good external consistency without the atomic-clock CapEx.

05

Practical rules

  • For timeouts — use monotonic clock, never wall clock. Wall-clock jumps break timeouts; monotonic ones don't.
  • For audit logs — wall clock + NTP is fine. Humans will read them; 100ms error doesn't matter.
  • For ordering distributed events — logical clocks (Lamport / vector). If you need wall-time approximation, use HLC.
  • For distributed transactions — TrueTime if you're Google, HLC otherwise. Don't roll your own.
  • For leases + expiry — include enough margin for clock skew. "Lease expires at T" should be treated as "definitely expired by T+ε" by any consumer.
06

Real-world

Google Spanner

TrueTime

GPS + atomic-clock redundant time masters. External consistency at planet scale.

CockroachDB / YugabyteDB

Hybrid Logical Clocks

NTP + logical counter. 99% of Spanner's benefit without atomic-clock hardware.

Cassandra

Wall clock + LWW

Last-write-wins using wall-clock timestamps. Why Cassandra has a reputation for lost writes — two concurrent writers with skewed clocks silently lose one write.

Chrony / PTP

Sub-millisecond NTP

High-precision time protocols for financial + scientific systems. PTP achieves < 1μs in a controlled LAN.

07

Used in problems

Google Docs uses logical clocks (OT sequence numbers) for concurrent edits. Stock exchange uses PTP for matching-engine determinism. Distributed logging uses HLC for log ordering. Payment gateway uses monotonic clocks for idempotency-key expiry.

Next up