Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Distributed Architecture
Lesson 13 of 24

Time, clocks and ordering

Why wall clocks lie across machines, and how logical, vector and hybrid clocks order events safely.

20 min read

Physical clocks drift

Each machine's quartz clock drifts, commonly by tens of parts per million, which is a few seconds a day. NTP corrects this to within about a millisecond on a good data centre network and tens of milliseconds over the internet, and corrections can make the clock jump backwards. So if node A stamps a write at 12:00:00.005 and node B stamps a later write at 12:00:00.003, last-write-wins keeps the wrong one. Never use wall-clock timestamps from different machines to decide order when it matters, and use a monotonic clock for measuring durations and timeouts.

Lamport clocks

A Lamport clock is a counter per node. Increment it on every event, attach it to every message, and on receipt set yours to max(local, received) + 1. This guarantees that if event a caused event b, then L(a) < L(b). Ties are broken by node id to get a total order. The limitation: L(a) < L(b) does not tell you whether a caused b or they were merely concurrent. That is enough for a consistent total order, such as ordering log entries, but not for detecting conflicts.

Vector clocks detect concurrency

A vector clock keeps one counter per node. Compare two vectors element by element: if every entry of A is at most B's, A happened before B; if each has an entry larger than the other's, they are concurrent and you have a genuine conflict to resolve. Dynamo and Riak used this to keep sibling versions rather than silently dropping one.

  • Size grows with the number of writers, so prune or use per-replica rather than per-client entries
  • Concurrent versions are surfaced to the application or a merge function

Hybrid logical clocks and TrueTime

Hybrid logical clocks (HLC) pair the physical time with a logical counter. They stay close to wall-clock time, so they are readable and usable for snapshot reads, while preserving causality like a Lamport clock. CockroachDB and YugabyteDB use HLCs. Google Spanner goes further with TrueTime, GPS and atomic clocks that expose an uncertainty interval of a few milliseconds; a transaction waits out that uncertainty before committing, which yields globally ordered, externally consistent timestamps.

Key takeaways

  • Wall clocks on different machines disagree by milliseconds or more and can jump backwards
  • Lamport clocks preserve causality but cannot detect concurrency
  • Vector clocks detect concurrent writes so conflicts are surfaced, not lost
  • Hybrid logical clocks combine readable timestamps with causal ordering

Sign in to track lessons and earn badges.

Consensus with RaftDistributed transactions and sagas

Coming up

Course: 0/24 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 3/10Design a Hotel Booking SystemPrevent double booking with transactional holds and sagas while keeping availability search fast.Medium
  2. 4/10Design an Inventory ServiceKeep stock counts exact under concurrent orders using conditional updates, reservations and idempotent events.
Medium
All 10 challenges →