URL SHORTENER
Short codes, hot links, and the collision you did not plan for
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.
// 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// option 1: counter (no collisions until the space runs out)
id = idService.incr() // or lease ranges of 1,000 ids per app server
code = base62(id)
db.put(code, url)
// option 2: hash, but insert atomically and rehash on conflict
for (salt = 0; ; salt++) {
code = base62(fnv(url + salt))[0..7]
if (db.insertIfAbsent(code, url)) break // unique constraint / conditional put
}
// redirect: cache-aside, coalesce concurrent misses
url = cache.get(code) ?? singleflight(code, () => db.get(code)) 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Platform, API and technical PMProduct Manager
- Working with engineeringProduct Manager