Question #12 of 60•Scaling•Medium• ~3 min read

How do you design a distributed unique ID generator (Twitter Snowflake)?

Executive Takeaway

Deconstruct Snowflake ID bit layouts (Timestamp, Datacenter ID, Machine ID, Sequence) to generate unique, sorted IDs without a central coordinator.

Architectural Breakdown & AnalysisFAANG Aligned

Single database auto-increment keys create severe write bottlenecks and single points of failure in distributed architectures.

Twitter Snowflake 64-Bit Structure

  • 1 Bit (Unused): Reserved sign bit (always 0).
  • 41 Bits (Timestamp): Milliseconds since custom epoch (~69 years lifespan).
  • 5 Bits (Datacenter ID): Up to 32 datacenters.
  • 5 Bits (Worker Node ID): Up to 32 worker nodes per datacenter.
  • 12 Bits (Sequence Number): Up to 4,096 IDs per millisecond per node.

Key Architectural Advantages

  • Zero Network Coordination: Nodes generate unique IDs locally without calling a centralized master service.
  • Naturally Time-Sorted: Leading timestamp bits ensure IDs sort chronologically, preserving B-Tree index insert efficiency.
  • High Throughput: A cluster of 1,024 nodes can generate over 4 million IDs per millisecond.
NOTE — Snowflake vs UUIDv4

UUIDv4 is 128 bits and completely random. Random UUIDs cause severe B-Tree index fragmentation and page splits compared to 64-bit time-ordered Snowflake IDs.

Key Architectural Comparison

ID Generation ArchitectureID Size / TypeGeneration ThroughputMonotonic OrderingSingle Point of Failure
UUIDv4128-bit StringInfinite (Decentralized client-side).No (Random distribution degrades B-Tree indexing).Zero SPOF.
Auto-Increment DB (Ticket Server)64-bit IntegerLimited by DB master write capacity (2k–5k/s).Strictly sequential.Central DB is SPOF.
Twitter Snowflake64-bit Integer10,000+ IDs/ms per worker node.Time-ordered (Timestamp + Machine ID + Sequence).Zero SPOF (Independent nodes).
MongoDB ObjectId96-bit Hex StringHigh (Client-side generation).Time-ordered prefix.Zero SPOF.
Previous QuestionHow do you prevent Cache Stampede (Thundering Herd) in high-traffic systems?Next Question How does Kafka scale consumer groups and handle partition delivery guarantees?

Continue Learning on Being SDE

Explore deep-dive system architecture modules, low-level object-oriented designs, and interview cheat sheets.