← All articles

Base62 short codes and how URL shorteners make keys

Updated Nov 8, 2026 · 3 min read

A short link needs a code that is short, safe to put in a URL, and never handed out twice. Base62 covers the first two. It writes numbers using the 62 characters 0–9, a–z and A–Z, all of which are allowed in a URL path without escaping. The third part, uniqueness, depends on where the numbers come from.

How long does a code need to be?

Each extra character multiplies the number of possible codes by 62.

LengthPossible codesRoughly
162
23,844~3.8 thousand
3238,328~240 thousand
414,776,336~15 million
5916,132,832~920 million
656,800,235,584~57 billion
73,521,614,606,208~3.5 trillion
8218,340,105,584,896~220 trillion

To pick a length, estimate how many links you will ever create (links per day × days you keep them) and take the shortest row that covers it. At 10 million links a day for 5 years you need about 18 billion codes. 6 characters cover 57 billion, so 6 works, with room to spare. At 100 million a day you need about 180 billion, and only 7 characters are enough.

4 characters~15 million
5 characters~920 million
6 characters~57 billion
7 characters~3.5 trillion
8 characters~220 trillion
Possible codes by length. Seven characters covers 10 million links a day for centuries. This uses a log scale, so equal steps in length mean multiplying, not adding, so bars that look close can be 1,000× apart.

Encoding a number

Base62 works like any other base. Divide by 62 repeatedly and use each remainder as a digit, least significant first.

const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

function toBase62(n: number): string {
  let code = '';
  do {
    code = ALPHABET[n % 62] + code;
    n = Math.floor(n / 62);
  } while (n > 0);
  return code;
}

toBase62(125);           // '21'      (2 × 62 + 1)
toBase62(1_000_000_000); // '15FTGg'

Decoding runs the other way. Start at zero, and for each character multiply the total by 62 and add that character’s position in the alphabet. For ids above 2⁵³ use bigint, since JavaScript numbers lose precision past that point.

Three ways to get the number

ApproachCollisions?Trade-off
Hash the long URL and keep the first 7 charactersYesTruncating throws away most of the hash, so different URLs can share a prefix. You must check the database and retry on every clash.
7 random charactersYes, eventuallySimple and unguessable, but you still have to check before saving.
A counter, encoded in base62NeverUnique by construction. The work moves to keeping the counter consistent across servers.

Why random codes still collide

With 3.5 trillion possible 7-character codes it feels like clashes should never happen. The birthday problem says otherwise. The chance that some pair of codes matches passes 50% after only about 2.2 million codes, roughly the square root of the space. Any single new code is still very unlikely to clash (at a billion links, about 0.03%), so retrying is cheap, but the check before saving can never be skipped.

Running a counter across many servers

One shared counter that every server increments on every new link becomes a bottleneck and a single point of failure. The usual fix is to hand out ranges instead of single numbers:

  1. A small key-generation service owns the counter, backed by a database row or a coordination service.
  2. Each app server asks it for a block, say the next 10,000 numbers, and gets back a start value.
  3. The app server hands out numbers from its block in memory, with no network call per link.
  4. When the block runs low it asks for another one.

If a server crashes, the unused part of its block is simply skipped. Gaps in the sequence cost nothing, since nobody needs the codes to be dense.

An alternative is a time-based 64-bit id (timestamp, machine number and a per-machine sequence packed together), which needs no coordination at all. The cost is length, since a 64-bit number can need up to 11 base62 characters.

Block allocatornext is 3,000,001App server 11 – 1MApp server 21M – 2MApp server 32M – 3M
Each server takes a block of a million numbers and hands them out without asking anyone. Only the next block needs coordination.

Practical details

  • Sequential codes are guessable, so anyone can walk through abc1, abc2 and so on. If that matters, scramble the number with a reversible step before encoding, for example multiplying by a large constant that shares no factors with 62⁷ and taking the result modulo 62⁷.
  • Codes are case sensitive. If links will be read aloud or typed from print, consider base58, which drops look-alikes such as 0, O, I and l, or base36 if the codes must survive being lowercased.
  • Reserve words you do not want to hand out (api, admin, offensive strings) and skip them when they come up.