How would you design a unique ID generator at scale?
0 views
95 of 106
1
The problem
Question presented to candidate:
"Every tweet, order and message in our system needs an ID. We write about 20,000 rows a second across many database shards, peaks are ten times that, and we want IDs that are unique without a central bottleneck and roughly sortable by creation time. How would you generate them?"
What a strong answer should cover:
Requirements: uniqueness across machines, size (64-bit fits a BIGINT, 128-bit does not), time-sortable (k-sorted) or not, throughput, no single point of failure, whether IDs may leak information (volume, creation time)
Options and trade-offs: database auto-increment, ticket servers (Flickr), range allocation, UUIDv4 (random, 128-bit, poor index locality), UUIDv7 (RFC 9562, time-ordered, 128-bit), Snowflake (64-bit: timestamp + machine + sequence)
Snowflake bit math: 41 bits of milliseconds last about 69 years from a custom epoch, 10 bits give 1,024 workers, 12 bits give 4,096 IDs per ms per worker
Hard parts: sequence overflow within a millisecond (wait for the next ms), clock moving backwards (refuse or wait), unique worker ID assignment (config, or leases from a coordination service), and IDs only k-sorted across machines
Practical details: IDs above 2^53 lose precision in JavaScript, so APIs send them as strings
Clarifying questions expected:
Must IDs be 64-bit integers, or is a 128-bit value acceptable?
Do they need to be sortable by time, and is it acceptable that they reveal creation time?
How many generating machines, and how is a machine identity assigned?
Code / implementation expected: Yes โ a Snowflake-style generator; the example runs one on a fake clock through sequence overflow, a clock rollback and a duplicate worker ID.
system-design
case-study
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Developers preparing for system design interviews. You know what a primary key is; no distributed-systems background assumed.
Difficulty: Hard
Verified with: Node 22.22, Python 3.12, Go 1.24 and Java 21 (all four print identical output).
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the exact words an interviewer expects. All behaviour shown comes from running the example; the Verified section quotes the full output.
1. Why This Even Matters โ A Story First
A city has one office that hands out house numbers. Every builder must queue there before putting up a house. It works for a village; for a city it is a bottleneck, and when the office closes for a day, nobody can build.
So the city changes the rule: a house number is the **date and minute it was registered, p
The solution is hidden so you can try first
14 min read ยท 16 sections
4
Read the code
Snowflake IDs on a fake clock: layout, sequence overflow, clock going backwards, ordering, duplicate worker IDs