URL Shortener: The Design
Putting it together.
We have requirements, estimates, and short codes. Now step three, the high level design. Two paths matter, creating a link, and following one.

- Requirements, estimates, codes
- Now the design
- Create and follow
Creating a link
To create a link, the client posts the long URL. The API server gets a new unique number from an ID generator, and turns it into a short code. It saves the code and long URL in the database. And it returns the short link.
- POST long URL
- Get unique ID
- Base 62 code
- Save to DB
- Return short link

Following a link
To follow a link, the browser requests the short code. The server checks the cache first. On a miss, it reads the database, and fills the cache. Then it answers with a redirect, status three oh one or three oh two, and the browser goes to the long URL.
- GET /abc123
- Check cache
- Miss: read DB
- Fill cache
- Redirect 302
Deep-dive choices
Some deep dive choices. A three oh one redirect is cached by browsers, which saves load, but hides clicks from our statistics. A three oh two is counted every time. The database is a key value store, sharded by short code. And popular links stay hot in a Redis cache.
| key | value |
|---|---|
| 301 | browser caches it, fewer requests |
| 302 | every click is counted |
| Database | key-value, sharded by code |
| Cache | Redis for hot links |
Summary
The shortener uses an ID generator and base sixty two to create links, and a cache in front of a sharded key value store to follow them. Next, a rate limiter as a shared service.
Key takeaways- ID → base 62
- Cache, then DB
- 302 to count clicks