UNIQUE ID GENERATOR

Sortable IDs from many machines without coordination

L318 MINSYSTEM DESIGN
PREDICT

Each ID packs (timestamp in ms, worker id, sequence within that ms). A naive worker trusts its clock and issues IDs at 1000–1003 ms. Its clock is then stepped back 2 ms, and it issues three more IDs. What happens?

You want IDs that are unique, 64 bits, roughly sorted by time, and made by many machines with no round trip per ID. Snowflake packs three fields into one integer: timestamp | worker | sequence. The timestamp makes IDs sort by time. The worker id keeps machines apart. The sequence counts IDs within one millisecond on one worker.

Uniqueness rests on two promises. No two live workers share a worker id. No worker reuses a (timestamp, sequence) pair, so it never issues on a clock earlier than its last one, and it waits for the next millisecond when the sequence runs out.

▶ SIMULATION
WORKER W1
not started
ISSUED IDS (41 ts | 10 worker | 12 seq)
issued 0 · distinct 0
duplicates 0
WORKER W2
not started
worker id registryfrom config, unchecked: —
true timeT = 999 ms since epoch · largest gap between an ID's timestamp and T: 0 ms
last event—
✓ no duplicate IDs✓ each worker's IDs strictly increase
SCHEDULE
01[DESIGN]naive generator: worker id from config, trust the clock
02[W1]W1 boots; config file says worker_id = 1
03[W2]W2 boots; config file says worker_id = 2
04[W1]t=1000 ms: W1 issues 3 IDs
05[W2]t=1000 ms: W2 issues 3 IDs
06[W1]t=1001 ms: W1 issues 3 IDs
07[W2]t=1001 ms: W2 issues 3 IDs
08[W1]t=1002 ms: W1 issues 3 IDs
09[W2]t=1002 ms: W2 issues 3 IDs
10[W1]t=1003 ms: W1 issues 3 IDs
11[W2]t=1003 ms: W2 issues 3 IDs
12[W1]t=1004 ms: W1 issues 3 IDs
13[W2]t=1004 ms: W2 issues 3 IDs
14[DESIGN]report
Start of naive, calm day scenario. 14 steps.
ts = now()
if (ts == lastTs) seq = (seq + 1) & 4095   // wraps to 0 inside the same ms
else seq = 0                               // clock went back? use it anyway
lastTs = ts
return ts << 22 | WORKER_ID << 12 | seq    // WORKER_ID from a config file

Watch ISSUED IDS and the invariant strip. NAIVE, CALM DAY is unique: nothing went wrong yet. NAIVE + CLOCK STEP reissues three IDs from 1002 ms. NAIVE + COPIED CONFIG duplicates every ID W2 makes. LEASE + WAIT faces the copied config, the clock step and a 5,000-ID burst, and keeps every ID unique. It leases worker id 2, waits 1 ms for the clock and 1 ms for sequence overflow.

BREAK IT NOT YET

The naive generator runs on a calm day and every ID is unique. Inject one fault so that two IDs come out the same.

GOAL: at least one duplicate ID

FIX IT NOT YET

Make nextId safe. The grader runs it through a steady clock, a burst of 6,000 calls inside one millisecond, a clock that jumps back 10 ms, and two workers at the same moment. IDs must never repeat and each worker's IDs must strictly increase. now() reads the clock; calling it again lets time pass.

Mental model:

a Snowflake ID is unique because the triple (timestamp, worker, sequence) never repeats. That is three promises: the worker id is leased, not configured; the timestamp never goes backwards, whatever the wall clock says; and the sequence never wraps inside a millisecond. Break any one and the generator emits duplicates silently. The only symptom is a key violation far away.

IN PRODUCTION

Twitter's Snowflake, Discord and Instagram IDs, Sonyflake, database-sharding keys, order and message IDs, ULID and UUIDv7 (the same time-first idea in 128 bits). The symptoms: rare duplicate-key errors after a deploy or a clock correction, and IDs that sort slightly out of order across machines.

IN INTERVIEWS

“Design a unique ID generator for a distributed system.” On the whiteboard: requirements first (unique, 64-bit, sortable by time, 10,000+ IDs a second, no single point of failure). Weigh the options: auto-increment (one bottleneck), UUIDv4 (128 bits, not sortable), a ticket server (a round trip per ID), Snowflake. Then the estimates: 41 bits of milliseconds is about 69 years, 10 bits is 1,024 workers, 12 bits is 4,096 IDs per millisecond per worker. Then the failures you would mention: two workers with one worker id, a clock stepped backwards (a VM resume or a large correction; configure NTP to slew small offsets rather than step them), and sequence overflow in a burst. Fix them with leased worker ids, waiting out regressions (or refusing on a big one), and spinning to the next millisecond. Close with the caveat: IDs are only roughly time-sorted across workers, because their clocks differ by milliseconds.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: SEARCH AUTOCOMPLETE →