Skip to content
BytePatterns

Design a URL Shortener

System Design Cases: lesson 1 of 20

One tiny key, a hundred million long links behind it.

Lesson 1 of 20 · 6 min

Design a URL Shortener

Step 1 of 11

A hundred million links, and 500 reads for every 5 writes. The read path is the design.

The Idea

Take a long URL, hand back a short key, and redirect every later hit on that key. Assume 100 million links and 500 redirects a second against 5 writes — read-heavy, and every read is one exact-key lookup.

Real-World Example

A cloakroom ticket. The number you are given says nothing about your coat; the desk's ledger is the only thing that connects the two, and it is read far more often than it is written.

The Tradeoff

A base62 counter gives the shortest collision-free keys and leaks how many links exist, because one shared sequence hands them out in order. Random keys hide that and need a uniqueness check on every write. Redirect with 302: a 301 is cached by the browser forever, and your click counts stop arriving.

Your turn

Put the steps in the right order.

  1. Answer with 302 to the long URL and publish a click event
  2. Generate a short key and confirm no row already holds it
  3. On a redirect, read the cache first and fall back to the store
  4. Write key to long URL, then return the short link

Mini quiz

1 / 3

The redirect is a 302 rather than a 301 because:

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.