Distributed Systems for Backend Engineers · Distributed Systems Fundamentals
Time, Clocks and Ordering
Wall clocks, monotonic clocks, logical clocks and hybrid logical clocks.
Wall clocks (NTP-synchronized) drift and can jump backwards, so they can't order events across machines reliably. Monotonic clocks measure durations on one machine. Logical clocks (Lamport timestamps) order causally related events; vector clocks detect concurrency; hybrid logical clocks (HLC, used by CockroachDB and others) combine physical time with logical counters. Google Spanner's TrueTime exposes clock uncertainty bounds and waits them out to provide external consistency.
Clocks
| Clock | Gives you | Use for |
|---|---|---|
| Wall clock (NTP) | Approximate real time | Timestamps for humans, TTLs (with slack) |
| Monotonic clock | Elapsed time on one host | Timeouts, latency measurement |
| Lamport clock | Total order consistent with causality | Ordering messages |
| Vector clock | Detects concurrent updates | Conflict detection (Dynamo-style) |
| Hybrid logical clock | Causality + close to physical time | Distributed databases, MVCC |
class LamportClock:
def __init__(self):
self.time = 0
def tick(self) -> int: # local event or send
self.time += 1
return self.time
def receive(self, remote_time: int) -> int:
self.time = max(self.time, remote_time) + 1
return self.time
start := time.Now() // contains a monotonic reading
doWork()
elapsed := time.Since(start) // uses the monotonic clock: safe across NTP adjustments
Common pitfalls
- Last-write-wins by wall-clock timestamps across nodes (silent data loss).
- Using wall time for timeouts.
- Assuming event timestamps from different services are comparable to the millisecond.
Key points
- Wall clocks drift and jump
- Monotonic for durations
- Logical/hybrid clocks for ordering
Further reading: Lamport: Time, Clocks, and the Ordering of Events
Practice questions
Why is last-write-wins using wall-clock timestamps dangerous?
Answer
Clock skew means a later write on a node with a slow clock can get an earlier timestamp and be discarded, silently losing data.
What does a vector clock let you detect that a Lamport clock can't?
Answer
Whether two events are concurrent (neither causally precedes the other), which indicates a conflict.
Open in the app to mark lessons complete, take notes and track your progress.