Answer
Requirements: create a short alias for a long URL, redirect quickly, and optionally support custom aliases, expiry and click analytics. Traffic is read-heavy, often around 100 redirects per URL created.
- Key generation: encode a unique numeric ID in base62; seven characters give about 3.5 trillion keys. IDs can come from a distributed counter or from ranges pre-allocated to each server, which avoids the collision checks that hashing the long URL would require.
- Storage: a key-value or wide-column store mapping short key to long URL, which shards easily by key.
- Read path: cache hot keys in Redis in front of the database, so most redirects never reach storage.
- Trade-off: a 301 (permanent) redirect lets browsers cache it and cuts load, while a 302 routes every click through your servers and enables analytics.
More in Web Services
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | Design a URL shortener | Concept | Medium | Read the answer to Design a URL shortener | |||
| 2 | Design a pastebin service | Concept | Medium | Read the answer to Design a pastebin service | |||
| 3 | Design a rate limiter | Concept | Medium | Read the answer to Design a rate limiter | |||
| 4 | Design a notification system | Concept | Hard | Read the answer to Design a notification system |