6-step system design answer: URL shortener example
45-60 minutes: about 5 min requirements, 5 estimates, 15 design, 15 deep dive, 5 wrap-up
1. Requirements: Shorten a URL, redirect fast, optional custom alias and expiry. Non-functional: high availability, low redirect latency, reads far more than writes.
2. Estimates: Say 100 million new URLs a month and a 100:1 read ratio. That is roughly 40 writes and 4,000 reads per second on average, and about 6TB of storage over five years at 1KB per record.
3. API: POST /urls returns a short code; GET /{code} returns a 301 or 302 redirect.
4. High-level design: Load balancer, stateless app servers, a key-value or relational store mapping code to URL, and a cache like Redis in front for hot codes.
5. Deep dive: Generate codes with base62 from a unique ID generator to avoid collisions; cache popular redirects; shard by code; use 302 if you need click analytics.
6. Trade-offs and wrap-up: Consistency is relaxed for analytics but strict for code creation; discuss abuse prevention, rate limiting and monitoring.