LESSON 04 · Local algorithm

Generate safely within one millisecond

The worker increments a sequence for the current millisecond and waits when it exhausts that slot.

SYSTEM VIEW

The ID path

Diagram view
Millisecond—
Sequence—
Waited—

Watch one Worker spend the last sequence value of a millisecond, wait for the next, then stay unique across threads and a restart.

  1. The Worker keeps its last millisecond and sequence in memory: millisecond 1,000, sequence 4,094. The App server asks for an ID. The Clock still reads 1,000, so the Sequence goes up by one to 4,095, the last value.

    Same millisecond
    sequence + 1
    New millisecond
    sequence back to 0
    This ID
    1000·4095

    What changed

    • Sequence 4,094 → 4,095
    • ID 1000·4095
  2. The next call finds the Clock still at 1,000 and all 4,096 sequence values used. Wrapping to 0 would repeat millisecond 1,000, sequence 0, an ID already issued. So the Worker waits for the next millisecond, or rejects with a defined “slow down” error.

    Used
    0 to 4,095, all 4,096
    Wrap to 0
    repeats 1000·0
    Or reject
    a “slow down” error

    What changed

    • All 4,096 values used
    • Never wrap to 0
  3. The Worker spins until the Clock reads 1,001. A new millisecond resets the Sequence to 0, so the call gets millisecond 1,001, sequence 0. The wait is under a millisecond, so waiting usually beats failing.

    Waited
    under 1 ms
    This ID
    1001·0

    What changed

    • Clock 1,000 → 1,001
    • Sequence back to 0
    • ID 1001·0
  4. At 25,000 IDs a second shared across a fleet, the average is 25 per millisecond, far below 4,096, so a Worker rarely runs out. Count exhaustions anyway: a rising count suggests traffic has concentrated on too few workers.

    Fleet
    25,000 IDs/s
    Per ms
    about 25 of 4,096
    Rising count
    too few workers

    What changed

    • Fleet at 25,000 IDs/s
    • About 25 of 4,096 per ms
  5. In millisecond 1,002, two App server threads ask at once. A lock, an atomic step or a single generator loop hands out one pair per call: one thread gets millisecond 1,002, sequence 7, the other sequence 8. Unguarded, both could take sequence 7.

    Thread 1
    1002·7
    Thread 2
    1002·8
    Guard
    lock, atomic step, loop

    What changed

    • Two threads in 1,002
    • One pair per call
    • IDs 1002·7 and 1002·8
  6. The Worker restarts within millisecond 1,002 under the same worker ID. Starting again at sequence 0 would repeat millisecond 1,002, sequence 0. So it waits past 1,002 and issues millisecond 1,003, sequence 0. Keeping the last-issued time, or leasing a new worker ID, also works.

    Risk
    1002·0 again
    Fix
    wait past 1,002
    Or
    a new worker ID

    What changed

    • Worker restarted in 1,002
    • Waited past 1,002
    • ID 1003·0
  7. At low traffic most IDs carry sequence 0, so their low bits barely vary. Pick ID mod 2: nearly every row lands on Shard 0. Pick hash(ID) mod 2: rows split about evenly between Shard 0 and Shard 1. Hash the ID first.

    Quiet traffic
    sequence mostly 0
    ID mod 2
    nearly all to Shard 0
    Fix
    hash the ID first

    What changed

    • Clock
    • Sequence
    • Shard 0 and Shard 1
    • A choice: ID mod 2 or hash
    Shard by

Space plays · ← → step · Home resets

THE IDEA

How it works

Each worker keeps its last timestamp and sequence in memory. If the clock advances, reset the sequence to zero; if it remains in the same millisecond, increment it. When 4,096 sequence values have been used, wait for the next millisecond or reject with a defined backpressure response, meaning an explicit “slow down” error the caller can act on. Wrapping to zero in the same millisecond would repeat an ID.

Trace one worker. At millisecond 1,000 it has issued sequence 4,094. The next call takes 4,095, the last value. The call after that finds the range spent, spins until the clock reads 1,001, and takes sequence 0. The wait is under a millisecond, so waiting usually beats failing. At 25,000 IDs per second shared across a fleet, an average of 25 per millisecond, exhaustion should be rare; a rising count suggests traffic has concentrated on too few workers.

Make generation thread-safe within the process. A lock, atomic state transition, or a single generator loop must ensure two threads cannot observe the same timestamp and sequence pair. If the process restarts within the same millisecond under the same worker ID, it may reuse a sequence; preserve last-issued time where needed, wait beyond the prior slot, or lease a new worker identity.

One side effect: at low traffic most IDs carry sequence 0, so their low bits barely vary. Sharding by ID modulo a power of two would then send almost everything to one shard, so hash the ID first.

DECISION POINT

A worker has issued all 4,096 sequence values in the current millisecond. What must it avoid?

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

Keep in mind

  • A full sequence range requires waiting or backpressure, not wraparound.
  • Worker state and restart behavior are part of uniqueness.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

When does a worker reset its sequence?

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.