Design a URL shortener
medium 45 minTurn a long URL into a short code and redirect anyone who opens it.
Explain your choices
Answer first. Then compare your reasoning.
12 questions 0 got it 0 to review12 not marked
Summary
Trade-offs and cheat sheet
Decisions
| Decision | Choice | Because |
|---|---|---|
| 301 vs 302 | 302 | Links can change and clicks can be counted, at the price of load |
| Base62 vs Base64 | Base62 | URL-safe without escaping |
| Hash vs counted IDs | Ranges + scramble | No collisions, fast, resilient |
| SQL vs NoSQL for links | DynamoDB | Pure key-value access |
| Sync vs async analytics | Async (Kafka) | Decoupled from the hot path, absorbs bursts |
| One vs two CDNs | Ready for two | Failover, with more complexity |
When they push back
| They say | Answer with |
|---|---|
| "Why not just PostgreSQL?" | "At 3 TB I would. At 183 TB with only key lookups I prefer a store that partitions for me. I would rethink if you need rich queries on links." |
| "ZooKeeper for a counter?" | "It is light use. If the team does not run it, one compare-and-set row in a strongly consistent store does the same job." |
| "Your edge cache breaks analytics." | "Agreed, origin cannot see those clicks, so they come from edge logs. If logs are too late for you, I drop edge caching for tracked links." |
| "Isn't 302 slower?" | "Slightly more traffic for us, yes. In return links can be edited and counted." |
| "Is all this needed?" | "Not at first. I would ship version 2 and add each part when its number arrives." |