BOOKS DEEP-READ · SYSTEM DESIGN INTERVIEW VOL 1 · CH 8

Design a URL Shortener

System Design Interview Vol 1 · Ch 8 · Alex Xu · 2020

中文 →

What is this chapter about?

When you paste a monstrously long link into a social app, what comes out the other side is usually a seven-or-eight-character stub. Chapter 8 of System Design Interview asks you to build that service from scratch: turn a long address into a short one, and send whoever clicks it back to the original. The problem statement is small enough to look like homework, but its skeleton is the clearest in the book — one lookup table, one number issuer, one cache layer. All three big pieces of a large system, in miniature.

An analogy

Think of a coat check at a theater. You hand over a bulky coat (the long URL) and get back a small numbered tag (the short URL). The tag carries no information about the coat — it is a thin slip of cardboard. Everything depends on the ledger behind the counter that pairs each number with each coat. So the whole business comes down to two things: the rule for handing out numbers, and a ledger you can never afford to lose.

The hard part was never "make it short"

The difficulty is that three requirements have to hold at once. The tag must be short enough to copy by hand (about seven characters). Over ten years, hundreds of billions of tags must include no duplicate number, ever. And the several counters handing out tags should not have to consult each other — every round trip costs time.

There is also a property that is easy to miss: this business is write once, read many, in the extreme. A link is registered once and may be clicked tens of thousands of times. So the design weight naturally falls on the click side, not the creation side.

Two ways to hand out numbers

The first: take a fingerprint of the coat. Compute a characteristic value from the long URL and keep the first seven characters as the tag. Fast — but two different coats can fingerprint to the same value, and when that happens you tweak the input and recompute until it stops colliding.

The second: take a sequence number, then rewrite it. The counter hands out 1, 2, 3, and so on, and you rewrite that decimal number in a more compact notation that uses 62 symbols — 0-9, a-z, A-Z. (This is the same trick as writing a decimal number in hexadecimal: more symbols means fewer digits.) Seven positions over 62 symbols covers roughly three and a half trillion tags, which lasts centuries.

The stack of common tags on the counter

Since roughly ten thousand clicks follow every one registration, the counter does not need to open the ledger every time. Keep a copy of the most frequently requested number-to-coat pairs within arm's reach; almost everyone gets an answer on the spot, and only the obscure numbers require a trip to the ledger. That stack is the cache — and the fraction of requests it can answer is very nearly your server bill.

One choice hidden in the details

When you send someone on their way, you can say one of two things. Say "this address moved permanently" and the browser remembers it — next time the visitor flies straight there without passing through you, and you save machines. But you also never get to count that click. Say "go over there for now" and every click still comes through you, so you can record exactly who clicked what and when — at the cost of carrying all of that traffic yourself. Data or cheap servers: pick one.

And one honest cost: you have to keep that ledger alive forever — the day the service shuts down, every short link scattered across the internet dies at once.

Remember this one thing

A URL shortener is fundamentally a ledger you cannot lose: tags must be short and never repeat, reads vastly outnumber writes so the cache decides the cost, and saying "permanent" versus "temporary" on the redirect is a straight choice between cheaper servers and click data.

Want the mechanisms, the notation and the diagrams? → Switch to the deep read