Case Study: URL Shortener (TinyURL / bit.ly)
100:1 read-heavy redirection using Base62 IDs, Redis caching, and 301/302 trade-offs.
In plain words
Save a map from a tiny 7-letter code like aB9x2Kq to the full long URL. Cache popular short links in Redis so redirects happen in 1 millisecond.
Think of it like…
A speed-dial number (*12) that dials a full 15-digit international phone number.
Theory & how it works
Why 7 Base62 characters (`[a-zA-Z0-9]`) is the sweet spot
62⁷ ≈ 3.52 trillion unique short codes. Even at 100 million new URLs created per day, 7 characters lasts 3.52 × 10¹² / 10⁸ ≈ 35,200 days (~96 years)! Generate unique counter IDs via a Ticket Server / Snowflake / Redis range allocator and Base62-encode them so collisions are zero.
301 vs. 302: 301 Moved Permanently lets browsers cache the redirect locally (cheapest for your servers, but breaks click-tracking analytics). 302 Found hits your server every click so you can log analytics accurately.
Key points & practical guide
Summary checklist
- Base62 over unique distributed counter avoids collision retries
- Cache hot short codes in Redis (~20 GB holds top daily traffic)
- 302 for analytics tracking; 301 for maximum caching
When to use it
- Classic beginner-friendly HLD warm-up problem
Watch out for
- Doing synchronous analytics SQL writes on the redirect critical path - emit to Kafka instead