URL SHORTENER

Short codes, hot links, and the collision you did not plan for

L318 MINSYSTEM DESIGN
PREDICT

You use the first 7 base62 characters of a hash as the code: 3.5 trillion possible codes. Roughly when do you have a 50% chance that two URLs share a code?

A shortener is two operations. POST /shorten turns a URL into a short code and stores code → url. GET /code looks the code up and returns a redirect (a 301 if browsers may cache it, a 302 if you need click counts). Reads outnumber writes about 100 to 1.

The code needs one property: it maps to exactly one URL, forever. A hash prefix is short and stateless, but different URLs can share a prefix. Then you need an atomic insert-if-absent and a rehash. A counter rendered in base62 never repeats. Reads need a cache: one viral link is a hot key, and the database cannot serve it alone.

▶ SIMULATION
APP-1
links issued 0
last —
idle
DATABASE (code → url)
empty
read requests last second 0 / 200
APP-2
links issued 0
last —
idle
CODE GENERATOR
scheme hash prefix, blind PUT
rehashes 0
code space 3844 (2 chars)
CACHE
none: every redirect reads the DB
hits last second 0
CLIENTS
redirects asked 0
dropped 0 · wrong page 0
—
last event—
hash collision odds50% after ~73 URLs in 3844 codes (7 chars: ~2.2M URLs)
✓ each issued code still maps to the URL it was issued for✓ DB reads per second <= 200
SCHEDULE
01[DESIGN]code = first 2 base62 chars of hash(url); blind PUT; cache-aside
02[APP-1]APP-1: POST /shorten A = https://shop.example/sale
03[APP-2]APP-2: POST /shorten B = https://docs.example/intro
04[APP-1]APP-1: POST /shorten C = https://news.example/story/3557
05[APP-2]APP-2: POST /shorten D = https://blog.example/post/1
06[CLIENTS]second 1: link A goes viral, 1,200 GET /(code of A)
07[CLIENTS]second 2: 1,200 more redirects
08[CLIENTS]second 3: 1,200 more redirects
09[CLIENTS]report
Start of hash prefix + cache scenario. 9 steps.
// code = hash prefix, blind write
code = base62(fnv(url))[0..7]
db.put(code, url)            // a second URL with the same prefix overwrites the first

// redirect
return 301 db.get(code)       // every click is a DB read

The sim uses 2-character codes (3,844 of them), so the collision shows up in four URLs instead of millions. URL C really hashes to the same code as URL A. Watch the DATABASE box and the invariant strip. HASH PREFIX repoints A’s code to C, and the cache then serves the wrong page 3,600 times. COUNTER, NO CACHE keeps every mapping but asks the database for 1,200 reads a second. Both fixes keep both invariants under the same four URLs and the same viral traffic.

BREAK IT NOT YET

Both app servers use check-then-insert: hash the URL, GET the code, PUT it (rehashing first if the GET saw it taken). URL A and URL C hash to the same code. Order the six steps so both finish and one of the issued codes points at the wrong URL.

GOAL: both links issued, and one code no longer maps to its URL

FIX IT NOT YET

Make shorten collision-safe. The grader shortens 300 URLs into a 3,844-code space, some of which collide, while another server writes to the same database. Every code you return must still map to its URL at the end.

Mental model:

a short code is a key you promise never to reuse. Either you generate keys that cannot repeat (a counter), or you generate keys that might, and make the database refuse the duplicate atomically. A check followed by a write is neither. The read path is a hot-key problem: the mapping rarely changes, so cache it aggressively (and invalidate on delete or takedown) and let one miss fill it for everyone.

IN PRODUCTION

Link shorteners, invite and referral codes, short share links, paste and gist IDs, any public ID that must be short and must not collide. The symptoms are a link that opens the wrong page and a redirect tier that falls over when one link trends.

IN INTERVIEWS

“Design a URL shortener.” On the whiteboard: requirements first (shorten, redirect, custom aliases maybe, links never change, reads about 100× writes). Then estimates: 100M URLs a month is about 40 writes and 4,000 reads a second, 6B rows and about 3 TB over five years, so 7 base62 characters is plenty. Then the design: app servers behind a load balancer, a key-value store for code → url, a cache in front, and a code generator. Say which generator you pick: a counter with ranges leased per server, or a hash with a unique constraint and rehash. A counter is enumerable, so shuffle or encrypt the id (or add a random suffix) if links must not be guessable. Say which redirect you return: a 301 is cached by browsers, so later clicks never reach you; a 302 keeps click counts and lets you repoint or take down a link. Then name the failure: a hash prefix collides at about 2.2M URLs, check-then-insert races, and a viral link is a hot key that needs a cache with coalesced misses.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: DESIGN A DISTRIBUTED RATE LIMITER →