LESSON 04 · Identity and tradeoffs

Generate keys without collisions

The key-space must fit demand, and concurrent creation must preserve uniqueness.

SYSTEM VIEW

The system

Diagram view
Key length—
Space taken—
Inserts per link—

Watch keys get sized, issued and checked, then two servers race for the same one.

  1. A creator posts a long URL, and the Key generator issues x7Kp2Q: six base62 characters, about 56.8 billion keys. By year five, 55 billion mappings fill 96% of them; at 30 million links a day, six characters run out in about 5.2 years.

    Base62
    0–9, a–z, A–Z
    Runs out in
    about 5.2 years

    What changed

    • Key x7Kp2Q, 6 characters
    • 96% of keys taken by year 5
  2. One more character multiplies the space by 62, to about 3.52 trillion keys. The Key generator now issues x7Kp2Qa, and the same 55 billion mappings take only 1 key in 64, leaving room for reservations and uneven allocation.

    Size it from
    lifetime creations

    What changed

    • Key x7Kp2Qa, 7 characters
    • 1 key in 64 taken by year 5
  3. Here the Key generator encodes a counter in base62. Its keys are compact and never collide while the counter is truly unique, but they run in order: someone holding x7Kp2Qa can simply try x7Kp2Qb, another person’s link, unless keys are obfuscated.

    What changed

    • Keys from a counter, 1 insert each
    • x7Kp2Qa, then x7Kp2Qb
    • The next key is guessable
  4. A 64-bit Snowflake ID, the scheme Twitter published, avoids a central counter. In base62 it takes 11 characters, so the creator gets a key like 1gK7wQz3XpR instead of a 7-character one, which gives back much of the shortness.

    Snowflake ID
    64 bits
    In base62
    11 characters

    What changed

    • 64-bit Snowflake IDs
    • Central counter
    • Keys grow to 11 characters
  5. A random 7-character key hides the order but can collide. The Key generator draws Rq8Zt3m, and the Create service inserts it under a unique constraint on key, which stores the row only if no row holds that key yet.

    Postgres
    INSERT … ON CONFLICT
    DynamoDB
    conditional write

    What changed

    • Random 7-character keys
    • Unique constraint on key
    • Rq8Zt3m stored
  6. The next candidate, x7Kp2Qa, is taken, so the unique constraint rejects the insert and the Create service retries with Tq5Mx2e. With 1 key in 64 taken by year five, a creation averages about 1.02 inserts; with six characters, about 28. Truncated URL hashes collide too.

    7 characters
    about 1.02 inserts
    6 characters
    about 28 inserts

    What changed

    • Insert of x7Kp2Qa rejected
    • Tq5Mx2e stored
    • About 1.02 inserts per link
  7. Create servers A and B, each handling a new link, draw the same random key, Jd4Wv9k. Each checks the database before inserting, and both see “unused”, because neither has written yet. The check alone can’t keep them apart.

    What changed

    • Create server B
    • Key generator
    • Both draw Jd4Wv9k
    • Both see “unused”
  8. Both servers now insert Jd4Wv9k. The unique constraint stores one and rejects the other, so Create server A draws a new key and retries. Try “Check only”: both inserts succeed and the key has two owners. Custom aliases need the same guarantee.

    Check, then insert
    can’t close the race

    What changed

    • Both insert Jd4Wv9k
    • Unique constraint: one owner
    • Check only: two owners
    Guard

Space plays · ← → step · Home resets

THE IDEA

How it works

Base62 uses digits and uppercase and lowercase letters. Six characters provide about 56.8 billion combinations, which the example’s 55 billion mappings would almost fill; seven provide about 3.52 trillion, leaving room for reservations and uneven allocation. At 30 million new links a day, six characters would run out in about 5.2 years.

A unique integer encoded in base62 is compact and free of collisions (two links receiving one key) when the ID allocator is truly unique. It also exposes a guessable sequence unless keys are obfuscated or access policy accepts enumeration: someone holding x7Kp2Qa can simply try x7Kp2Qb. A 64-bit Snowflake ID, the scheme Twitter published, avoids a central counter but takes 11 base62 characters, which gives back much of the shortness.

A random key hides ordering but needs collision detection. Generate a candidate, insert with a unique constraint, and retry on conflict. Postgres exposes this as INSERT ... ON CONFLICT, and DynamoDB as a conditional write. With 55 billion of 3.52 trillion keys taken, about one candidate in 64 collides by year five, so a creation averages about 1.02 inserts. The same 55 billion in a six-character space would fill 96% of it, and a creation would average about 28 inserts.

Truncating a hash of the destination also creates collision risk and couples identity to URL normalization, the rules for deciding whether two spellings are the same URL. Merely checking for an existing key before insertion is not safe under concurrent writers, because two servers can both see “unused” before either writes. Custom aliases need the same atomic uniqueness guarantee.

DECISION POINT

Two creation servers generate the same random key at once. Both check that it is unused. What prevents duplicate ownership?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Estimate key-space from lifetime creations, not daily traffic alone.
  • Let an atomic unique constraint decide collisions under concurrency.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A product wants case-insensitive keys so people can read them aloud, leaving 36 symbols. How long must keys be to hold 200 billion links?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.