31. Design a URL Shortener
1
The interview prompt2
31.1 What do you ask first?3
31.2 How big is this, really?4
31.3 What are the doors?5
31.4 What do we store?6
31.5 What does the whole system look like?7
31.6 How do you generate the short code?8
31.7 301 or 302?9
31.8 How do you keep redirects fast at 12,000 a second?10
31.9 How do you count clicks without slowing redirects?11
31.10 What would you say in the last five minutes?12
Score yourself13
Variations to practise14
Sources15
Code lab31. Design a URL Shortener
31.1 What do you ask first?
3 min read+10 XP
Step 1: Requirements (5 minutes)
A strong candidate doesn't draw. They run through SCALE (Chapter 1, section 1.5) and write the answers down.
| Letter | Candidate asks | Interviewer answers |
|---|---|---|
| Scope | "Shorten and redirect for sure. Custom aliases? Expiry? Analytics? User accounts?" | "Shorten and redirect. Click counts per link would be nice. Skip accounts and custom aliases for now." |
| Count | "How many new links a month?" | "Say 100 million." |
| Access pattern | "How often is a link clicked compared to created?" | "About 100 clicks per link created." |
| Latency | "How fast must a redirect be? Does it need to be up all the time?" | "It should feel instant, and links should always work." |
| Exactness | "Must click counts be exact and live, or can they lag a little?" | "A few minutes of lag is fine." |
Then the candidate writes the agreed requirements where both can see them:
- Functional (verbs): shorten a long URL; redirect a short URL; count clicks per link.
- Non-functional (adjectives): redirects fast and highly available; a saved link is never lost and never points somewhere else; codes short; counts can lag by minutes.
Interviewer pushback:
- "Why do you care about the read/write ratio already?" → Because it decides where the design effort goes. At 100 clicks per creation, the redirect path is the one to make fast and cheap.
- "Should the same long URL always get the same short code?" → Not required. If two users shorten the same page, giving them separate codes is simpler and lets each have their own click count. I'll note it as a choice.
Memory code: "Ask about SCALE, then write it on the wall." Requirements you can both see stop you solving the wrong problem.