UNIQUE ID GENERATOR
Sortable IDs from many machines without coordination
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.
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 fileworkerId = registry.lease() // etcd/ZooKeeper: unique while alive
ts = now()
while (ts < lastTs) ts = now() // wait out a backwards step (or refuse if it is large)
if (ts == lastTs) {
seq = (seq + 1) & 4095
if (seq == 0) while (ts <= lastTs) ts = now() // 4,096 used: wait for the next ms
} else seq = 0
lastTs = ts
return ts << 22 | workerId << 12 | seq 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.
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
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.
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.
“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.