AlgoViz

Unique ID generator

medium 28 min

Give every record its own number, from many servers at once.

Remember it

The few things worth knowing cold, and how to make them stick.

Memory hook

SNOW: Sortable, No coordination, One machine one number, Works at scale.

Raffle sellers: give each a different book, or let each write the time, their letter and a small number.

Cheat sheet

Snowflake
1 + 41 + 10 + 12 bits
Life
69.7 years from the epoch
Per machine
4,096 a millisecond
Machines
1,024
UUIDv7, ULID
48 bits time, then random
ObjectId
4 + 5 + 3 bytes
Random clash
n² / (2 × 2^bits)

Spot it when you see

  • Several writers.
  • IDs needed before the insert.
  • Sort by newest.
  • Short links.

Tricks that stick

  • Time + place + person + number: like a raffle ticket.
  • A library book number: year added, city, building, shelf number. Unique everywhere and it tells you where it came from.
  • Passport number: one for each piece of data, valid everywhere.

Do not forget

  • Two processes with one machine number.
  • Ignoring a clock that went back.
  • IDs as secrets.
  • IDs as text.
  • Expecting exact order across machines.