Unique ID generation in distributed systems is crucial for ensuring consistency, ordering, and avoiding collisions when multiple services or nodes generate IDs independently. An ID generator is a mission-critical system, it must be highly available.
| Approach | Decentralized | Ordered | Portable | ID Size |
|---|---|---|---|---|
| Ticket server | ❌ | |||
| (Low availability, | ||||
| No horizontal scaling) | ✅ (sequence) | ❌ | 64 bit | |
| Partitioned ID space (Multi-master) | ✅ | ❌ | ⚠️ (careful setup) | 64 bit |
| UUID | ✅ | v1: ✅ (timestamp) | ||
| v4, v5: ❌ | ✅ | 128 bit | ||
| Twitter Snowflake | ✅ | ✅ (timestamp) | ⚠️ (careful setup) | 64 bit |
A centralized server is responsible for issuing unique IDs.
The server maintains an atomic counter (e.g., auto_increment)
Each node in a distributed system generates IDs independently, but is assigned a unique range, prefix, or node ID to avoid collisions.
We are only talking about the the generator in a distributed system, not the whole system, after the ID is generated, the ID may be send to another environment which needs it.
1, 4, 7, 10... (ID % 3 == 1)2, 5, 8, 11... (ID % 3 == 2)3, 6, 9, 12... (ID % 3 == 0)Each region generates unique IDs like:
1000001, 1000002, ...2000001, 2000002, ...