31. Design a URL Shortener
1
The interview prompt2
31.1 What do you ask first?3
31.2 How big is this, really?4
31.3 What are the doors?5
31.4 What do we store?6
31.5 What does the whole system look like?7
31.6 How do you generate the short code?8
31.7 301 or 302?9
31.8 How do you keep redirects fast at 12,000 a second?10
31.9 How do you count clicks without slowing redirects?11
31.10 What would you say in the last five minutes?12
Score yourself13
Variations to practise14
Sources15
Code lab31. Design a URL Shortener
Variations to practise
3 min read+10 XP
Chapters used
Approach (1), Estimation (2), Load balancers (7), SQL vs NoSQL (8), Unique IDs (10), Caching (12, 13), Replication (14), Message queues (22), APIs (24), Rate limiting (25), Failures (29).
Try these variations
Each is a common follow-up. Answer in two minutes, then check the sketch.
- "Add custom aliases." → Store them in the same table; the alias is the code. Check it's free before inserting (the primary key does this) and keep aliases out of the generated-code space, for example by requiring at least one character a generated code can't have, or a minimum length.
- "Links should expire." → Add
expires_at. Check it on redirect (return 404 or 410 when expired), set the cache TTL no later than the expiry, and run a cleanup job. - "Make it work across two regions." → Each region gets its own ID blocks so codes never clash, writes go to one primary or are replicated between regions, and DNS sends users to the nearest region (Chapter 4).
- "10 times the traffic." → Recalculate: 120,000 redirects a second at peak. Add Redis nodes (shard by code, Chapter 13), more replicas or a CDN for redirects. Writes are still small.