skip to content

In a household app resolving offline edits by last-write-wins, why is the phone's clock a weak way to pick the winner, and what is safer?

level: middleimportance: should knowfreq 46%

answer

  1. whose clock decides
  2. set by hand, drifting, unverifiable
  3. the server applies writes one at a time
  4. arrival order rewards the last to sync
  5. measure the offset, still sanity-check

basics

~20 s

A phone's clock can drift or be set by hand, so a phone whose clock runs ahead wins ties it should lose. Let the server decide order by arrival or by the revision it assigns, which every device sees the same way.

solid answer

~50 s

With device timestamps, the verdict tilts toward whichever phone's clock runs ahead: a partner whose phone is ten minutes fast, or was set to tomorrow by hand, wins ties against edits made after theirs whenever the gap is smaller than its lead. The server cannot verify those timestamps; it can only reject the absurd. Its own order is different: the server applies writes one at a time, so its order of arrival, or the revision number it assigns, is one sequence both phones agree on. The cost is meaning: arrival order rewards the phone that synced last, so an edit made offline yesterday can overwrite one made this morning. Where that matters, keep the edit time only as a hint, corrected by the offset the phone measured against the server's time at its last sync and clamped by the server, or detect the clash with base revisions and send that field to a merge or to the user.

go deeper

for a junior

Recall that phone clocks can be wrong or set by hand, so the server, not the phone, should decide which write came last.

for a middle

Explain the two meanings of 'last', edit time and arrival order, and what each one gets wrong for an app used offline.

for a senior

Show how base revisions turn a blind tie-break into detected staleness, and when corrected edit times are worth their failure modes.

for a principal

Decide per data type whether 'last to sync' is acceptable product behaviour, and keep that rule on the server so it can change without an app release.

## What "last" means **Last-write-wins (LWW)** keeps one of two conflicting values: the one that counts as written last. In a **shared household app**, where two partners edit shopping lists, chores and notes on their own phones and often offline, it is a sensible policy for a checked flag, a list title or a due date. But "last" needs a judge, and there are two candidates: - **Edit time**: when the user made the change, as measured by the phone that made it. - **Arrival order**: when the change reached the server, as measured by the server. They agree when both phones are online. They disagree as soon as a phone is offline for a while, which is the case this app exists for. ## Why the phone's clock is a weak judge - **Clocks drift and are set by hand.** A user can turn off automatic time or set the date manually; a phone that cannot reach a time source keeps drifting. - **The fastest clock wins.** A phone running ten minutes ahead beats every edit made in those ten minutes by its partner, consistently and invisibly. - **The server cannot verify it.** A timestamp arrives as a claim. The server can reject the absurd, such as a time ahead of its own clock, but cannot recover the true edit time. - **UTC does not help.** Storing times in a common time zone removes formatting confusion, not a clock that is simply wrong. Common offline-first guidance describes LWW with timestamps the devices attach. That is a valid design; the weak point is trusting whose clock the timestamp came from. ## What the server can offer instead | Judge | Who sets it | Same verdict on both phones? | Meaning of "last" | Main weakness | |---|---|---|---|---| | Raw phone time | Each phone | Yes, but driven by bad data | Edit time, if the clock is right | A skewed clock wins ties | | Corrected phone time | Phone, adjusted by measured offset | Yes | Edit time, approximately | Breaks if the clock changes after the last sync | | Server arrival order | Server | Yes | Sync time | An old offline edit beats a newer one | | Server revision plus base | Server | Yes | Sync time, with stale edits detected | Needs a policy for each detected clash | The **server revision** is the number the server assigns to each accepted write. Paired with the **base revision** each edit carries, it does more than break ties: it tells the server that an edit was made on a copy older than the current one, so the clash can go to a merge or a person instead of a coin toss. ## The cost of arrival order: a worked timeline 1. 09:00. Alex, offline on a train, ticks "oat milk" as bought. 2. 12:00. Sam, at home and online, unticks it: they ran out again. 3. 18:00. Alex's phone reconnects and sends the tick. 4. Under arrival order, Alex's tick lands last and wins, though Sam's untick was the newer intent. 5. Under correct edit times, Sam's untick would win, but only if both clocks were right. With base revisions the server sees that Alex's tick was based on the revision before Sam's untick. For a checked flag the household may accept arrival order, since the result is visible and easy to fix. For data where the gap matters, the server can prefer the newer edit time when the base was stale, or ask. ## Making edit time trustworthy enough If the product really needs edit-time meaning: - **Measure the offset.** At each sync, compare the phone's clock with the server's time in a response and store the difference; adjust edit times by it. - **Clamp on the server.** Refuse or cap timestamps later than the server's own clock at arrival. - **Fall back.** When a timestamp is implausible, use arrival order. - **Break exact ties deterministically**, for example by device id, so both phones settle on the same winner. This still fails if the user changes the clock between the last sync and the edit, which is why the safer default is to let the server decide order and reserve edit times for the fields where they truly matter.

  • How can the app keep edit-time meaning without trusting the raw clock?
    At each sync the phone compares its clock with the server's time from a response and stores the offset, then records edit times corrected by it. That fixes steady skew, not a clock the user changes after the last sync, so the server still clamps timestamps ahead of its own clock and falls back to arrival order when one is implausible.

  • Storing timestamps in UTC removes clock problems between phones.
  • A phone's clock is always correct because the network sets it.
  • The server's order of arrival always matches the order the user made edits.
  • A server revision number shows which partner edited most recently.