System Design Problem

Design a Unique ID Generator (Twitter Snowflake)

Commonly Asked By:TwitterMetaMicrosoftAmazon

  • Generate globally unique IDs across a distributed system
  • IDs must be 64-bit integers (fit in a long)
  • IDs must be roughly sortable by time (IDs generated later are larger)
  • Generate IDs with extremely high throughput (100K+ IDs/sec per node)
  • No coordination between nodes required for ID generation
  • IDs should not expose sensitive information (e.g., exact count of objects)

Approaches Compared

ApproachProsCons
UUID (v4)No coordination, simple128 bits, not sortable, poor index performance
DB Auto-IncrementSimple, orderedSingle point of failure, not scalable
DB with RangesScalableRequires coordination to assign ranges
Twitter Snowflake ⭐64-bit, sortable, no coordination, fastRequires clock synchronization
MongoDB ObjectId96-bit, sortableLarger than 64-bit

Snowflake ID Structure (64 bits)

Loading...

Architecture

Loading...