advanced
URL shortener
Design key generation, redirect latency, analytics, abuse controls, custom aliases, and hot-link caching.
A URL shortener maps short keys to long URLs with fast redirects, low latency reads, and collision-safe key generation. Core components: API for create/redirect, key generation (base62 counter or hash), relational or KV store for mappings, cache for hot links, and analytics pipeline optional.
Redirects are read-heavy — cache popular keys at CDN or Redis. Custom aliases need uniqueness checks. Plan abuse detection and rate limits on creation. Estimate billions of keys — 7-char base62 scales far.
On interviews: sketch create and redirect APIs, key generation strategy, storage schema, and caching for viral links.
Common pitfalls: sequential IDs enumerable by attackers; no cache on redirect path; analytics slowing redirect hot path.
The trade-off is flexibility versus complexity—know when the simpler path is enough.
Checklist:
- Design create and redirect flows.
- Choose key generation and collision handling.
- Cache hot mappings at edge or Redis.
- Add rate limits and abuse controls.