System Design FAQ: Top Questions
37. How would you design a URL Shortener like Bitly?
A URL Shortener maps long URLs to short aliases. It's widely used for improving link readability, analytics, and character-saving (e.g., Twitter).
📋 Functional Requirements
- Generate short unique keys for long URLs
- Redirect short URLs to original long URLs
- Track usage analytics (clicks, referrers)
- Support custom aliases
📦 Non-Functional Requirements
- High availability and low-latency redirection
- Idempotent key generation (same URL maps to same key optionally)
- Expiration and deletion of links
🏗️ Core Components
- API Server: Accepts long URLs and returns short aliases
- Key Generator: Encodes URL or uses hash/ID space
- Redirector: Handles high-throughput redirects
- Analytics Tracker: Stores visit metrics
🔑 Base62 Encoding (Python)
import string
CHARSET = string.digits + string.ascii_letters
def encode(num):
base = 62
s = []
while num > 0:
s.append(CHARSET[num % base])
num = num // base
return ''.join(reversed(s)) or '0'
🔧 Example Redis Key-Value Mapping
SET short:abc123 "https://verylongsite.com/user/onboarding?id=983"
EXPIRE short:abc123 31536000 # 1 year
📥 Sample API
POST /shorten
{
"long_url": "https://openai.com/research/chatgpt"
}
Response:
{
"short_url": "https://sho.rt/x9Bz4A"
}
📈 Analytics Schema (Click Log)
{
"short_code": "x9Bz4A",
"clicked_at": "2025-06-11T10:15:00Z",
"ip": "45.23.19.88",
"user_agent": "Mozilla...",
"referrer": "https://twitter.com"
}
🔐 Custom Alias Handling
- Allow user to reserve codes like
https://sho.rt/promo2025
- Reject duplicates with 409 Conflict
📊 Admin Panel Features
- Top-performing links
- Expiration date manager
- Click-through analytics and referrer pie chart
📈 Observability
- Short link resolution latency
- 404 rates (invalid code hits)
- Most clicked links in time window
🧰 Tools/Infra Used
- Store: Redis / DynamoDB / PostgreSQL
- Tracking: Kafka + OLAP store (ClickHouse)
- Web server: FastAPI / Node.js / Go
📌 Final Insight
URL shorteners are deceptively simple but must be scalable, analytics-ready, and collision-safe. Encoding strategy, rate of generation, and efficient redirection paths are key to success.