Capstone: Design a URL Shortener
Overview
A URL shortener turns a long link into a short one and redirects visitors. It is small enough to design in minutes, yet it touches estimation, caching, databases and ID generation. Follow the same steps for any design question.
Detailed Notes
What is the task
Design a service where a user submits a long URL and gets a short link (like sho.rt/aB3x9Q). Opening the short link redirects to the original.
The problem it solves
Long links are awkward to share. The system must redirect very fast, stay available, never give two URLs the same code, and cope with far more reads than writes.
How it works: follow these steps
- Requirements. Functional: create a link, redirect, optional expiry. Non-functional: low redirect latency, high availability, no collisions.
- Estimate. 100 million new links a month is about 40 writes a second. With 100 reads for every write, about 4,000 redirects a second (peak maybe 20,000). Storage: 100 million x 500 bytes is 50 GB a month, about 3 TB in five years. That is small enough for a sharded key-value store.
- Generate the code. Use base62 (a-z, A-Z, 0-9). Seven characters give 62^7, about 3.5 trillion codes. Take a unique id from a counter-range service (each server owns a block of ids) and base62-encode it. Random codes are harder to guess but need collision checks.
- Store. A key-value store maps code to long URL, partitioned by code and replicated for availability.
- Redirect. The request goes through the CDN and cache first; on a miss it reads the database. Reply with HTTP 301 (permanent, cacheable, fewer hits to you) or 302 (every click reaches you, so you can count them).
- Protect. Rate limit link creation and scan for malicious URLs.
A simple example
A user posts https://example.com/very/long/article?id=98213. The service takes the next id, 125,000,001, encodes it as base62 (8sud3), saves 8sud3 -> long URL, and returns sho.rt/8sud3. A visitor opening it hits the cache, finds the URL and is redirected.
System design example
Client -> CDN -> Load balancer -> Redirect service -> Cache -> Key-value store
Client -> Load balancer -> Create service -> ID generator -> Key-value store
Redirect service -> queue -> analytics workers
Bottlenecks and fixes:
- Hot links: cache them in memory and at the CDN.
- ID generator as a single point of failure: give each server its own id range and run replicas.
- Database growth: partition by code.
- Analytics load: record clicks through a queue, not on the redirect path.
Trade-off to mention: counter-based codes are collision-free but guessable; 301 offloads traffic but loses click counts.
Summary
- Clarify requirements, estimate, then design.
- Reads dominate, so cache heavily and put a CDN in front.
- Base62 over a unique id gives short, collision-free codes.
- Name the bottlenecks and trade-offs out loud.
Architecture Diagram
Reads go through cache; writes get a unique id.
- 1The CDN and cache answer popular links directly.
- 2A miss reads the key-value store.
- 3New links get a unique id, base62-encoded into a short code.
- 4Clicks are counted asynchronously through a queue.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

