Notes › System Design
Design a URL Shortener
Like TinyURL or bit.ly: https://example.com/blog/2025/very/long/path?ref=abc → https://sho.rt/aZ3x9Kq.
1. Requirements
Functional
- Create a short URL from a long one.
- Visiting the short URL redirects to the original.
- Optional: custom alias (
sho.rt/my-event), expiry date, click analytics.
Non-functional
- Very low latency redirects (they're on every click).
- High availability — broken links are bad.
- Short codes must be unique and not easily guessable.
- Read-heavy: ~100 redirects for each new URL.
2. Capacity estimates
- 100 M new URLs/month → 100M / (30 × 86,400) ≈ 40 writes/s.
- 100:1 read ratio → ~4,000 redirects/s (peaks ~10–20k).
- Keep for 5 years → 100M × 12 × 5 = 6 billion URLs.
- ~500 bytes per record → 6B × 500 B ≈ 3 TB of storage.
- How long must the code be? Base62 (a–z, A–Z, 0–9): 62⁶ ≈ 56.8 billion, 62⁷ ≈ 3.5 trillion. 7 characters is plenty.
- Cache: 20% of daily redirects are hot → cache a few GB in Redis.
3. API
POST /api/urls
{ "longUrl": "https://example.com/...", "alias": "my-event", "expiresAt": "2026-12-31" }
→ 201 { "shortUrl": "https://sho.rt/aZ3x9Kq" }
GET /{code}
→ 302 Location: https://example.com/... (404 if unknown, 410 if expired)
GET /api/urls/{code}/stats → clicks, countries, referrers
4. Data model
urls
code VARCHAR(10) PRIMARY KEY
long_url TEXT
user_id BIGINT
created_at TIMESTAMP
expires_at TIMESTAMP NULL
clicks (or an analytics store)
code, clicked_at, country, referrer, device
Access pattern is a simple key → value lookup with no joins → a NoSQL key-value/wide-column store (DynamoDB, Cassandra) scales easily; PostgreSQL/MySQL also works fine at this size with sharding later.
5. Generating the short code
Option A: hash the long URL
MD5(longUrl) → base62 → take the first 7 characters.
- ✅ Same URL → same code (natural de-duplication).
- ❌ Collisions — two URLs share 7 chars. Must check the DB and retry (append a salt/counter and rehash). Extra lookup on every write.
Option B: unique counter + base62 ✅ (common answer)
Give each URL a unique number, then encode it in base62.
const CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function toBase62(n) {
let s = '';
do { s = CHARS[n % 62] + s; n = Math.floor(n / 62); } while (n > 0);
return s;
}
toBase62(125); // "21" (2 × 62 + 1)
toBase62(3_500_000_000); // a 6-char code
- ✅ No collisions ever.
- ❌ A single DB auto-increment is a bottleneck / SPOF, and sequential codes are guessable.
- Fixes: each app server grabs a range of IDs (e.g. 1,000,000 at a time) from a coordinator like ZooKeeper or a DB row — no coordination per request. Use a distributed ID generator (Snowflake: timestamp + machine id + sequence). Shuffle/obfuscate the number (e.g. multiply by a large prime mod 62⁷) so codes don't look sequential.
Option C: pre-generated key service (KGS)
A separate service generates random unique 7-char keys in advance and stores them in an "unused" table. App servers take a batch into memory and mark them used.
- ✅ Fast, random-looking, no collisions at write time.
- ❌ One more service to run and keep highly available; a crashed server wastes its unused batch (acceptable).
Custom aliases: check availability and insert with a unique constraint; reject reserved words (api, admin).
6. Redirect: 301 or 302?
| 301 Moved Permanently | 302 Found (temporary) | |
|---|---|---|
| Browser caches it | yes — next clicks skip your server | no — every click hits you |
| Server load | lower | higher |
| Analytics | you miss repeat clicks | every click counted |
| Can change/expire the link later | hard (cached) | easy |
bit.ly-style services usually use 302 (or 307) because analytics and editable links matter.
7. High-level architecture
┌──────────── write path ────────────┐
Client → LB → URL service → ID range / KGS → DB (sharded by code)
└──────────── read path ─────────────┘
Client → CDN/edge? → LB → Redirect service → Redis cache → (miss) DB
↓
click event → Kafka queue → analytics workers → analytics DB
- Cache-aside in Redis:
code → long_url, LRU eviction. Most redirects never touch the DB. - Analytics asynchronously: publish a click event to a queue; never slow the redirect down to write stats.
- Shard the urls table by hash(code) — lookups always know the code, so they hit one shard.
8. Other details
- Expiry: check
expires_aton read (return 410); a background job deletes old rows. - Rate limiting on creation (per user/IP) to stop spam bots; scan long URLs against malware/phishing lists.
- Same long URL twice? Either return the existing code (needs an index on long_url hash) or create a new one per user (needed for per-user analytics).
- Availability: replicas in multiple zones, cache in front, stateless services behind the load balancer.
- Security: non-sequential codes, HTTPS, don't allow
javascript:URLs.
Interview one-liners
- 7 base62 characters give 3.5 trillion codes.
- Counter + base62 avoids collisions; hand out ID ranges per server to avoid a bottleneck.
- Read-heavy → Redis cache in front of the DB; analytics through a queue.
- 302 keeps every click visible for analytics; 301 lets browsers cache.
- Related: System Design Basics: Scalability, Caching, CDN and Databases at Scale.