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.
A machine clock jumps backward by 20 seconds after a correction. Should it continue using the same worker ID and reset its sequence?
No. That can reuse prior timestamp/sequence pairs. Pause or fail issuance until safe, or use a controlled identity and rollback strategy with alerts.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Never ignore a backward timestamp under the same worker identity.
- Approximate sort order is not a strict global event order.