Design a URL shortener (like Bitly or TinyURL)
Turn long URLs into short codes and redirect millions of clicks per second. A classic first system design: tiny write path, huge read path, unique ID generation and analytics that must not slow down redirects.
In one sentence
Store code → long URL in a key-value friendly table, generate unique short codes without collisions, and make the redirect path almost entirely cache hits.
A reference design: it covers what an app like this needs and follows how these companies have described their systems publicly, simplified to be learnable.
Requirements
Functional
- Create a short link for a long URL (optional custom alias and expiry)
- Redirect a short link to the original URL
- See click statistics for your links
Non-functional
- Redirects must be very fast (< 50 ms) and always available
- Codes must be unique and hard to guess
- Read-heavy: ~100 redirects per link created
- Links live for years
Back-of-the-envelope estimates
| Metric | Estimate |
|---|---|
| New links | 100M / month ≈ 40 writes/s (peak ~100/s) |
| Redirects | 100× reads ≈ 4,000/s (peak 10K+/s) |
| Storage | 100M × 12 × 5 years = 6B links × ~500 B ≈ 3 TB |
| Code length | base62, 7 chars = 62⁷ ≈ 3.5 trillion codes |
The numbers already tell the story: writes are tiny, reads dominate, and the whole dataset fits in a single database for a long time. The hard part is latency and availability of the redirect, not volume.
API and data model
POST /api/urls { longUrl, alias?, expiresAt? } → 201 { code, shortUrl }
GET /:code → 302 Location: longUrl
GET /api/urls/:code/stats → { clicks, byDay, byCountry }
links(code PK, long_url, owner_id, created_at, expires_at)
clicks(code, day, country, referrer, count) -- analytics storeArchitecture
Pick a scale and a request to follow it through the system. Tap any box to see what it does.
Step by step · Open a short link
- 01GET sho.rt/aZ3k9Qx
- 02Viral link? The edge already has the answer
- 03Routed to a server
- 04Looks up the code
- 05Cache hit most of the time
- 06On a miss: read from a replica
- 07Publishes a click event without waiting
- 08302 Found → browser goes to the long URL
At this scale · 1M users
Split reads from writes
- ▸Redirect service and shorten service scale separately (100:1 traffic)
- ▸A key generator hands out pre-made unique codes
- ▸Clicks go through a queue into an analytics store
- ▸Read replicas and an edge cache for viral links
Tap any box to see what it does.
Deep dive: generating unique codes
| Approach | How | Trade-off |
|---|---|---|
| Hash the URL | First 7 chars of base62(MD5(url)) | Collisions need a check-and-retry; same URL always gets the same code |
| Counter + base62 | Auto-increment ID → base62 (125 → "21") | Unique and short, but codes are guessable and one counter is a bottleneck |
| Random + check | Random 7 chars, retry if taken | Simple; retries grow as the space fills |
| Key generation service | Pre-generate random codes, hand out batches to servers | No collisions at write time; servers keep a batch in memory (a crash loses a few codes, which is fine) |
Start with random + unique index (the database rejects the rare collision). Move to a key generator or a Snowflake-style ID split into base62 when many servers create links at once.
How it changes with scale
Step by step · Shorten a URL
- 01POST /urls with the long URL
- 02Validates the URL and generates a 7-character base62 code
- 03INSERT (code, long_url); the unique index rejects duplicates
- 04Gets back sho.rt/aZ3k9Qx
At this scale · 100 users
One server, one database
- ▸A single app handles both creating links and redirecting
- ▸PostgreSQL with a unique index on the code
- ▸Click count is a column updated on each redirect
Tap any box to see what it does.
| Users | What you run | Why it changes |
|---|---|---|
| 100 | One app + PostgreSQL | Anything works. Ship it. |
| 10K | 2 servers + LB + Redis | Availability during deploys; cache popular codes |
| 1M | Separate redirect/shorten services, key generator, replicas, click queue | Redirects are 100× the writes and analytics must not slow them down |
| 100M+ | Sharded key-value store, multi-region, Kafka + columnar analytics | Billions of rows and global users who expect a fast redirect anywhere |
Key takeaways
- 01It's a read-heavy system: optimise the redirect path with caches at every layer.
- 02Generate codes without coordination at write time: random + unique index, or a key generator.
- 03Record clicks asynchronously so analytics never slow down redirects.
- 04A single PostgreSQL goes surprisingly far; shard by code only at billions of links.
Check your understanding
1 of 3
Why is a URL shortener like Bitly a read-heavy system?