System Design · 09. Whole Systems · 14 min · Intermediate

Design a URL Shortener

Turn long links into short codes, and redirect billions of clicks fast.

In short

100,000,000 links a month means 39 writes and 11,700 reads a second at peak. A scrambled base62 counter gives 3.5 trillion unique codes; a cache cuts store lookups to 1,170 a second and redirects to 11.5 ms.

Shards hold the 3 TB, a queue counts clicks off the hot path, and a checker and rate limit stop abuse.

Key ideas

  • Requirements: unique short codes, fast redirects, links never silently change, and click counts. (step 1)
  • Unique is not the same as unguessable. Do not depend on a secret code alone for private data. (step 6)
  • Move work that the user does not wait for out of the request, onto a queue. (step 10)

Good to know

  • 7 characters is short, never collides, and has room for a thousand times more links. (step 5)
  • Read-heavy and rarely changed is the best case for a cache. (step 7)
  • Shard by the code: a random code spreads the rows evenly. (step 9)

Watch out

  • The read peak, not the storage, is the first problem. (step 2)
  • It works on a laptop and fails at the first busy hour. (step 3)
  • A truncated hash is short, but it is not unique. Collisions arrive much sooner than people expect. (step 4)
  • Browsers may cache a 301 for a very long time, so a wrong target is hard to fix later. (step 8)
  • Abuse protection belongs on the create path, which has only about 39 writes a second, not on the busy read path. (step 11)
UsersLoad balancerShortener serviceCacheClick logLink storeClick eventsShard 2Shard 3

Key numbers

Redirect time
11.5 ms
Store lookups/s
1,170/s
Short codes
3.5 T
Links kept
6 billion

Terms

Base62
Writing a number with 62 symbols (0–9, a–z, A–Z) so it takes few characters.
Hash collision
Two different inputs that give the same hash, so they would get the same short code.
Read-heavy workload
A system where reads far outnumber writes, so the read path is sized and cached first.
Guessable codes
Sequential codes let a stranger walk through other people's links; scramble or randomise them.
301 redirect
A permanent redirect that browsers may remember, so repeat clicks skip the server.
302 redirect
A temporary redirect: every click reaches the server, so it can be counted.
Click events queue
A queue that takes click records off the redirect path so counting never slows it.
Link checker
A check of each new long URL against known bad sites before a short link is made.