URL Shortener: Scaling It
Minutes twenty to thirty belong to the interviewer's pushes. The first design works, and now the interviewer leans on it in five places, one after another. Each push gets one change, and each change gets its cost in the same breath. By the end of the ten minutes the first design has become the full one, and every new box on the board is there because a push put it there.
The five pushes, in the order they come:
- Redirects outnumber new links a hundred to one, and all of them hit the one database.
- One link goes viral and draws tens of thousands of reads a second on a single key.
- Five years of links no longer belong on one machine.
- Once the table is split, the auto-increment that made keys unique is gone.
- Keys handed out in order can be walked by anyone with a loop.
The Read Path
"Twenty thousand redirects a second, and every one goes to the database. Where should they go?" The answer is a cache in front of the store, filled on a miss and keyed by the short key. Size it from the working set, as Chapter 2 does: if the links of the last thirty days take most of the clicks, that is 300 million links at 500 bytes, 150 gigabytes, which two mid-size cache nodes hold, doubled for replicas.
At a 99 percent hit rate, the store sees one redirect in a hundred: 200 a second out of the 20,000 at peak. Read replicas behind the cache take those misses and keep the primary for writes. What it costs is a second system to run and a new question, how long a deleted link stays in the cache, which the curveballs come back to.
The Viral Link
"Someone with millions of followers posts one of your links. What happens to that key?" Say it is 50,000 reads a second on one key. Partitioning cannot help, because one key lives on one cache node however many nodes there are. That node takes the whole load, and if it is lost, the whole load falls on the store at once.
The change is a small cache inside each application server for the hottest keys, with an expiry of a few seconds, and misses on the same key collapsed into one request while it is in flight. The viral link is now answered from the memory of every server in the tier, not from one node. What it costs: each server can serve a link for those few seconds after it was deleted, a staleness bound you now have to state.
Five Years of Growth
"What does this table look like five years from now?" It holds 18 billion links, 27 terabytes with three copies, and a create is confirmed once two of the copies have it. The table is split by a hash of the short key. That choice is easy here for the reason noted in Scope: every lookup is by key, so no query ever has to visit two partitions. Assume about 2 terabytes per storage node, and 27 terabytes is about 14 nodes before growth.
Hashing the key spreads new and old links evenly, so no node takes all the fresh writes. What it costs is the thing the first design got for free. The auto-increment no longer exists as one sequence, and that is the next push.
Where Keys Come From
"With fourteen nodes, who hands out the next number?" A counter on each node would hand out the same numbers twice. A central counter asked once per link puts a network call and a single point of failure in front of every create. The change is a small replicated counter service that hands each application server a block of numbers, a hundred thousand at a time, which the server spends locally.
At about 200 new links a second at peak, the whole fleet spends a block in 500 seconds, about eight minutes. So the counter service sees a request every few minutes, and it is nowhere near the hot path. A server that crashes loses the unused part of its block, which wastes some numbers and harms nothing. What it costs is that keys from different servers are no longer in creation order, which nothing in this design needs.
Keys Nobody Can Walk
"Your keys come out of a counter. Couldn't someone just count through them?" They could: whoever has one key can try the next. That matters, because people share links with a few colleagues as often as with the world, and walking the key space finds the private ones.
The change is to pass each number through a keyed one-to-one mapping over the seven-character space before encoding it, a small cipher whose output stays inside the same range. Every number still gives exactly one key, so keys stay unique, and neighbouring numbers give unrelated keys. What it costs is a secret to keep and rotate, and keys that no longer sort by time. This is also why the key is always seven characters: the mapping fills the whole space from the first link.
The Full Design
The read path now runs from the load balancer to application servers that keep a few seconds of the hottest keys, then to the shared cache tier, and only on a miss to the link store, split across about fourteen nodes with replicas. The write path takes a number from the server's block, scrambles it into a seven-character key, and writes one row to the partition that key hashes to.
Every box that is not in the first design answers one of the five pushes, and each one carries a cost you have already said out loud. The figure marks what changed.
- Answering every push with "add a cache", including the ones about keys and growth. The interviewer hears one tool used on every problem.
- Partitioning by creation time, which sends every new link to the same node and every viral link to the newest one.
- Putting the counter service on the read path, where it would turn a lookup into two calls.
- Treating the local cache's few seconds of staleness as free, then failing the takedown curveball.
- Every change tied to the push that forced it, said in the same sentence.
- A cost named with each change, before the interviewer has to ask for it.
- The hit rate, the node count and the block size given as numbers, not adjectives.
- The partition key chosen from the access pattern agreed in Scope.
Knowledge Check
The cache holds 99 percent of 20,000 redirects a second at peak. What reaches the link store?
- About 2,000 reads a second, a tenth of all the redirects
- About 200 reads a second, the one redirect in a hundred
- Nothing at all, since the cache now answers every redirect
- Only the 200 writes a second, because every read is cached
What does the local hot-key cache in each application server cost the design?
- A network trip to every server for each redirect that is served
- A few seconds in which a deleted link can still be served
- Duplicate short keys whenever two servers create links at once
- The whole working set copied into the memory of every server
Why is the link table partitioned by a hash of the short key?
- So that links created on the same day stay together on one node
- So that the cache tier and the store always hold the same keys
- Every lookup is by key, so no query ever has to visit two partitions
- So that nobody can guess the next key from the last one created
An application server crashes with 60,000 numbers left in its block. What does that cost?
- Those 60,000 numbers are reused, and some keys collide later on
- Every server stops creating links until the block is recovered
- The counter service must replay its log to find which numbers were used
- Nothing that matters: the unused numbers are skipped for good
You got correct