Design a URL shortener (like bit.ly)
Turn long URLs into short links, like bit.ly/x7Kp2a, that redirect to the original.
Why interviewers ask it
It's the classic first HLD question: small enough to finish, and it covers APIs, unique IDs, caching, database choice and read-heavy scaling.
Functional requirements
- Create a short URL for a long URL (optional custom alias and expiry).
- Redirect from the short URL to the long URL.
- Optional: click analytics.
Non-functional requirements
- Very read-heavy: about 100 redirects per new link.
- Redirects in under about 50 ms; highly available.
- Codes must be unique and, ideally, hard to guess.
From version 1 to the final design
- Version 1: one server and one database table mapping code → URL, with a counter in the database for IDs.
- Version 2: a Redis cache for redirects, so most requests never touch the database.
- Version 3: many stateless app servers behind a load balancer, with ID ranges reserved per server so there's no single counter.
- Version 4: shard the table by code when it outgrows one database, and send click analytics through a queue.
The design, step by step
- Write: the API generates a unique code and stores code → long URL.
- Read: GET /{code} → check the Redis cache → on a miss, read the database → redirect.
- Code generation: a distributed counter (ID ranges per server) encoded in Base62, or a hash of the URL cut to 7 characters, with collision checks.
- Very popular links can also be cached at the edge.
Deep dives
- Counter + Base62: no collisions. Each server reserves a block of IDs from a coordinator, so servers don't fight over one counter.
- Hashing: the same URL always gets the same code (no duplicates), but collisions need check-and-retry.
- 301 (permanent; browsers cache it, so less load but no analytics) or 302 (temporary; every click reaches you, so analytics work).
- Analytics: log each click to a queue and aggregate in the background.
Common mistakes
- Hashing the URL without handling collisions.
- No caching, for a workload that's 100 times more reads than writes.
- Using 301 redirects, then wondering why the analytics are empty.
Not true: 301 redirect: each click still reaches your server, so analytics work.
In fact: Browsers remember a 301 (permanent) redirect, so later clicks never reach your server. Use 302 (temporary) when every click has to be counted.
In the interview
It's a read-heavy key-value lookup. Writes generate a unique 7-character Base62 code from a distributed counter, with ID ranges reserved per server, and store code → URL. Reads go through a Redis cache in front of the database and return a 302, so we can count clicks; click events go onto a queue for background analytics.
Part of Unit 8: High-level design questions in the free System Design course.