// The Design Playbook

Design a unique ID generator

System Design track · 15 min

By the end of this lesson you can
  • Compare UUIDs, a database counter and a ticket server, and say what each one costs.
  • Split 64 bits into timestamp, datacenter, machine and sequence, and calculate what each part allows.
  • Choose a policy for when a machine's clock jumps backwards.

Thousands of machines create IDs for new orders, messages and users, every second, all at once. No two IDs may ever be the same, on any machine, ever.

And you can't ask a central office for permission each time: there is no time for that. Yet this problem is solved in production every day with 64 bits and some careful bookkeeping.

Requirements first
  1. Unique everywhere: no two machines may ever produce the same ID, and there is no central coordinator to stop them.
  2. Roughly sorted by time: newer IDs are bigger numbers than older ones, so database indexes and 'load more' pages stay cheap.
  3. Small and fast: fits in a 64-bit integer, and handles about 10,000 new IDs per second without effort.
The usual options

A UUID is the easy answer: any machine generates one, no coordination needed. But it is 128 bits, twice the size of a 64-bit key, so indexes get heavier. And because the values are random, new rows land all over the database index instead of at the end. That scatters writes, breaks pages apart and loses any time order.

A single database counter (auto-increment) is small and perfectly ordered. But every insert goes through one machine: a bottleneck and a single point of failure.

A ticket server softens this. One database hands out ranges of numbers to many machines, say 1,000 at a time. But that one database is still a box whose death stops everything. Every central option buys simplicity and pays with availability.

The idea

Here is the trick: you don't check that an ID is unique, you build it so it can't be anything else.

Think of the Egyptian national ID number. It starts with the person's birth date, then the governorate code, then a sequence number. The office in Alexandria and the office in Aswan can issue numbers on the same day without ever calling each other, because the governorate code keeps their numbers apart.

The snowflake design does the same with 64 bits. Time, place and a counter split the number space so cleanly that two machines simply cannot produce the same number. Coordination is replaced by structure.

63 bits used — sign bit = 041 bitstimestamp (ms)high bits ⇒ ids sort roughly by creation time5 bitsdatacenterno collisions across regions5 bitsmachine1024 workers mint in parallel12 bitssequence4096 ids per ms per machineone number carrying when, where, and which-in-order — minted anywhere, colliding never
Read the bits left to right: 1 unused sign bit, 41-bit timestamp, 5-bit datacenter, 5-bit machine, 12-bit sequence. Time comes first, so IDs sort by time.
What each part buys you

Timestamp, 41 bits: milliseconds since a starting date you choose. 2⁴¹ ≈ 2.2 trillion milliseconds, and one year is about 31.5 billion milliseconds, so that is about 69 years. Because time sits in the highest bits, newer IDs are always bigger numbers.

Datacenter and machine, 5 + 5 bits: 2¹⁰ = 1,024 generators, each with its own number, and none of them ever needs to talk to another.

Sequence, 12 bits: a counter that restarts every millisecond on each machine. 2¹² = 4,096 IDs per millisecond per machine, so about 4 million per second. You needed 10,000. Uniqueness is no longer something you check; the layout guarantees it.

Try it yourself

Change the datacenter, the machine and the sequence one at a time, and watch which bits change in the final number. Then ask yourself: what would it take for two machines to produce the same ID?

Try it yourself
When the clock goes backwards

The answer to that question: a clock that goes backwards. Machines sync their clocks over the network with NTP, and a correction can step the clock back. Now your timestamp returns to milliseconds you already used, the sequence counter starts again, and duplicate IDs can slip out.

You have three options. Wait until the clock catches up with the last timestamp you used. Refuse to generate IDs until time moves forward again. Or keep using the last timestamp and just keep counting the sequence.

Most production systems refuse and raise an alert. A short pause on one generator is far better than a silent duplicate in your primary key. Whatever you pick, say why.

Test yourself

Why does the timestamp sit in the highest bits of a snowflake ID? And what exactly goes wrong when a machine's clock steps backwards?

Answer in your head first, then check.

Takeaway

Use a UUID when convenience matters more than size and order, a counter when one writer is honestly enough, and snowflake when you need all three properties at once. Whichever you ship, decide your clock-rollback policy before NTP decides it for you.

Do it this week

Find where your system creates IDs today: open the code, don't guess. If it uses random UUIDs, check whether the primary-key index is suffering from scattered inserts. Then sketch a snowflake layout for your own scale: how many machines, how many IDs per millisecond, how many years of timestamp. Ten minutes and one page.

What you can do now
  • Compare UUIDs, a database counter and a ticket server, and say what each one costs.
  • Split 64 bits into timestamp, datacenter, machine and sequence, and calculate what each part allows.
  • Choose a policy for when a machine's clock jumps backwards.

The Design Playbook