Requirements
- Create a short code for a long URL and redirect on lookup.
- Codes must be unique, short, and hard to guess sequentially.
- Reads dominate writes, often by a hundred to one.
High level design
A write goes to an API server that generates a code and stores the mapping. A read resolves the code and issues an HTTP redirect.
- Code generation: a counter encoded in base sixty two, or a hash of the URL truncated and checked for collisions.
- Storage: a key value store mapping code to URL plus metadata like owner and expiry.
- Cache: hot codes live in an in memory cache so most reads never touch the database.
Bottlenecks
- Read hot spots: viral links concentrate traffic, so a cache layer and a CDN absorb the spike.
- Counter contention: a single global counter is a bottleneck, so hand each server a range of ids to allocate locally.
- Custom aliases: vanity codes need a uniqueness check before insert.
A redirect should return in single digit milliseconds. Use a permanent redirect for stable links and a temporary one when you need to count clicks on every visit.
Key idea
A URL shortener is a read heavy key value lookup, so the design centers on unique code generation plus aggressive caching in front of a simple store.