The whole course ▾

System Design · Lesson 23 of 39 · Week 5

Unique IDs at scale

Updated 22 Oct 2026 · By Akshay, software engineer, India · 5 min read

QuickBite's orders now live on twenty shards, and two of them just created order number 5,001 at the same moment. Today: unique IDs.

The video for this lesson is coming soon.

New System Design videos go up at 6 pm. Subscribe on YouTube so you don't miss it.

Got a doubt? Ask in the comments under the video.Subscribe

QuickBite in this lesson

Order numbers across QuickBite's shards.

QuickBite is the food delivery app we grow through the course, from one server to millions of orders.

Two orders, one number

Two orders, one number
Shard router
Shard
3: next ID 5,001
Shard
7: next ID 5,001
new order
new order
two order 5,001s

In lesson twelve, QuickBite's orders were split across shards. Each shard is its own database, with its own counter. That's a problem nobody noticed, until today.

Every database can number rows for you: one, two, three. That's called auto-increment, and with one database it's perfect. But QuickBite's orders now live on twenty shards, behind the shard router, and each shard counts on its own. Shard three creates order five thousand and one. At the very same moment, shard seven creates its own order five thousand and one.

Now two different orders share the same number. A support agent searching for order five thousand and one finds two. A refund goes to the wrong customer. Everything that's keyed by order ID, Kafka events, cache entries, receipts, gets confused. And auto-increment leaks business numbers too: place two orders a day apart, subtract, and you know how many orders QuickBite takes in a day. Sequential IDs also let anyone guess other orders' numbers, which is a security risk.

What a good ID needs

What a good ID needs
A good ID
Unique across all machines
Made without asking anyone
Sorts by time, roughly
Fits in 64 bits
THE TENSION
Nocoordination,yet
neveraclash,andintime
order:eachschemegives
upsomething.

Before choosing a scheme, decide what we want from an ID. There are four things.

First, unique across every machine, forever. Second, made locally, without asking a central service for every new order, so it's fast, and never a single point of failure. Third, ideally it sorts by creation time, so newest-first queries, and the B-tree indexes from lesson ten, stay efficient. And fourth, small: sixty-four bits fits an ordinary integer column, and keeps every index compact.

Those wishes pull against each other. The easiest way to never clash is one central counter, but that's coordination on every order. The easiest way to avoid coordination is randomness, but random IDs don't sort. Every scheme we'll see makes a different trade, and choosing well is what the interviewer is listening for.

UUIDs

UUIDs
UUID VERSION 4
128bits,122ofthemrandom:
550e8400-e29b-41d4-a716-446655440000.
The costs
Twice the size of a 64-bit number
Random: no time order
Inserts scatter across the index

The quickest answer: random IDs, so large that two machines will never pick the same one.

A UUID, version four, is a hundred and twenty-eight bits, and a hundred and twenty-two of them are random. Any machine can make one at any moment, without asking anyone, and the chance of two colliding is so small you can ignore it. You'd need to make a billion every second for about eighty-five years before a single collision became likely.

But there are costs. They're twice the size of a sixty-four-bit number, in every table and every index that stores them. They're random, so they don't sort by time. And because each new ID lands at a random spot in the B-tree, inserts scatter across the whole index instead of adding to the end, which slows down write-heavy tables. Newer UUIDs, version seven, put the time first to fix exactly that. Still, for many systems, UUIDs are a perfectly good answer.

Snowflake IDs

Snowflake IDs
Order server
machine 12
Order server
machine 13
Orders
shards
ID made locally
ID made locally
One 64-bit ID
41 bits: milliseconds since a start date
10 bits: machine ID, up to 1,024
12 bits: sequence, 4,096 per millisecond
= an ID that sorts by time
WHY IT WORKS
TimefirstsoIDssort;a
machineIDsomachines
neverclash;asequence
withineachmillisecond.

Twitter needed small, sortable IDs at enormous scale, and built a design so good it's now used everywhere: Snowflake.

A Snowflake ID packs three things into sixty-four bits. Forty-one bits of milliseconds since a chosen start date: enough for about sixty-nine years. Ten bits of machine ID, for up to one thousand and twenty-four machines. And twelve bits of sequence: a counter for IDs made in the same millisecond on the same machine, up to four thousand and ninety-six. The time comes first, so the IDs sort by time.

Two machines can never produce the same ID, because their machine bits differ. One machine never repeats itself, because time only moves forward, and the sequence counts within each millisecond. And nobody waits on a central service: each machine can make over four million IDs a second, from memory. Instagram, Discord and many others use their own versions of this idea.

On QuickBite's board, each order server gets a machine number when it starts, say twelve and thirteen. When an order comes in, the server makes its ID right there, in memory: the current millisecond, its machine number, and the next sequence number. Then the order goes to whichever of the orders shards the router picks. No counter to ask, and the ID itself tells you roughly when the order was placed.

Other ways, and the catches

Other ways, and the catches
Ticket servers
->
A central counter hands out blocks
->
Small numbers, roughly in order
->
One more service to keep alive
Snowflake's catches
Clocks can jump backwards
Machine IDs must be assigned
Time order is only rough

There's one more classic option, and Snowflake has catches of its own.

A ticket server is a central database that does nothing but count. Flickr famously ran two, one handing out odd numbers and one even, so either could fail. Clients grab blocks of a thousand numbers at a time, exactly like the ID counter in mock interview one. The IDs are small, and roughly in order. But it's one more service to keep alive, and to scale.

And Snowflake's catches. If a machine's clock jumps backwards, say after a time correction, it could repeat IDs, so the generator must notice and wait until time catches up. Machine IDs must be assigned carefully, often from a coordination service, so two machines never share one. And the order is only rough: two IDs made on different machines in the same millisecond can come out either way round.

In the interview

In the interview
SAY THIS IN THE INTERVIEW
Snowflake-style64-bit
IDs:time,machine,
sequence.Uniquewithout
coordination,andsortable.
Follow-ups to expect
->
Why not auto-increment?
->
Why not UUIDs?
->
Should customers see these
IDs?
THE COMMON MISTAKE
Keeping
auto-incrementIDs
afteryou'vesharded.

IDs come up in nearly every design with shards, and as a full question: design a unique ID generator.

Say it like this: Snowflake-style sixty-four-bit IDs, with time, machine and sequence bits, so every machine makes unique IDs without coordination, and they sort by time. Then mention the clock: if it goes backwards, the generator waits. That one detail shows you've thought it through.

Then expect these. Why not auto-increment? It clashes across shards, and leaks how busy you are. Why not UUIDs? You can, but they're twice the size, and random ones don't sort. And should customers see these IDs? Often you show a separate short code, like a booking reference, so internal IDs stay internal.

The common mistake: sharding the data, and keeping each shard's auto-increment IDs. Duplicates are guaranteed, and they surface at the worst moment: a refund, a support call, or a merge of two systems.

Quick check

Quick check
?
Snowflake has 12 sequence bits.
How many IDs can one machine
make in one millisecond?
4,096: two to the power twelve.
?
Why do random UUIDs slow
down inserts into a big
B-tree index?
Random placement: each new ID
lands somewhere new, so inserts
scatter instead of appending.
Remember
Auto-increment breaks on
shards
UUIDs: easy, big, unordered
Snowflake: time, machine,
sequence
Quick check

Snowflake has 12 sequence bits. How many IDs can one machine make in one millisecond?

Show the answer

4,096: two to the power twelve.

Quick check

Why do random UUIDs slow down inserts into a big B-tree index?

Show the answer

Random placement: each new ID lands somewhere new, so inserts scatter instead of appending.

Remember: auto-increment breaks once you shard. UUIDs are easy, but big and unordered. And Snowflake packs time, machine and sequence into sixty-four bits: unique, coordination-free, and sortable. Tomorrow, the last lesson of the week: seeing inside all of this.

Practise with flashcardsYour progress stays on this device.
New System Design videos at 6 pm

Lessons on weekdays, the week's revision on Saturday, a mock interview on Sunday.