Unique ID Generation at Scale
Auto-increment works great on one database. The moment data is sharded, something else has to guarantee uniqueness across all of them.
Advanced
| Requirement | Why it matters |
|---|---|
| Uniqueness | Two different events must never receive the same ID, even generated by different servers at the same instant. |
| High throughput | A large system can need to generate many thousands of IDs per second. |
| Availability | ID generation can't have a single point of failure — if it's down, nothing that depends on an ID (placing an order, for instance) can proceed. |
| Reasonably compact | A 64-bit numeric ID is far cheaper to index and compare than, say, a long string. |
m ID-generating servers increments by m, starting from a different offset, so no two servers ever produce the same number without needing to coordinate on every request.
Server 1 generates: 1, 4, 7, 10, ...
Server 2 generates: 2, 5, 8, 11, ...
Server 3 generates: 3, 6, 9, 12, ... (m = 3 servers)
m) risks duplicate IDs unless handled carefully.
64-bit layout (illustrative):
[ 41 bits: milliseconds since a custom epoch ][ 10 bits: worker ID ][ 12 bits: sequence within that millisecond ]
| Need | Reasonable choice |
|---|---|
| Simple, fast, no ordering requirement | UUID |
| Compact IDs, moderate throughput, simple to reason about | Range handler |
| Roughly time-sortable IDs at high throughput | Snowflake-style ID |
| Strict global ordering, willing to pay for specialized infrastructure | TrueTime-style approach |