Distributed Unique ID Generator — Notes
Requirements
- Globally unique 64-bit IDs.
- Roughly time-sortable.
- 10k+ IDs/s per node, 1M+ aggregate cluster.
- No single point of failure.
Snowflake math
- 4096 IDs/ms × 1000 ms × 1024 workers = 4.2 billion IDs/sec theoretical.
- 41-bit ms gives 2^41 / (365864001000) ≈ 69.7 years from custom epoch.
API
GET /v1/id -> 64-bit integer
POST /v1/ids?count=100 -> batch
Operational concerns
- Worker ID assignment: ZooKeeper sequential ephemeral nodes; release on shutdown.
- Clock: chrony with hard sync; disable
step (use slew).
- Monitoring: emitted IDs/ms, sequence saturation, clock-skew events.
Trade-offs
- Snowflake: short, ordered, needs coord for worker ID.
- UUIDv4: zero coord, 128-bit storage cost, hurts index locality (random inserts in B-trees).
- UUIDv7 / ULID: best of both for modern systems.
- DB sequence: simplest, but central; OK up to ~10k inserts/s.
- Range allocator (Leaf-segment): claim block of 1k IDs at a time → 1k× fewer ZK trips.
Refs
- Twitter Snowflake (2010 blog), Discord "How Discord Stores Billions of Messages",
Meituan Leaf paper, Instagram Engineering "Sharding & IDs", RFC 9562 (UUIDv7).