Back to blog

Five servers, one counter: how do you hand out unique IDs?

Thắng Đoàn
Thắng Đoàn

FotoFlow has 50,000 users and three shards. This morning two orders shipped with the same id: each shard is its own database with its own AUTO_INCREMENT counter, and both handed out 1004. A link, a cache entry, a refund: each could mean either order. The counter was only ever unique inside one machine.

Way 1: one counter for everyone

Move the counter out of the shards into a small service whose only job is handing out the next number: a ticket server. Uniqueness is back and ids still sort by time, but every insert costs an extra round trip, and if the ticket server is down nobody can create anything. Flickr ran two ticket servers for years, one odd numbers, one even, so either could fail.

Ask less often: each app server reserves a whole block of 1,000 ids in one call and hands them out from memory. One round trip per thousand inserts, and the app keeps working for a while if the ticket server is down. The price: ids no longer follow creation time across servers, and a crashed server takes the rest of its block with it. Gaps are harmless. Never promise that ids are gap-free or strictly ordered, except for legal invoice numbers, which get their own deliberate counter.

Way 2: pick a number nobody else will

Drop the coordination. A UUID v4 is 122 random bits: any server, phone or script can make one, with nobody to ask. Collisions? You would have to generate a billion per second for about 86 years to reach a 50 percent chance of a single repeat. Uniqueness has to hold across the whole system, not just inside one machine, and UUIDs deliver it with zero coordination.

The cost hides in the index. The primary key lives in a B-tree sorted by id. Counter ids always land on the last page, which stays in memory. Random ids land on a random page, every time. Random UUIDs land on a random index page every time; once the index outgrows memory, insert throughput can drop several-fold. This bites hardest where the table is stored in primary-key order, like MySQL's InnoDB.

UUID v7 fixes it: 48 bits of milliseconds first, then random bits. UUID v7 leads with a timestamp, so inserts return to the right-hand edge of the index and sorting by id means sorting by creation time. Keep version 4 for tokens that must be impossible to guess.

Way 3: Snowflake

Twitter needed ids that fit in 64 bits, sort by time, and mint tens of thousands per second with no central counter. Snowflake packs three fields into one number: a millisecond timestamp, the machine id of the generator, and a sequence counting up within the millisecond. Each machine mints 4,096 ids per millisecond; Discord and Instagram use the same idea with different bit splits.

Snowflake trusts the clock. When time sync sets a fast server back, that server would stamp ids with milliseconds it has already used: duplicates. The guard is to remember the last timestamp issued and wait until time catches up. Assign machine ids from one place, never by hand, because duplicate machine ids break uniqueness silently.

What ids give away

A competitor orders on Monday, id 1004, and on Tuesday, id 1250: your daily volume is public. Sequential ids are also walkable: change /invoices/1004 to /invoices/1005 and see what comes back. Ids leak volume and timing; an unguessable id is not access control. Show a separate random public id, and check authorization on every request regardless.

What this does not solve

Ids are now safe across machines, but the shards holding those rows sit in two regions, and the network between them is not always up. What the copies should do when it breaks is the next post.

Your primary keys today: counter, UUID v4, or something time-prefixed, and do you know why?
If someone read your sequential ids for a week, what exactly would they learn?

One counter, five servers: pick two.

Next in the series: When the network splits, what does your database give up?

Sources

Wikipedia: Snowflake ID

Instagram Engineering: Sharding IDs at Instagram

Designing Data-Intensive Applications

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.