Unique ID Generation
How distributed systems create IDs that never collide, from auto-increment and UUIDs to time-ordered Snowflake IDs.
Why IDs are a design problem
Every row, message and file needs an ID (identifier) that no other item shares. On one database this is trivial. With many servers creating records at once, two of them must never pick the same ID, and ideally nobody should wait on a central service.
Properties you may want:
- Unique across all machines. This one is mandatory.
- Sortable by time. Newer IDs are larger, so "latest 20 items" is cheap.
- Compact. A 64-bit number is half the size of a 128-bit UUID in every index and foreign key.
- Not guessable. Sequential public IDs let anyone count your orders or scrape
/users/1,/users/2, ... - Generated locally. No network call per ID.
No scheme gives all five. The options below trade them off.
1 of 8