// The Design Playbook

Design a URL shortener

System Design track · 20 min

By the end of this lesson you can
  • Run all four playbook steps on a real system, from requirements to deep dive.
  • Work out how many characters a short code needs, and compare a counter with a hash for generating it.
  • Choose between a 301 and a 302 redirect based on what the business needs.

It looks like a toy: paste a long link, get a short one. You have seen these links in WhatsApp groups and on Instagram bios a thousand times.

Yet inside this toy live estimation, hashing, caching and a redirect choice that is worth real money. By the end of this lesson you will be able to design it on a whiteboard, calmly, using the four steps from the playbook lesson.

Step 1: requirements
  1. What it does: given a long URL, return a short code. Given the code, send the user to the long URL, fast.
  2. Scale: 100 million new links a day, and about 10 clicks for every link created. Reads far outnumber writes.
  3. Keep links for 5 years. Redirects must feel instant, and the service must never lose a mapping between a code and its link.
Step 2: the quick maths

Writes: 100M a day ÷ 86,400 ≈ 1,160 per second. Easy for one database.

Reads: ten times that, about 11,600 per second. Worth a cache, but not scary.

Storage: each record holds a 7-character code, the long URL (around 80 characters on average) and a couple of dates, so about 100 bytes. 100M × 100 bytes = 10 GB a day. Over 5 years: 10 GB × 365 × 5 ≈ 18 TB.

Conclusion: a few stateless web servers, one strong database with replicas, and a cache in front of it. No sharding needed, yet.

Step 3: the sketch

The API has two endpoints. POST /api/shorten takes a long URL and returns a code. GET /{code} looks the code up and redirects the browser.

The data is one table: code, longUrl, createdAt, expiresAt. Behind the API: stateless web servers, a cache holding the most-clicked codes, and one database that holds the truth. That is the whole system.

WRITE — create a short linkClientAPIvalidate & orchestrateCode generatorbase62Databasecode → long URLPOST /shortenmint codestoreREAD — open a short linkClientAPICachehot linksDatabaseGET /abc123lookupon miss302 redirect ← long URL
Trace both paths. Write: POST, generate a code, save to the database. Read: GET, check the cache, on a miss read the database, then a 302 redirect.
The idea

Step 4, the deep dive. The numbers say the load is small, so the real difficulty is not scale. It lives in two decisions: how to generate the code, and which kind of redirect to send.

That is the pattern of every design question. Once the numbers are on the table, the hard part is usually a couple of choices that each have a price.

How long must the code be?

Codes use base62: the digits 0–9, the letters a–z and A–Z, so 62 possible characters in each position.

How many codes do we need? 100M a day × 365 × 5 years ≈ 183 billion.

Six characters give 62⁶ ≈ 57 billion codes. Not enough. Seven characters give 62⁷ ≈ 3.5 trillion. Plenty, about 19 times what we need. So the code is 7 characters.

Generating the code: counter or hash?

Option one: a unique counter, from the database or from an ID generator like the one in the previous lesson, written in base62. No collisions ever, and the codes are short. The catch: codes come out in order, so anyone can guess the next one and walk through your links. If that matters, shuffle the numbers before encoding.

Option two: hash the long URL (with MD5, for example) and keep the first 7 characters. No central counter is needed. But two different URLs can produce the same 7 characters, so you must check the database and retry with a small change if the code is taken.

The counter is simple but central. The hash needs no shared state but needs collision handling. Both can be defended, and defending your choice is the point.

The redirect is a business decision

A 301 redirect means 'moved permanently'. Browsers remember it and may never ask your server again. Your load drops, but you stop seeing clicks, so your analytics go blind.

A 302 means 'moved temporarily'. Every click passes through your server. You can count everything, and you carry all the load.

Neither is wrong; choose on purpose. If your product sells click analytics to marketers, it must be 302. If it is a plain redirect tool, 301 is a gift. Either way, the cache sits between the web servers and the database. A small share of popular codes gets most of the clicks, so a modest cache absorbs most reads.

Common mistake

The common mistake is spending the whole discussion on sharding and fancy databases for a system that, by your own numbers, fits on one machine. The estimate said 1,160 writes per second. Let it win the argument, and put your energy into codes and redirects instead.

Test yourself

Why does a URL shortener taking 100 million new links a day still not need sharding? And what would change your mind?

Answer in your head first, then check.

Takeaway

Run the playbook from start to finish and the 'hard interview question' becomes routine: two endpoints, 1,160 writes and 11,600 reads per second, four boxes. Then spend your energy where the real arguments are: how codes are made and what kind of redirect you send.

Do it this week

Open a short link you received recently in your browser's developer tools (Network tab) and look at the response: is it a 301 or a 302? Then guess why that company chose it, given how it makes money.

What you can do now
  • Run all four playbook steps on a real system, from requirements to deep dive.
  • Work out how many characters a short code needs, and compare a counter with a hash for generating it.
  • Choose between a 301 and a 302 redirect based on what the business needs.

The Design Playbook