10. Unique ID Generation
10.3 Why does auto-increment break across many servers?
At the bank: Twenty machines all start at 1. Head office receives twenty "token 57"s and can't tell them apart. The obvious fix, one central machine for every branch, means every customer in the country waits on one machine.
In the system: Two ways it goes wrong.
Figure: each shard's counter is unique on its own. Across shards, "order 1003" means two different orders.
- Each database counts on its own. After sharding, shard A and shard B both create order 1003. Any feature that looks across shards (search, analytics, customer support, merging data) now has duplicate IDs.
- One shared counter becomes a bottleneck. If every app server asks one database for the next number, that database is in the path of every write, and if it's down, nobody can create anything.
You also often want to know an ID before the row is saved: to put it on a queue message, return it to the client, or link related rows. Database auto-increment only tells you after the insert.
So distributed systems use one of three families: random IDs (10.4), structured time-based IDs (10.5), or coordinated ranges (10.6).
Memory code: "Many counters repeat; one counter queues."