Designing a Distributed Unique ID Generator
Design a 64-bit distributed ID generator: Twitter Snowflake architecture, B-Tree index locality, and NTP clock drift defenses.
In a monolithic system with a single relational database, generating primary keys is trivial. Databases provide native AUTO_INCREMENT or SERIAL sequences that reliably produce sequential integer IDs (1, 2, 3, 4...).
However, once a system scales horizontally across multiple database shards, multi-region clusters, and microservices, centralized auto-incrementing integers break down:
- A single centralized database instance generating auto-increments becomes a Single Point of Failure (SPOF) and a critical write bottleneck.
- If multiple database shards increment independently, they produce duplicate primary keys unless complex multi-master offsets (e.g., Node 1 generates 1, 3, 5; Node 2 generates 2, 4, 6) are configured, which makes adding new shards operational pain.
- Relying on random UUIDv4 produces 128-bit strings that severely degrade database B-Tree index performance.
To support high-throughput distributed systems, we need a Distributed Unique ID Generator capable of producing billions of globally unique, roughly time-sorted 64-bit integer IDs with sub-millisecond latency and zero cross-network coordination.
01Core concepts and intuition
Before evaluating architectures, let us define the core system requirements for a modern distributed ID generator:
- Global Uniqueness: No two entities across any datacenter or shard may ever receive the same ID.
- 64-bit Numeric Size: The ID must fit into a standard 8-byte signed integer (SQL
BIGINTor Javalong) to minimize index storage overhead. - Roughly Time-Sortable (k-sorted): Newer IDs must be numerically larger than older IDs. This preserves B-Tree indexing performance and allows sorting by creation time without additional timestamp columns.
- High Availability and Low Latency: Generating an ID must take less than 1 millisecond and must not require cross-network consensus (like Paxos or Raft) for every single ID.
Candidate approaches evaluated
Multi-master database auto-increment with step offsets
Configure N database servers where each node increments by N:
- Server 1 generates: 1, 4, 7, 10...
- Server 2 generates: 2, 5, 8, 11...
- Server 3 generates: 3, 6, 9, 12...
- Limitation: Adding or removing a database server requires changing the step offset across all nodes, making horizontal scaling complex. Furthermore, IDs are not time-sortable across different servers.
Universally Unique Identifiers (UUIDv4)
Generate 128-bit pseudo-random numbers (e.g., 550e8400-e29b-41d4-a716-446655440000):
- Advantage: Fully decentralized; zero coordination between nodes; virtually zero collision risk.
- Critical Flaws:
- Storage waste: 128 bits (36 characters as a string) is double the size of a 64-bit integer.
- B-Tree index fragmentation: Standard B-Tree storage engines (like MySQL InnoDB) store primary keys in sorted leaf pages. Because UUIDs are completely random, inserting new rows forces the database to write to random physical disk locations, causing continuous B-Tree page splits, cache churn, and write performance degradation.
Twitter Snowflake (The Industry Standard)
Pioneered by Twitter in 2010, the Snowflake pattern generates 64-bit integers by combining a timestamp, a worker node ID, and a local sequence counter.
Loading the rest of the lesson…