Notesdistributed🌱 seedling

Unique ID Generation

Also: Snowflake ID · UUID · UUIDv7 · UUIDv4 · ULID · Distributed ID Generation · Unique ID

Schemes for issuing IDs that never collide, trading coordination against order. Time-first layouts (Snowflake, UUIDv7) give uncoordinated, roughly time-sorted IDs whose order is only as good as clock sync.

Why it exists

A single auto-increment counter is perfectly ordered but makes one node a bottleneck and a single point of failure for every insert.

How it works

  • Auto-increment: one counter, one node. Ordered, compact, coordinated.
  • UUIDv4: 122 random bits. No coordination, no order, 128 bits.
  • Snowflake (Twitter, 2010): 64 bits = unused sign bit + 41-bit ms timestamp (custom epoch, ~69 years) + 10-bit worker ID (1,024 workers) + 12-bit sequence (4,096 per ms per worker). Refuses to issue IDs if its clock goes backwards.
  • Instagram (2012): 41 bits time + 13 bits logical shard + 10 bits sequence, generated in PostgreSQL.
  • UUIDv7 (RFC 9562, May 2024): 48-bit Unix ms timestamp, then version, variant and 74 random bits; the RFC recommends it over v1/v6.

Trade-offs

Scheme Gains Costs
auto-increment small, strictly ordered single coordination point
UUIDv4 zero coordination random inserts scatter across B-Tree pages
Snowflake / UUIDv7 uncoordinated, append-friendly index inserts reveal creation time; order bounded by clock skew; newest range is a Hot Spot under Range Partitioning

In practice

  • PostgreSQL 18 (2025) generates UUIDv7 natively with uuidv7().
  • Twitter promised Snowflake IDs sorted within about a second, aiming for tens of milliseconds: a clock-sync budget, not a causal guarantee.

Pitfalls

  • Treating time-sorted IDs as causal order. A fast-clocked worker's ID sorts after a later one from a slow worker.
  • Using time-based IDs as unguessable secrets; use random IDs for security tokens.

Recall

These cards are also in your review deck, scheduled with spaced repetition.