Architecture
Interviews
Backend
Software Engineering
Ask anything about this article
Hi! I've read this article.
What would you like to know?
@farhan
URL shortener is the most commonly asked system design question. It tests your ability to design for scale, handle edge cases, and make smart architectural trade-offs. Let's design one from scratch.
Write: 100M URLs/month ≈ 40 URLs/second
Read: 10B redirects/month ≈ 4000 reads/second (100:1 read/write ratio)
Storage per URL: ~500 bytes (URL + metadata)
5-year storage: 100M × 12 × 5 × 500B = 3TB
A 7-character short code gives us: 62^7 = 3.5 trillion unique URLs.
pythonimport hashlib
import base64
def generate_short_url(long_url: str, counter: int) -> str:
# Combine URL with counter for uniqueness
raw = f"{long_url}:{counter}"
hash_bytes = hashlib.md5(raw.encode()).digest()
# Take first 6 bytes and base62 encode
encoded = base64.urlsafe_b64encode(hash_bytes[:6])
return encoded.decode()[:7]
sqlCREATE TABLE urls (
short_code VARCHAR(7) PRIMARY KEY,
original_url TEXT NOT NULL,
user_id BIGINT,
created_at TIMESTAMP DEFAULT NOW(),
expires_at TIMESTAMP,
click_count BIGINT DEFAULT 0
);
Client → Load Balancer → API Servers → Cache (Redis) → Database
↓
Analytics Service → Kafka → Analytics DB
Redis with LRU eviction is perfect:
Cache hit ratio target: 80%
Cache size: 20% of 3TB = 600GB → distributed across nodes
TTL: 24 hours for popular URLs
Read: Redis GET → DB fallback → write-through cache
Write: Write to DB → invalidate cache
Track every redirect asynchronously:
json{
"short_code": "abc1234",
"timestamp": "2025-01-15T10:30:00Z",
"user_agent": "Mozilla/5.0...",
"ip_address": "203.0.113.1",
"referer": "https://twitter.com",
"country": "IN"
}
Use Kafka for event streaming and ClickHouse for analytics queries.
| Decision | Option A | Option B |
|---|---|---|
| Redirect code | 301 (permanent, cached) | 302 (temporary, trackable) |
| Hash function | MD5 (fast, collisions) | Counter-based (no collisions) |
| Database | SQL (consistency) | NoSQL (scale) |
| Cache | Write-through | Write-around |
This design handles billions of URLs with sub-100ms latency. Practice drawing this architecture on a whiteboard — that's how you'll present it in an actual interview.