URL shortener
Short codes from a counter, not a hash, and a read path that almost never touches the database.
- source
- Classic system design
- tags
- Postgres · cache · redirects
The problem
Turn a long URL into a short one. Redirect fast. Never hand out the same code twice.
Requirements and scale assumed
- 100 writes per second, 10k redirects per second. Reads dominate by two orders of magnitude.
- Codes should be short (7 characters) and not guessable in sequence.
Design
- 01WriteTake the next id from a counter, encode in base62, store id → url
- 02ReadCache lookup first; on a miss, one primary-key read and fill the cache
- 03Redirect301 for permanent links, 302 when click analytics matter
Counter plus base62, not a hash of the URL
- why
- A counter never collides. A hash needs a collision check and a retry path on every write.
- cost
- Codes are sequential and reveal volume. Multiply the id by a large odd constant modulo 62^7 before encoding to scramble them.
Still to write
Analytics pipeline, custom aliases, and what happens when the counter range runs out.