Design interviews / Beginner / 7 min
Design a URL shortener
Counter or random codes, collisions, a cache for hot links, and why 301 vs 302 changes your traffic and your analytics.
Start the simulation → Change the load, break a part, watch what happens. Free, no sign-up.
In short
- What it is
- A service that maps a short code to a long URL: one write when a link is created, then many reads and redirects.
- Why it matters
- Reads outnumber writes by 100 to 1 or more, so the design is about fast lookups and a code space that never runs out.
- The trade-off
- Counter codes are unique but guessable; random codes hide the count but collide as the space fills; 301 saves traffic but loses analytics.
- Say it in an interview
- "A counter (or ID ranges per server) encoded in base 62, links in a key-value store, a cache for hot codes, and 302 redirects so clicks are counted."
Check yourself
How many different 7-letter codes can you make from a-z, A-Z and 0-9?
Answer: About 3.5 trillion. 62 to the power 7 is about 3.5 trillion, plenty for most shorteners.
Why does a URL shortener put a cache in front of the database?
Answer: A few popular links get most of the clicks, so caching them removes most database reads. Clicks follow a long tail: the hottest links are read again and again, which is exactly what a cache serves well.
What do you lose with 301 Moved Permanently instead of 302?
Answer: Accurate click counts, because browsers remember the target and skip your server. Browsers cache a 301, so repeat clicks never reach you. Use 302 (or 307) when you need analytics.
More in design interviews
- Design a job scheduler
- Design a notification system
- Design WhatsApp: ticks, offline inboxes and retries
- Design Instagram: photos through a CDN
- Design YouTube: upload, transcode, publish
- Design Netflix: adaptive bitrate streaming
- Design Uber: finding the nearest driver
- Design Google Maps: routes and live traffic