Unique ID Generator
COMMONUnique ID generator design is asked at Twitter, Discord, and Meta because it packs distributed-systems judgment into the smallest possible surface: 64 bits. It is the Snowflake design behind every tweet, Discord message, and Instagram photo ID. You will budget the 64 bits (41 timestamp + 10 worker + 12 sequence), handle the clock that steps backward without ever minting a duplicate, assign worker IDs with leases and fencing deadlines, and beat the real bottleneck: RPC overhead, not bit arithmetic: with batching and library embedding.
- Budget 64 bits (1+41+10+12) and explain what each field buys: and how Sonyflake tunes the split
- Handle backward clock steps with the spin/refuse/borrow ladder: loud beats wrong
- Assign worker IDs with ephemeral leases and fencing deadlines that survive GC pauses
Visual Solutions
Step-by-step animated walkthroughs with capacity estimation, API design, database schema, and failure modes built in.
Cheat Sheet
Key concepts, trade-offs, and quick-reference notes for ID Generator. Everything you need at a glance.
Anti-Patterns
Common design mistakes candidates make. Wrong approaches vs correct approaches for each trap.
Failure Modes
What breaks in production, how to detect it, and how to fix it. Detection metrics, mitigations, and severity ratings.
Start simple. Build to staff-level.
“I would design a Snowflake-class generator: 64-bit IDs from 41 timestamp bits (69.7 years from a custom epoch), 10 worker bits (1,024 coordination-free minters), and 12 sequence bits (4,096 per millisecond per worker, a 4.2 billion/sec cluster ceiling). Demand of 10M IDs/sec is 0.24% of the bits: the real ceiling is RPC, so batch-500 calls and library embedding serve the platform on 12 workers. Clocks that step backward hit a policy ladder: spin-wait under 20ms, refuse and page beyond, because a paused generator beats a duplicating one. Worker IDs come from ephemeral leases with fencing deadlines that survive GC pauses, and a 1-in-1,000 SETNX canary continuously proves uniqueness.”
The 64-Bit Budget
STANDARD1+41+10+12: each field buys a guarantee; the split is a dial, not a constant
Core ID DesignWhy Not UUIDs
STANDARDRandom keys fragment B-trees ~10x; UUIDv7 fixes locality, Snowflake fixes size too
What is a Unique ID GeneratorClock Backward
TRICKYSpin-wait / refuse / sequence-borrow: paused fails loudly, duplicating corrupts quietly
Core ID DesignWorker ID Assignment
TRICKYEphemeral leases with fencing deadlines, or StatefulSet ordinals: never hand-config
Replication and Fault ToleranceK-Sortability
EASYBuys locality, cursors, shard merges, range archival, extractable timestamps: not causality
High Level System DesignSequence Overflow
STANDARD4,096/ms then spin to next ms; the real ceiling is RPC: batch or embed
Capacity EstimationTicket Servers and Ranges
STANDARDGrant ranges, mint locally: density and clock-freedom vs ordering and decentralization
High Level System DesignCustom Epochs
EASY2^41 ms = 69.7 years from YOUR epoch: the one parameter you can never rotate
Monitoring and Complete System