Skip to main content

Later Than What? Ordering Events Without a Global Clock

· 10 min read
Clustron Team
Distributed Systems Engineering

Picture the simplest possible conflict. Two nodes accept a write to the same key within a few milliseconds of each other, replication carries both toward a common owner, and something has to decide which value survives. The rule everyone reaches for first is last-write-wins: each write is stamped with the time it happened, and the one with the later stamp is kept. It is simple, it is deterministic, and on a single machine with a single clock it is exactly right.

Across machines it is a quiet data-loss bug waiting for the clocks to disagree — and they always disagree. Every node keeps time with a physical oscillator that drifts, corrected periodically by NTP, and between corrections two nodes can differ by milliseconds on a good day and far more on a bad one. Worse, the corrections are not gentle: NTP can step a clock backward, a virtual machine can be paused and resume with its clock seconds behind real time, and a leap second can make the same wall-clock instant happen twice. None of this is exotic; it is the normal weather of a cluster.

So here is what last-write-wins does under that weather. Node A, whose clock runs a little fast, writes v1 and stamps it 10:00:00.005. A few milliseconds later in real time, node B, whose clock runs a little slow, writes v2 and stamps it 10:00:00.003. Both land at the owner, which compares 005 against 003, keeps v1, and discards v2 — the write that actually happened later. No exception is raised, no conflict is logged; the newer value is simply gone. This post is about why timestamps cannot be trusted to order events, and what to use instead.

A timestamp is not an ordering​

The failure above is not a tuning problem you fix by syncing clocks harder. Tighter NTP shrinks the window but never closes it, because the thing a wall clock measures — an approximation of absolute physical time, independently on each node — is not the thing ordering needs. What ordering needs is the happens-before relationship: did event X causally precede event Y, could Y have been influenced by X, or were the two genuinely concurrent with no way for either to know about the other? Wall-clock timestamps cannot answer that question, because two numbers drawn from two drifting clocks tell you which clock read higher, not which event came first.

The gap shows up most sharply with causality. Node A sends a message to node B, and B processes it and writes a result. The result obviously happens after the message — B could not have produced it otherwise. Yet if A's clock is ahead of B's, the message can carry a later timestamp than the reply it caused, so a timestamp ordering puts the effect before its cause. Any system that resolves conflicts, orders a change feed, or decides read visibility on wall-clock stamps will, sooner or later, act on an ordering that reverses cause and effect. The timestamp is not wrong about the time; it is simply answering a different question than the one we asked.

Last-write-wins by wall clock — the newer write is lostNode A (fast)Node B (slow)write v1ts=10:00.005write v2ts=10:00.003real time →v2 is later in real time — but stamped earlier → discarded ✕Hybrid logical clock — causal order preservedNode ANode Bwrite v1hlc=(1000, 0)write v2hlc=(1000, 1)real time →(1000,1) > (1000,0) → v2 wins ✓

Logical clocks: count events, not seconds​

The escape is to stop measuring seconds and start counting events. A Lamport clock is a single integer each node keeps: bump it on every local event, and stamp every outgoing message with it; when a message arrives, set your counter to one more than the larger of your own value and the message's. The rule guarantees that if X happens-before Y, then X's counter is strictly less than Y's — the effect can never carry a smaller number than its cause, which is exactly the property wall clocks lack. Lamport counters give a total order good enough to break ties deterministically, and they cost one integer.

What a single counter cannot tell you is whether two events were concurrent — if X's number is less than Y's, X might have preceded Y or the two might have been independent, and the counter cannot distinguish them. A vector clock can: each node carries one counter per node, so the stamps capture the full happens-before partial order and can prove two events concurrent. The price is size — the stamp grows with the number of nodes, and in a cluster that churns, tracking a counter per participant becomes its own bookkeeping problem. Vector clocks are the right tool when you must detect conflicts precisely; they are heavy when all you need is to resolve them.

Hybrid logical clocks: keep the wall clock honest​

The practical middle ground is the hybrid logical clock. It keeps a physical-time component — so stamps still look like real timestamps and sit close to wall-clock time, which matters for human-readable logs and for reasoning about recency — but it never lets that component move backward, and it attaches a small logical counter to break ties and absorb clock skew. The result is a stamp that is monotonic like a Lamport clock, bounded close to physical time like a wall clock, and total-ordered so two writes always have a deterministic winner.

// Local event or send: advance physical time, but never go backward.
pt = max(wallClock.Now(), last.pt)
l = (pt == last.pt) ? last.l + 1 : 0 // same millisecond → bump the logical counter

// On receiving a message stamped (m.pt, m.l):
pt = max(wallClock.Now(), last.pt, m.pt) // never behind self OR sender
if (pt == last.pt && pt == m.pt) l = max(last.l, m.l) + 1
else if (pt == last.pt) l = last.l + 1
else if (pt == m.pt) l = m.l + 1
else l = 0

// Conflict resolution is just a comparison of (pt, l): the greater stamp wins — deterministically.

Because a node always advances its clock past any stamp it observes, a reply can never carry a stamp less than or equal to the message that caused it — causality is preserved without a per-node vector. Last-write-wins over hybrid logical stamps keeps the write that genuinely happened later, resolves true conflicts the same way on every replica, and degrades gracefully: if the physical clocks are well synchronized the logical counter stays near zero and the stamps read almost exactly like wall-clock time, and when the clocks skew the counter quietly does the work the wall clock cannot. The per-key version numbers that fence a stale cache write or a superseded lock holder are the same idea in its simplest form — a monotonic counter that refuses to move backward — scaled down to a single resource.

Wrap-up​

Ordering distributed events by wall-clock timestamp loses data, because wall clocks drift, jump and pause, so a write that happened later can carry an earlier stamp and last-write-wins silently keeps the older value. The real need is not a more accurate clock but the happens-before relationship, which physical time cannot express — it can even put an effect before its cause. Logical clocks count events instead of seconds: a Lamport counter gives a cheap total order that never puts a cause after its effect, a vector clock additionally detects concurrency at the cost of a per-node stamp, and a hybrid logical clock takes the practical path — physical time kept monotonic with a logical tiebreak, close to wall-clock time yet immune to skew. Resolve conflicts on those stamps and every replica makes the same decision, the later write wins for a defensible meaning of "later," and the per-key version fence you already use for a cache or a lock turns out to be the same principle in miniature. The clock on the wall is for humans; ordering belongs to a clock that counts.