System Design FAQ: Top Questions
3. How would you design a URL Shortener (like Bitly)?
A URL Shortener converts long URLs into compact, unique short codes (e.g.,
https://my.ly/abc123) and redirects users to the original link when accessed. It must be fast,
globally available, and resilient.
๐ Requirements Gathering
- Functional: Generate short URLs, redirect to long URLs, track analytics
- Non-Functional: High availability, low latency, short code uniqueness
๐ค Short Code Generation Approaches
- Hashing: MD5/SHA-256 of long URL + base62 (risk: collision)
- Random Generator: Create 6-8 char alphanumerics (check uniqueness in DB)
- Auto-increment ID + base62: Scalable, predictable
๐ก Example: Base62 Encoding with Auto-Increment ID
def encode_base62(num):
characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
result = []
while num > 0:
result.append(characters[num % 62])
num //= 62
return ''.join(reversed(result))
# Example: ID 125 converts to "cb"
print(encode_base62(125)) # cb
๐๏ธ System Components
- Frontend: UI or API for URL submission
- Application Layer: Logic for encoding/decoding and redirection
- Database: Store mapping between short code and original URL
- Cache: Redis for fast lookups on redirects
- Analytics Queue: Kafka or SQS for async click tracking
๐๏ธ Sample MySQL Table
CREATE TABLE urls (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
long_url TEXT NOT NULL,
short_code VARCHAR(10) UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
โก Redis Lookup Cache (Python + Redis-py)
import redis
r = redis.Redis()
code = "abc123"
# Check Redis first
cached_url = r.get(code)
if cached_url:
redirect_to(cached_url)
else:
long_url = db_lookup(code)
if long_url:
r.setex(code, 86400, long_url) # Cache for 1 day
redirect_to(long_url)
๐ Optional Feature: Click Analytics
- Push to Kafka topic on every redirect
- Async consumer stores in analytics DB or data lake
โ๏ธ Deployment Considerations
- Stateless services behind load balancer (NGINX, ALB)
- Read/write DB replicas for scaling
- Geo-distributed cache (Cloudflare Workers/Redis)
๐งช Observability
- Prometheus/Grafana for latency and cache hit ratio
- Datadog or OpenTelemetry for tracing redirection flow
๐ Final Insight
A URL shortener might seem simple, but it teaches key concepts: encoding, caching, database scaling, and queue-based analytics. It's a classic system design interview problem.
