System Design Advanced
05 Capstone: URL Shortener
The classic, end to end. A URL shortener looks trivial until the numbers arrive -- which is exactly why it tests everything at once: estimation, key design, storage, caching, and the evolution story when product asks for more.
Design a URL shortener end-to-end for: 100 million new links per month, 10 billion redirects per month, links must keep working for years. Cover: the API, short-key generation (length, alphabet, collision story), the storage schema with a size estimate, the redirect read path with caching, and your QPS numbers.
Hints
- Numbers first: a month is about 2.6 million seconds. Derive writes/s and redirects/s before drawing anything; the read:write ratio should drive the whole design.
- Base62 over N characters gives 62^N keys. Pick a length by comparing that keyspace to the links you accumulate over years. Generate from a counter (collision-free, guessable) or randomly (check-and-retry) -- pick one and defend it.
- The redirect path should almost never touch the database: short-key mappings are immutable, which makes them the easiest thing in the world to cache.