Design a URL shortener
medium 45 minTurn a long URL into a short code and redirect anyone who opens it.
Agree on the scope
Ask what changes the design, then commit to a short list.
Clarifying requirements
Interviewer
Let's design a URL shortener like Bitly or TinyURL. What questions do you have?
You
First scale, then features, then technical limits:
- How many new URLs a day? At Bitly's size I would assume 100M or more.
- What is the read to write ratio? I would guess 100:1 or higher, since links are clicked many times.
- Do links live forever, or can they expire?
- Custom aliases, analytics, QR codes, accounts?
- Redirect latency? I would target under 100 ms at p99.
- Must we handle phishing and malware?
- Availability? Even a short outage breaks millions of links, so 99.99%.
Interviewer
100M new URLs a day, 100:1 reads, links live forever with optional expiry. Yes to custom aliases and analytics.
You
Then I will design for 100M writes and 10B reads a day. Password-protected links, A/B tests, mobile deep links and team workspaces are out of scope for now.
Functional requirements
- Generate a unique short URL from a long URL
- Redirect a short URL to the original (301 or 302)
- Custom short codes (optional aliases)
- Expiry (optional TTL), then
410 Gone - Click analytics: count, geography, referrer, device, over time
- User accounts to manage links
Out of scope for now
- Password-protected links
- A/B testing for links
- Deep links into mobile apps
- Team and workspace management
Non-functional requirements
| Quality | Target | Why |
|---|---|---|
| Latency | redirect < 100 ms p99 | Users should not notice the hop |
| Availability | 99.99% for redirects | A broken link is worse than a slow shorten call |
| Scale | 100M writes, 10B reads a day | Must scale horizontally |
| Durability | zero link loss | A link that was issued must work |
| Security | codes hard to enumerate, abuse blocked | Links may point at private or harmful pages |
| Consistency | new link works at once, analytics may lag | Read your own write; counts can be late |
| Uniqueness | codes never reassigned, even after expiry | An old link must not start pointing elsewhere |
Why these questions
Each answer changes the design
| You ask | If the answer is | Then |
|---|---|---|
| Can a link's destination change? | Yes | No permanent redirects; short edge TTLs and purges |
| Must clicks be exact? | Yes | Count from edge logs, or skip edge caching |
| Are destinations private? | Yes | Random codes plus access control, not scrambling alone |
| Custom aliases? | Yes | A conditional write decides; generated codes and aliases share one namespace |
| How many links a day? | Thousands | Stop at version 1 |
| Global users? | Yes | Edge, regional caches, one home region for alias claims |