System Design · Senior · Question 18 of 75
How do you generate short links in a URL shortener?
- ✓Correct answer: A unique id from a counter, encoded in base62
- 2Random 3 characters without collision checks
- 3An MD5 hash of the whole URL, all 32 characters
Explanation
Base62 of a 64-bit id gives a short string without collisions; the counter is handed out in ranges so nodes don't sync on every request. Random short strings require collision checks; a full hash is too long. Reads are cached, and the redirect is 301 or 302 depending on whether analytics is needed.