Skip to main content
Common Designschapter 1 of 8 · 4 lessons

URL Shortener

Map a short code to a long URL. Read-heavy, globally cached.

You paste a link into a message and it comes out seven characters long. Somebody clicks it, and about 40 milliseconds later they are on the original page.

Between those two moments a server took those seven characters, found the address they stand for among six billion others, and sent the browser onward. It did that 40,000 times that second, because links get clicked far more often than they get made.

That ratio is the whole design. Roughly forty new links a second, forty thousand redirects a second, off the same table.

Interviewers keep asking it because it is small enough to finish and still contains generating unique identifiers, caching strategy, and the redirect decision that quietly determines whether you have a business.

Lessons

4 in this chapter
  1. Requirements and EstimationPin down the read-write ratio first, because it decides the whole architecture.2 min
  2. Short Codes and the Data ModelBase62 over a unique ID gives 3.5 trillion codes in 7 characters, no collisions ever.3 min
  3. The Read PathA redirect is one cache lookup; the database should almost never hear about it.2 min
  4. 301 vs 302 and the Analytics PipelineOne HTTP status code decides whether you ever see the click again.2 min