URL Shortener Design is a case-study chapter for designing a URL shortener at large scale. The objective is to move from requirements to APIs, capacity estimates, storage choices, cache strategy, scaling strategy, and failure handling.
API Design
- POST /urls creates a short code for a long URL
- GET /{code} redirects to the long URL
- GET /urls/{code}/stats returns click analytics
Capacity Estimation
- Reads usually dominate writes because redirects are frequent
- Estimate redirect RPS, new URL writes per day, mapping size, and click event volume
- Design redirect path for very low p99 latency
Database Selection
- Use a key-value or relational table keyed by short code
- Store code, long_url, owner_id, created_at, expiry, and status
- Use append-only analytics storage for click events
Cache Layer
- Cache code to long_url mappings in Redis
- Use CDN or edge cache for extremely hot public redirects when safe
- Expire or invalidate cache when a URL is disabled
Load Balancing
- Route redirects through global load balancers
- Keep redirect services stateless
- Use health checks to remove unhealthy instances
Sharding Strategy
- Shard by short code hash
- Avoid sequential code ranges that create hot partitions
- Keep analytics partitioned by code and time
Replication Strategy
- Replicate mapping data for high read availability
- Use asynchronous analytics pipelines for click events
- Route critical admin reads to primary if immediate consistency is required
Failure Handling
- Serve cached mappings if the database is briefly unavailable
- Fail closed for disabled or abusive links
- Buffer analytics events when the stream is slow
Real Production Considerations
- Abuse detection and phishing takedowns are essential
- Redirect latency is the product experience
- Analytics accuracy can be eventually consistent