Design a unique-ID generator (Twitter Snowflake)
A distributed unique-ID generator (Twitter Snowflake) produces 64-bit, roughly time-sortable IDs without coordination on every call. It's a building block for sharded databases and event ordering.
Infrastructure, 8 components, about 25 min
1. Scope
In scope
- • Generate globally unique 64-bit IDs
- • Roughly time-ordered
- • High throughput, no per-ID coordination
- • Avoid collisions across nodes
Out of scope
- • UUID/ULID comparison deep-dive
- • Database auto-increment
- • Cryptographic randomness
2. Functional requirements
Must have
- • Unique IDs across all nodes
- • Monotonic-ish (time-sortable)
- • High throughput per node
- • No central bottleneck per request
Nice to have
- • Compact (fits in a 64-bit int)
- • Embeddable timestamp
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • 64-bit layout: 41 ts + 10 worker + 12 seq
- • 12-bit sequence = 4096/ms
- • Worker ids leased
- • Clock synced via NTP
Derived numbers
5. API design
Return a new unique, time-sortable ID
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
The 64-bit layout
Sign bit (0) + 41-bit millisecond timestamp (relative to an epoch, ~69 years) + 10-bit worker id (1024 nodes) + 12-bit per-millisecond sequence (4096 ids/ms). Concatenated, this is unique as long as each worker has a distinct id and the sequence never overflows within a millisecond. Because the timestamp is the high bits, IDs sort roughly by creation time.
Why not a central counter or UUID
A single auto-increment counter is a bottleneck and SPOF. Random UUIDs (v4) are unique but 128-bit and not sortable, hurting database index locality. Snowflake gives compact, sortable, coordination-free IDs — ideal as primary keys in sharded systems.
Worker id assignment
Two workers with the same machine id would collide. Assign ids via a lease from ZooKeeper/etcd on startup; release on shutdown. This is the only coordination, and it happens once per process, not per ID.
Clock skew
If a node's clock moves backward (NTP correction), it could reissue timestamps and risk duplicates. The standard guard: refuse to issue IDs until the clock catches up to the last-seen timestamp, and alert. Keep clocks tightly synced with NTP.
9. Bottlenecks
- Sequence overflow within 1ms → wait for next ms (rare at 4096/ms)
- Clock skew → refuse-and-wait guard
- Worker id reuse → lease via ZooKeeper
10. Trade-offs
Snowflake-style or UUID
Choose: 64-bit time-ordered ids: 41-bit milliseconds, 10-bit worker, 12-bit sequence.
Instead of: UUIDv4: no coordination at all, but 128 bits and random order, which fragments B-tree indexes. UUIDv7 is time-ordered and is a good modern alternative.
Fits a BIGINT, sorts by time, and generates locally at millions per second.
Worker id assignment
Choose: Lease worker ids from ZooKeeper or etcd with a TTL; the node stops generating if it loses the lease.
Instead of: Static config: simple, but duplicate ids if two nodes are misconfigured with the same number.
Duplicate worker ids are the main way Snowflake produces duplicate ids.
Handling clock going backwards
Choose: Refuse to generate (or wait) until the clock passes the last timestamp used.
Instead of: Ignore it: can produce duplicate ids.
Uniqueness is the one guarantee the service must never break.
11. Failure modes
NTP steps the clock back by 2 s
Possible duplicates.
Mitigation: Track last timestamp in memory, block or error until it is passed, and configure NTP to slew rather than step.
More than 4,096 ids in one millisecond on one node
Sequence overflow.
Mitigation: Spin until the next millisecond; add workers if this is common.
Coordination service down
New nodes cannot get worker ids.
Mitigation: Existing leases keep working; leases are long relative to outages.
12. What interviewers probe
Answer each one yourself before opening it.
When does the 41-bit timestamp run out?Show a strong answer
2^41 ms ≈ 69.7 years after the custom epoch; pick the epoch near launch.
Are the ids strictly increasing?Show a strong answer
Only per worker. Across workers they are k-sorted: roughly time-ordered within clock skew.
What does an id leak?Show a strong answer
Creation time and roughly how many ids a worker issues; avoid exposing them where volume is sensitive.
Do you need a service at all?Show a strong answer
Often no: embed the generator as a library in each service with a leased worker id.
Sign in to track this guide in your curriculum progress.