As asked
Design a URL shortener that handles 10 million links, 1000 writes per second, and 50,000 reads per second. Users also need click analytics per link. Walk me through the data model, the short code generation strategy, and the read path.
Sample answer outline
The data model is a links table with columns for id, short_code, original_url, user_id, created_at, and a clicks table. Short codes can be base62 encoded random IDs or a hash of the long URL with collision detection. The read path is latency-critical so you cache the short_code to URL mapping in Redis with a high TTL, the redirect hits Redis first and falls back to Postgres. For analytics, writing a row per click at 50k req/s would saturate Postgres, so you buffer click events in Redis or a queue and flush to the analytics table in batches. A strong answer also covers custom slugs, link expiry, and the CDN as the outermost cache layer.
Expect these follow-ups
- How do you prevent the top-1000 links from becoming a hot spot in your Redis cache?
- How would you implement per-country click breakdown?