Skip to content

Design a URL Shortener, stage 2 of 9: decide

Generate short codes

Every link needs a code like aZ3kQ9x. Codes must be short, two links must never share one, and knowing one code must not let anyone find others.

System so far· 5 parts
123CLIENTClickersSERVICERedirect serviceDATABASEPostgresSERVICELinks APICLIENTCustomerdashboard

Select a component to see what it is responsible for and which state it owns.

  1. 1Redirect service → Postgres: Look up code
  2. 2Customer dashboard → Links API: Create, edit, disable
  3. 3Links API → Postgres: Insert with unique code

What you need to know

0 of 4 checks done
  1. Codes use base62: the characters a–z, A–Z and 0–9. Each character has 62 possible values, so a code of n characters has 62ⁿ possible values.

    LengthPossible codes
    5about 916 million
    6about 57 billion
    7about 3.5 trillion
  2. Check

    You'll create about 1.2 billion links a year, and codes are chosen at random. Which length should you use?