Step 1 · The pattern
Turn a number into a short code
Each link has a numeric ID. Base62 writes it with 62 URL-safe characters: 0–9, a–z, A–Z. Seven characters are enough for 3.5 trillion links.
- create link
- short code returned
In short
Base62: how it works and how it breaks
A short link needs a short code. Base62 writes a number using sixty-two symbols, the digits plus lower and upper case letters, all safe in a URL. Seven characters cover about 3.5 trillion values. The encoding is the easy part. The decision that matters is what number you encode. If it is a plain counter, consecutive links get consecutive codes: abc123 is followed by abc124. Anyone can start from their own link, add one, and read every link ever created, including ones meant to stay private, such as password resets or unlisted documents. Competitors can also see how many links you create each day. The fix is to make codes unpredictable: draw them at random from the full space and check for a collision, or pass the counter through a keyed permutation before encoding. Still treat a short link as public, not secret.
How this comes up in interviews
"Design the short code for a URL shortener. How long is it, and how do you generate it?"
Then they ask
- If codes come from a counter, what can an outsider learn or do?
- With random codes, how do you handle two creates picking the same code?
- How else could you make codes unpredictable without a collision check on every create?
A strong answer covers
- Base62 writes a number with 62 URL-safe characters: digits plus lower and upper case letters.
- Seven characters cover about 3.5 trillion values.
- Encoding a plain counter makes codes consecutive, so anyone can enumerate every link and see how many are created each day.
- Make codes unpredictable: draw at random and check for a collision, or pass the counter through a keyed permutation first.
- Still treat a short link as public, not secret.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: System Design Primer: design Pastebin / Bit.ly (Donne Martin) ↗ · Instagram: Sharding & IDs at Instagram ↗ · Azure Architecture Center: rate limiting pattern ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.