Design a URL shortener (like TinyURL / Bitly)
Key points
- ✓ Read-heavy system: optimize the redirect path above all.
- ✓ Generate a short, unique key with base-62 over a global ID (or a hash + collision check).
- ✓ Store
short_key → long_urlin a fast key-value store; cache hot keys. - ✓ Redirect with
301(permanent, cacheable) or302(if you need click analytics).
High-level design
Request flow
- Create: API gets a unique id from the ID service, base-62 encodes it to a short key, stores
key → url, returns the short link. - Redirect: look up the key in cache; on a miss read the DB and populate the cache; return a redirect.
- Analytics: push the click event to a queue so counting never slows the redirect.
Deep dives & trade-offs
- ✓ Scale reads with cache + read replicas; the store is a natural key-value partition.
- ✓ Custom aliases & collisions: reserve/verify on write; hashing needs a collision check, counters don't.
- ✓ Capacity: ~62^7 ≈ 3.5 trillion 7-char keys — plenty. Add TTL/expiry if links should die.