Clocks
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Clocks, ordering and conflict resolution: the 60-second story

    Clocks and ordering

    The order says PAID. A rider is already on the way.

    Dispatch saw PAID and wrote RIDER_ASSIGNED 50 ms later. Node A’s clock ran 80 ms ahead, so last-write-wins silently kept PAID.

    1 ms

    NTP’s accuracy on a LAN, at best

    Tens of ms over the internet, and clocks step and pause. At 10,000 writes a second, that’s 10 writes per millisecond whose order is a guess.

    Who picks the order?

    Bad: Server timestamps

    Inside the skew, a later write can carry a smaller stamp.

    Good: One place hands out numbers

    A log index, a partition offset, a version with compare-and-set. Nothing to skew.

    Lamport’s clock: tick on every event, jump on receive

    Messages carry the counter; receivers take max(local, received) + 1. If a caused b, L(a) < L(b). But a smaller number can’t prove cause.

    Hybrid logical clocks stamp the largest wall time heard of, plus a counter. Replay the rider bug.

    The fix

    Dispatch carries PAID’s stamp, (.080, 0), so Node B must stamp above: (.080, 1). RIDER_ASSIGNED wins.

    Office adds tea; the offline rep adds rice. Who was last?

    Neither. Version vectors keep one counter per writer. All entries ≤ means older; mixed means concurrent: keep both and merge.

    Mumbai counted 10 likes, Delhi 13. What’s the real total?

    A G-Counter gives each region its own slot; merging keeps each slot’s maximum: 15. Adding the totals double counts; keeping the larger loses likes.

    Pick the conflict rule per data type

    1. Profile field, “last seen”: last-writer-wins
    2. Likes: a G-Counter. Cart: an OR-Set
    3. Field-sales orders: merge rules plus human review
    4. Wallet balance: one home with consensus, never merged

    Remember

    Wall clocks can’t order writes across machines. Get order from one place, detect concurrency with version vectors, merge per data type.