Engineering Practice Bug Report

Last-write-wins with wall clock timestamps dropped updates from a server running 2 seconds behind

Customers changed their delivery preferences and the change silently reverted. Our profile sync used last-write-wins on timestamps from each writing server, and one host's clock was 2.3 seconds slow. Why wall clocks cannot order events across machines, and how server-assigned versions fixed it.

Ravi Subramanian Ravi Subramanian @ravisubramanian · November 26, 2026 · 7 min read · 26 Views
Banner: clock skew causing last-write-wins to keep the older update

Support passed us a pattern that sounded impossible. A customer changed their delivery preference from "leave at door" to "hand to me only" in the app, saw the confirmation, and a minute later the setting showed "leave at door" again. It happened to a few dozen customers per day, never reproducibly in testing.

The profile data is replicated between two regions, each with its own API servers and its own database, and a sync service applies changes from one region to the other. Conflicts, the same field changed in both regions, are resolved with last-write-wins (LWW): each change carries a timestamp, and the one with the later timestamp is kept.

We found one affected customer in the change log:

region  host          field                 value           client_seen  server_ts (UTC)
------  ------------  --------------------  --------------  -----------  ------------------------
eu      api-eu-04     delivery_preference   hand_to_me      10:14:07.9   2026-10-28 10:14:05.611
us      api-us-11     delivery_preference   leave_at_door   (sync)       2026-10-28 10:14:06.920

The us row was not a new change by the customer. It was the sync service writing back a stale value. And yet LWW kept it, because its timestamp was later: 10:14:06.920 versus 10:14:05.611.

The client-side timestamp for the customer's change was 10:14:07.9, later than both server timestamps. The eu server's timestamp was more than two seconds earlier than what the customer's phone recorded. That pointed at the server clock.

The clock

ssh api-eu-04 chronyc tracking
Reference ID    : 00000000 ()
Stratum         : 0
Ref time (UTC)  : Thu Jan 01 00:00:00 1970
System time     : 0.000000000 seconds fast of NTP time
Last offset     : +0.000000000 seconds
Leap status     : Not synchronised

Not synchronised. The host had lost access to its NTP servers after a firewall change three weeks earlier. Compared against a synchronised host:

for h in api-eu-03 api-eu-04; do ssh $h 'date -u +%T.%N'; done
10:41:12.488102551
10:41:10.170448209

api-eu-04 was 2.3 seconds behind. Every change it wrote looked 2.3 seconds older than it really was.

How the update got lost

The sequence for the customer above:

  1. At about 10:14:06.0 real time, the sync service copied the old value leave_at_door from eu to us during a routine resync (unrelated to the customer's action). The us host, with a correct clock, stamped it about 10:14:06.9.
  2. At about 10:14:07.9 real time, the customer changed the preference on api-eu-04. Its slow clock stamped the change 10:14:05.6.
  3. The sync service compared the two: 10:14:06.9 (old value) beats 10:14:05.6 (new value). It wrote the old value back to eu.

The new value was real and later. The old value won because of a clock, not because of what happened.

Wall clocks cannot order events across machines

Each machine's clock drifts. NTP keeps them close, typically within milliseconds on a healthy network, but it offers no hard guarantee, and when it fails silently, as here, drift accumulates. Even with healthy NTP, two events a millisecond apart on different hosts cannot be reliably ordered by their timestamps. Clocks can also jump: NTP corrections, leap seconds, virtual machine pauses and manual changes all move time abruptly, sometimes backwards.

Lamport's 1978 paper on time and ordering in distributed systems makes this point directly: physical clocks on different machines do not give a reliable "happened before" relation. What you can order reliably is causality, using logical information carried with the data.

Why LWW made it silent

LWW never reports a conflict. It always picks a winner and discards the loser. With accurate clocks, that loses concurrent edits occasionally, which may be acceptable for some data. With a skewed clock, it systematically discards writes from one host whenever they race with anything else.

Environment

Item Value
Regions eu and us, active-active for profile writes
Conflict resolution last-write-wins on updated_at set by the API host
Clock sync chrony; one eu host unsynchronised for 21 days, 2.3 s slow
Impact about 30 reverted preference changes per day

1. Restore time sync, and alert on it

We fixed the firewall rule and confirmed chronyc tracking showed Leap status: Normal and an offset under a millisecond. Then we added an alert on clock offset for every host, exported by node_exporter's timex collector:

- alert: ClockNotSynchronised
  expr: node_timex_sync_status == 0
  for: 10m
- alert: ClockOffsetHigh
  expr: abs(node_timex_offset_seconds) > 0.05
  for: 5m

This would have caught the problem three weeks earlier. But it does not fix the design: with healthy clocks, LWW on wall time still mis-orders events that are close together.

2. Order by a server-assigned version, not by the clock

The deeper fix was to stop using timestamps for ordering. Each profile now has a version that only the owning region's database increments, inside the write transaction:

UPDATE profiles
SET delivery_preference = :value,
    version = version + 1,
    updated_at = now()
WHERE id = :id
RETURNING version;

updated_at stays for display and auditing. Ordering decisions use version.

Each profile has a home region (where the customer signed up). All writes for that profile are routed to its home region, so there is one sequence of versions per profile, and the other region holds a read replica of it. The sync service applies a change only if its version is greater than the version it already has:

UPDATE profiles_replica
SET delivery_preference = :value, version = :version
WHERE id = :id AND version < :version;

A stale resync with an old version is now ignored, regardless of what any clock says.

3. For truly multi-writer data, detect concurrency instead of hiding it

Routing writes to a home region costs some latency for customers travelling abroad. For one piece of data where both regions genuinely needed to accept writes (a shared shopping list), we used version vectors: each region keeps its own counter, and each value carries the vector of counters it has seen. If one vector is greater than or equal to the other in every component, the newer one wins. If neither dominates, the writes were concurrent, and the application merges them (for a list, a union of items) instead of dropping one.

value A: {eu: 5, us: 3}
value B: {eu: 4, us: 4}
neither dominates -> concurrent -> merge

4. If you must use time, use time with bounded uncertainty

We considered hybrid logical clocks (HLC), which combine physical time with a logical counter so that causally related events are always ordered correctly while timestamps stay close to wall time. They are a good fit when you need timestamps that are both meaningful and ordered. For our profile data, server-assigned versions were simpler.

1. A test with a skewed clock. The test injects a clock for each API host, with one 3 seconds behind, then issues a resync of an old value followed by a real change on the slow host:

def test_newer_change_wins_despite_slow_clock(eu_slow_by_3s, sync):
    sync.resync_old_value(profile_id, "leave_at_door")      # stamped by correct clock
    eu_slow_by_3s.update(profile_id, "hand_to_me")          # newer change, older timestamp
    sync.run()
    assert read_both_regions(profile_id) == {"eu": "hand_to_me", "us": "hand_to_me"}

With LWW on timestamps, both regions ended at leave_at_door. With versions, both show hand_to_me.

2. Production. Reverted preference reports dropped to zero after the rollout. The sync service now logs every ignored stale update with both versions; those logs show the resync path regularly attempting to write older versions, all correctly ignored.

3. Clock alerts. The offset alert has fired twice since, both for a single host after maintenance, and both were fixed before they affected anything.

4. Security implications. Wall-clock LWW also lets anyone who controls a timestamp win every conflict. An API that accepted a client-supplied updated_at would let a client overwrite newer data by sending a future time. Versions are assigned only by the database and never accepted from clients.

Takeaways

  • Wall clocks on different machines cannot reliably order events. Skew and jumps break last-write-wins.
  • Monitor NTP sync status and offset on every host; silent loss of sync is common.
  • Order writes with versions assigned in one place, such as the record's home database.
  • For multi-writer data, use version vectors to detect concurrent writes and merge them.

References: Lamport, Time, Clocks, and the Ordering of Events in a Distributed System, chrony documentation, Prometheus node_exporter timex collector.

Add a label

#ntp #distributed-systems #clock-skew #last-write-wins #logical-clocks
Written by
Ravi Subramanian @ravisubramanian

I have spent the past decade on systems where work moves through queues and events: order processing, notifications, payment flows and the services around them. I have helped split a monolith into services and, a few years later, helped merge some of them back. Distributed systems fail in ways that are hard to see from one log file. A poison message that blocks a queue for hours, a consumer that hoards messages, a schema change that silently drops events, a compensation step that ran twice, updates lost because two clocks disagreed. My posts trace each failure across the services involved, explain the guarantee we assumed and did not have, and show the design change that fixed it. For the bigger topics like sagas, CAP and service boundaries, I stay with concrete examples from systems I worked on rather than diagrams alone.

Advertisement

Comments

0

No comments yet. Be the first to comment!

Sign In to post a comment.