Skip to main content
PreparationZone Logo
PrepZone

Unique ID Generator

UUID, auto-increment, Snowflake and Twitter IDs — generate billions of unique keys without collisions.

Pass checkpoint below

Read these first

StreamHub generates IDs for streams, clips, chat messages, and payments — billions per month. Auto-increment IDs are simple but leak business metrics and fail across shards. Snowflake IDs give global uniqueness, time ordering, and no central coordinator.

Requirements

What a good ID generator provides

  • Uniqueness: No collisions across all services and regions.
  • Sortability (optional): Time-ordered IDs improve DB index locality and pagination.
  • Generation speed: 10K+ IDs/sec per worker without DB round trip.
  • No single point of failure: Decentralized generation preferred.
  • Compact: 64-bit integer fits in BIGINT; UUID is 128-bit (16 bytes).

Approach comparison

ApproachProsCons
Auto-increment (DB)Simple, sequential, index-friendlySingle-writer bottleneck; leaks count; sharding breaks it
UUID v4 (random)No coordination; 122 bits of randomnessNot sortable; index fragmentation; 16 bytes
UUID v7 (time-ordered)Sortable; no coordinationStill 16 bytes; newer standard
Snowflake (64-bit)Sortable, compact, decentralizedClock skew breaks ordering; needs worker ID assignment
DB sequence per shardSimple within shardGlobal uniqueness needs shard prefix; coordination on shard assign
  • Auto-increment (DB)

    ProsSimple, sequential, index-friendly
    ConsSingle-writer bottleneck; leaks count; sharding breaks it
  • UUID v4 (random)

    ProsNo coordination; 122 bits of randomness
    ConsNot sortable; index fragmentation; 16 bytes
  • UUID v7 (time-ordered)

    ProsSortable; no coordination
    ConsStill 16 bytes; newer standard
  • Snowflake (64-bit)

    ProsSortable, compact, decentralized
    ConsClock skew breaks ordering; needs worker ID assignment
  • DB sequence per shard

    ProsSimple within shard
    ConsGlobal uniqueness needs shard prefix; coordination on shard assign
64-bit Snowflake ID
41 bitstimestamp (ms)
5 bitsdatacenter ID
5 bitsworker ID
12 bitssequence
64-bit IDs: timestamp + datacenter + machine + sequence — sortable and unique.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 3 sections

  • Snowflake layout
  • Worker ID assignment
  • StreamHub ID strategy

Test yourself

Answer these before moving on — recall is what makes it stick.