Time, clocks and ordering
Wall clocks lie, monotonic clocks, Lamport and vector clocks, and why "at the same time" is undefined.
Every service has a clock, every log line has a timestamp, and every engineer assumes that two timestamps can be compared to say which event happened first. Inside one process that is nearly true. Across two machines it is false in ways that produce the bugs this lesson is about: the write that "happened before" the read it does not contain, the lock that expired on one clock and not the other, the sort by timestamp that puts the reply before the message. The fix is not a better clock; it is knowing which questions a clock can answer.
Wall clocks drift, and are corrected by jumping
A machine's wall clock (System.currentTimeMillis(), Instant.now()) is a quartz oscillator that drifts — tens of parts per million, which is seconds per day — corrected by NTP, which slews it gently when the error is small and steps it when the error is large. A step is a jump: the clock reads 10:00:05, then 10:00:02. Between machines, NTP keeps clocks within milliseconds on a good network and tens or hundreds of milliseconds on a bad one, a virtual machine that was paused, or a container whose host was under load; and an NTP outage lets them drift apart unbounded.
So a comparison of timestamps from two machines carries an error bar you do not know. if (event.timestamp > lastSeen) apply(event) drops events from a machine whose clock is behind; a token that expires at now + 30s on the issuer is already expired on a validator whose clock is 31 seconds ahead; and "the latest write wins" by wall-clock timestamp — which is what Cassandra does by default and what many "last writer wins" caches do — lets a machine with a fast clock overwrite newer data from a machine with a slow one, silently and forever. Wall time is for humans and logs, and even in logs the correlation id from the observability course is what orders the lines, not the timestamp.
Monotonic time: for durations, on one machine
System.nanoTime() reads a different clock: one that only moves forward, at a steady rate, unaffected by NTP steps — and whose value means nothing except as a difference from another nanoTime() on the same JVM. It is the clock for every duration: a timeout, a retry backoff, a latency measurement, a cache TTL. Instant.now() for those is a bug that surfaces once a year, when a clock step turns a 30-second timeout into a negative one that never fires, or into a 31-minute one; the JDK's own ScheduledExecutorService, Thread.sleep and CompletableFuture.orTimeout use the monotonic clock for exactly this reason, and the microservices course's retry budgets should too.
The rule is two clocks for two jobs: wall time to say when something happened for a person, monotonic time to say how long something took for a program. Neither says which of two events on two machines came first.
Ordering without a clock: happens-before
Lamport's observation is that the order we actually need is causal: event A happened before event B if A could have influenced B — the same process did A then B, or A was the sending of a message and B its receipt, or there is a chain of those. Two events with no such chain are concurrent, and asking which came first is asking a question with no answer; any system that gives one (by wall clock) is guessing.
The concurrency course's Java Memory Model is this idea inside one JVM: synchronized and volatile establish happens-before between threads, and without them two threads' writes have no order. Across machines the same is true with messages instead of monitors, and the clocks below are the tools that make happens-before computable.
Lamport clocks: a counter that respects causality
Each process keeps a counter. On every local event, increment it. On every send, increment and attach the value. On every receive, set the counter to max(local, received) + 1. The result: if A happened before B, then L(A) < L(B), always. So sorting events by Lamport timestamp (with a process id to break ties) gives a total order that never contradicts causality — the order a log, an audit trail or a replicated state machine needs, from a number that costs nothing.
What it does not give: the converse. L(A) < L(B) does not mean A caused B; two concurrent events get some order, arbitrarily. That is fine for "put these in a consistent sequence" and not fine for "detect that these two writes conflicted", which is the next clock's job.
Vector clocks: detecting concurrency
A vector clock is a counter per process, carried as a map: {A: 3, B: 1, C: 7}. Each process increments its own entry on an event and a send; on receive, take the element-wise maximum and increment your own. Now comparison is three-way: V(A) < V(B) (every entry less or equal, at least one less) means A happened before B; the reverse means B before A; and neither means they are concurrent — two writes that neither knew about the other, which is a conflict the application must resolve rather than a winner a clock can pick. That is what Riak and the original Dynamo used to return siblings instead of silently dropping a write, and what a CRDT uses to merge deterministically.
The cost is the vector's size: one entry per process that has ever written, which for a system with many short-lived clients grows without bound and needs pruning; version vectors (per replica rather than per client) and dotted version vectors are the practical forms. For most services the pragmatic middle is a single version number per object with optimistic concurrency — the JPA course's @Version, the MongoDB lesson's expected-version check, the HTTP ETag with If-Match — which detects the conflict between two writers to one row without ordering anything else.
Where this lands in a service
- Ordering events: a per-partition sequence from Kafka (the offset) or a per-aggregate version from the outbox, never the producer's wall-clock timestamp. Two events from two producers about different aggregates have no order and should not need one.
- Expiry and leases: a lease issued for 30 seconds is checked against the issuer's clock, or with a margin large enough to cover the drift you have measured; the failure-detection lesson makes this concrete with fencing tokens.
- Timeouts and retries:
nanoTime, always. - Idempotency and conflicts: a version or a vector on the object, and a merge rule written down, not "the later timestamp wins".
- Logs and traces: the correlation id orders them; the timestamp decorates them. A trace whose child span starts before its parent is clock skew, not time travel.
The one place wall clocks are made trustworthy at scale is by bounding their error explicitly: Google's TrueTime returns an interval [earliest, latest] from GPS and atomic clocks and Spanner waits out the uncertainty before committing, which is the engineering behind "external consistency" and the reason it needs hardware most systems do not have.
Under the hood: a Lamport clock worked in numbers, and the arithmetic NTP actually runs
A worked trace makes the Lamport rule concrete in a way the rule stated abstractly does not. Three processes, P1, P2, P3, each start their counter at 0. P1 does a local event (increments to 1), then sends a message to P2 (increments to 2, attaches 2 to the message). P2, independently, was doing its own local event at the same wall-clock moment (increments to 1) — a genuinely concurrent event, since nothing connects it to anything P1 or P3 has done yet. P2 then receives P1's message carrying timestamp 2: per the receive rule, P2 sets its counter to max(local, received) + 1 = max(1, 2) + 1 = 3. Meanwhile P3, which has done nothing yet (counter 0), receives a message from P2 sent before P2's local event — this ordering is deliberately awkward to make the point — carrying timestamp… there is no such message in this trace, so P3's counter stays at 0 until it does something of its own.
| event | process | rule applied | counter after |
|---|---|---|---|
| local event | P1 | increment | 1 |
| send to P2 | P1 | increment, attach | 2 |
| local event (concurrent with P1's events) | P2 | increment | 1 |
| receive from P1 (carries 2) | P2 | max(1, 2) + 1 | 3 |
| local event | P3 | increment | 1 |
Read the table against the rule the lesson already gave: P1's send (2) happened-before P2's receive (3), and indeed 2 < 3 — the guarantee holds. But P2's local event (1) and P3's local event (1) are concurrent — neither happened-before the other, there is no message between them — and their Lamport timestamps are simply equal, or would sort arbitrarily by process id if used as a total order. That equal-or-arbitrary outcome for concurrent events is not a flaw to fix; it is the exact fact the earlier section names when it says Lamport clocks give you a total order but not the converse — L(A) < L(B) never tells you A caused B, only that the clock had to put them in some order and this is the order it chose.
NTP's actual mechanism is a four-timestamp round trip, and its accuracy floor comes from an assumption about the network, not from the oscillator. A client sends a request stamped with its own clock at t1; the server stamps its receipt at t2 and its reply at t3; the client stamps the reply's arrival at t4. From those four numbers, NTP computes two quantities: the round-trip delay, (t4 - t1) - (t3 - t2) (the total time elapsed minus the time the server spent thinking), and the clock offset, ((t2 - t1) + (t3 - t4)) / 2. That offset formula is only correct under one assumption: that the network delay from client to server equals the delay from server to client — the trip is symmetric. When that assumption holds, the two one-way delays cancel out of the arithmetic and the offset is exact; when it does not — an asymmetric route, congestion in one direction only — the computed offset carries an error equal to half the delay asymmetry, silently, with nothing in the four timestamps that reveals it happened. This is why NTP's real-world accuracy floor over the public internet (single-digit to tens of milliseconds, commonly) is set by network path asymmetry far more than by the oscillator's own drift, and why it does not meaningfully improve just by polling more often.
Try it yourself
Trace a Lamport clock across three processes with a send and a receive
int p1 = 0, p2 = 0, p3 = 0;
p1 = p1 + 1;
System.out.println("p1 local -> " + p1);
p1 = p1 + 1; // send: increment, then attach p1 to the message
int carried = p1;
System.out.println("p1 send -> " + p1);
p2 = p2 + 1; // concurrent with p1's events above, no message yet
System.out.println("p2 local -> " + p2);
p2 = Math.max(p2, carried) + 1; // receive rule: max(local, received) + 1
System.out.println("p2 receive(" + carried + ") -> " + p2);
p3 = p3 + 1; // p3 has received nothing, so only its own rule applies
System.out.println("p3 local -> " + p3);Before running it: which two lines produce timestamps that came from a genuine happens-before relationship, and which two are concurrent events that merely ended up with the same or an arbitrary-looking number?
Misconceptions
- "NTP synchronization makes wall clocks accurate enough to use for ordering, as long as you sync often." NTP's offset calculation assumes a symmetric network path; an asymmetric one leaves an error the protocol cannot detect from its own timestamps, no matter how frequently it polls. "Synced with NTP" bounds drift, it does not certify the clock is accurate enough to safely order two specific events from two specific machines.
- "When a vector-clock comparison finds neither
V(A) < V(B)norV(B) < V(A), something has gone wrong." That outcome is the vector clock working correctly — it is the definition of two concurrent, conflicting writes, and it is precisely the signal the application is supposed to act on (return both versions as siblings, run a merge function), not evidence of a bug in the clock or the replication.
Sources
- Leslie Lamport, "Time, Clocks, and the Ordering of Events in a Distributed System" (1978) — the original paper defining happens-before and the Lamport clock.
- David L. Mills et al., RFC 5905, "Network Time Protocol Version 4: Protocol and Algorithms Specification" — the four-timestamp offset and delay computation.
- Martin Kleppmann, Designing Data-Intensive Applications — chapters 8 and 9 cover clock unreliability, happens-before and version vectors together.