← Lessons

quiz vs the machine

Silver1100

System Design

The Base62 Encoding For Ids

Packing numeric ids into short alphanumeric codes for compact, readable links.

4 min read · intro · beat Silver to climb

Turning numbers into short codes

A shortener often assigns each link a growing numeric id. But a number like 125000000 is long. Base62 encoding rewrites that number using sixty two symbols, the digits zero to nine, lowercase a to z, and uppercase A to Z.

More symbols per character means fewer characters for the same value. Base62 packs a large id into a short string that is safe in a url and easy to read.

Why base62

  • It uses only url safe characters, no symbols that need escaping.
  • Sixty two symbols give huge range from few characters. Seven characters cover trillions of values.
  • It is reversible. Decode the code back to the id to find the mapping.

How the size works

  • Base10 uses ten symbols, base62 uses sixty two.
  • Each extra character multiplies capacity by sixty two, so codes stay tiny even at scale.
  • Six characters already cover over fifty billion combinations.

Key idea

Base62 encoding maps a numeric id onto sixty two url safe symbols so a large number becomes a short reversible code, with each character multiplying the range by sixty two.

Check yourself

Answer to earn rating on the learn ladder.

1. Why does base62 produce shorter codes than base10?

2. Which characters does base62 use?

3. Why is base62 reversible useful here?