LESSON 05 · Time failure

Handle clocks that move backward

A backward clock can repeat a timestamp and break both uniqueness and time order.

SYSTEM VIEW

The ID path

Diagram view
Last issued—
Clock reads—
Worker A—

Watch Worker A’s clock step back behind the last ID it issued, then see what a small step, a 20-second step and a clock 40 ms ahead each cost.

  1. Worker A takes each ID’s time from its Clock. It issues IDs in millisecond 104, then 105, and remembers 105 as its last issued millisecond. Real time runs along the lines.

    Issued
    in ms 104 and 105
    Last issued
    T = 105

    What changed

    • IDs at 104
    • IDs at 105
    • Last issued 105
  2. A wall clock can be adjusted. NTP, which aligns machines with time servers, steps the Clock back 1 ms, so it reads 104 again. A virtual machine’s clock can jump the same way after a migration.

    Clock reads
    T = 104
    Last issued
    T = 105
    Steps it back
    NTP, a VM migration

    What changed

    • NTP steps back 1 ms
    • Clock reads 104
  3. The App server asks for an ID. The Clock reads 104, less than the last issued 105. Issuing now could repeat any ID from millisecond 104, and new IDs would sort before older ones, so Worker A doesn’t.

    Could repeat
    any ID from ms 104
    Would sort
    before IDs from 105

    What changed

    • App server asks for an ID
    • 104 < last 105
    • Worker A won’t issue
  4. For a rollback this small, Worker A pauses briefly. The Clock reads 105, then 106; once it has passed 105, Worker A issues the ID at 106. The caller waits a moment, and no ID repeats.

    1 ms back
    pause briefly
    Next ID
    at T = 106

    What changed

    • Worker A pauses
    • Clock reads 105, then 106
    • ID at 106
  5. Replay it with a 20-second step back. A pause doesn’t suit this: every caller would stall for 20 seconds. Worker A stops issuing and alerts. Twitter’s Snowflake took this strict route, refusing until the clock passed its last issued time.

    A pause
    stalls callers 20 s
    Instead
    stop and alert
    Or
    a fresh worker identity

    What changed

    • Clock 20 s behind
    • Worker A stops issuing
    • Alert
  6. The cost is availability: Worker A serves nothing until its Clock passes its last issued time. So the App server turns to Worker B, another worker, and gets its ID there. Callers need other workers to turn to.

    Cost
    availability
    Worker A
    serves nothing

    What changed

    • Worker B
    • App server asks Worker B
  7. Worker A’s region 1 runs 40 ms ahead: R1, created there at T = 200, is stamped 240; R2, created in region 2 at T = 210, is stamped 210 and sorts first. Exact order needs a coordinated sequencer or an ordering protocol; monitor skew.

    Created
    R1, then R2 10 ms later
    Sorted by ID
    R2, then R1
    IDs are
    roughly time ordered

    What changed

    • Worker A in region 1, 40 ms ahead
    • R1 stamped 240
    • R2 stamped 210
    • R2 sorts first

Space plays · ← → step · Home resets

THE IDEA

How it works

A wall clock reports time of day and can be adjusted. NTP (Network Time Protocol, which aligns machines with time servers) may step it backward, and virtual machines can jump after a migration. If a worker’s current millisecond is less than its last issued millisecond, generating with the new value risks reusing an old timestamp and sequence.

Suppose a worker issued IDs at T = 105 and its clock then reads T = 104. Issuing now could repeat any ID it created during millisecond 104. For a rollback this small, pause briefly until the clock passes 105. A pause does not suit a 20-second step, because every caller would stall for 20 seconds. Stop issuing and alert instead, or switch to a fresh worker identity under a carefully bounded policy. Twitter’s Snowflake took the strict route and refused to generate IDs until the clock passed its last issued time. The cost is availability: that worker serves nothing during the wait, so callers need other workers to turn to.

Even when each worker’s clock never moves backward, clocks across regions may differ. If one region runs 40 ms ahead, a record created there can sort after one created elsewhere 10 ms later. Snowflake-style IDs are roughly time ordered, not a strict global chronology. Use a coordinated sequencer or a separate event-ordering protocol if the product demands an exact cross-region order. Monitor clock skew, the gap between clocks, and protect the generator from unsafe time jumps.

DECISION POINT

A machine clock jumps backward by 20 seconds after a correction. Should it continue using the same worker ID and reset its sequence?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Never ignore a backward timestamp under the same worker identity.
  • Approximate sort order is not a strict global event order.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

What does a clock rollback threaten?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.