Lesson 11 of 16
10. Unique ID Generation

Memory card and where the analogy breaks

3 min read+10 XP

The memory card (tear this page out)

#QuestionMemory code
10.1A good ID?Unique always; then pick from fast, sorted, small, secret
10.2Auto-increment?One machine, one counter: perfect, until there are two
10.3Why it breaks?Many counters repeat; one counter queues
10.4UUIDs?v4 is a raffle ticket; v7 is a raffle ticket with a timestamp on the front
10.5Snowflake?Time, machine, count: stamp all three and nothing can clash
10.6Ticket servers?Give each branch a book of numbers
10.7Clock skew?Time-based IDs trust the clock, so never trust a clock that runs backwards
10.8Base62?Same number, fewer characters: digits plus both alphabets
10.9Guessable or not?Sequential inside, random outside, and always check who's asking

The whole chapter in one line: One counter until you can't; then stamp time and machine, or hand out books of numbers, and keep the public ones random.

Where the analogy breaks

  • A bank token is thrown away after the visit. An ID lives forever in databases, logs, URLs and other systems, so changing the scheme later is very hard. Choose carefully up front.
  • Bank clocks don't jump. Server clocks do, which is a real source of duplicate IDs in time-based schemes.
  • A raffle ticket can repeat in a small raffle. A 122-bit random ID practically never does: the numbers involved are beyond everyday intuition.

Quick check

Five questions on this chapter. Pick an answer, then check it.

quiz