Designing a URL Shortener: A System Design Interview Walkthrough
4 views
"Design a URL shortener like TinyURL or bit.ly" is often the first system design question candidates meet. It looks small, but it touches ID generation, storage, caching, redirects and analytics. Here is a structure that fits a 45-minute round.
1. Clarify requirements
Functional: given a long URL, return a short one; visiting the short URL redirects to the original; optional custom aliases; optional expiry; basic click analytics. Non-functional: very read-heavy (assume 100 reads per write), low redirect latency (under 50 ms at the edge), high availability, and short codes that are hard to guess if links are private.
2. Back-of-the-envelope estimates
Say 100 million new links per month, roughly 40 writes per second on average and 4,000 redirects per second, with peaks several times higher. Over five years that is about 6 billion links. With about 500 bytes per record, storage is around 3 TB, which is easy for a sharded key-value store.
3. Choosing the short code
Base62 (a-z, A-Z, 0-9) with 7 characters gives about 3.5 trillion combinations, plenty for 6 billion links. Three common approaches:
- Hash the long URL (MD5 or SHA-256) and take the first 7 characters. Simple, but you must handle collisions and decide whether the same long URL should always map to the same code.
- A global counter encoded in base62. No collisions, but sequential codes are guessable and a single counter is a bottleneck. Fix this by giving each app server a range of IDs from a coordinator such as ZooKeeper or a database sequence.
- Random codes with a uniqueness check on insert. Easy to reason about, but costs an extra read per write.
Interviewers mainly want you to compare the trade-offs, not pick a "right" answer.
4. API and data model
POST /api/links {long_url, alias?, expires_at?} returns {short_url}. GET /{code} returns 301 or 302 with a Location header. Use 302 if you need accurate click analytics, because browsers cache 301 redirects. Table: code (primary key), long_url, owner_id, created_at, expires_at. A key-value or wide-column store fits well because every lookup is by primary key.
5. Scaling reads
Put a cache (Redis or Memcached) in front of the database. Popular links follow a power law, so even a small cache gives a high hit rate. Add a CDN or edge workers for the hottest redirects. Shard the store by code with consistent hashing so you can add nodes without large rebalancing.
6. Analytics without slowing redirects
Never write click counts synchronously on the redirect path. Emit an event to a queue such as Kafka and aggregate it in a stream processor into per-link, per-day counters.
7. Edge cases worth mentioning
Expired links (lazy deletion on read plus a background cleanup job), abuse and malware links (scan on creation, rate-limit anonymous users), custom alias conflicts, and redirect loops where a short URL points to another short URL.
8. Wrap-up
Finish by summarising the bottlenecks and what you would monitor: cache hit rate, p99 redirect latency, ID allocation and error rates.
Practising this aloud is the best preparation, because the hard part is explaining trade-offs clearly under time pressure. If you want support during the actual round, Linkjob AI (https://linkjob.ai) gives real-time help during live job interviews and technical interviews, so you can keep your design structured while you talk it through.