Step 1 · The pattern
Every click reaches the origin
Clicks arrive at a CDN edge near the user, which forwards each one to your origin: the shortener and its database. With ordinary traffic the origin has plenty of room.
- click
- redirected
In short
Hot link + CDN: how it works and how it breaks
A link shortener is almost all reads: a link is created once and may be clicked millions of times. Usually clicks are spread thinly over many links. Then one link is posted somewhere huge, and a large share of all traffic is for a single code. If every click travels to your servers and database, that one link can saturate them, and then every other link fails too. The answer for a redirect is the same every time, which makes it ideal to cache close to the user. Put a CDN in front and let it keep each redirect briefly, for example with Cache-Control: max-age=5. The edge answers nearly all clicks itself, and the origin sees roughly one request per link per interval, however viral it is. The costs are small: a change takes a few seconds to appear, and clicks are counted from edge logs.
How this comes up in interviews
"One link on your shortener goes viral and takes a large share of all traffic. How do you keep the whole service from going down?"
Then they ask
- How long would you let the edge cache a redirect, and what do you give up?
- If the edge answers most clicks, how do you still count them?
- How many requests does the origin see for a very hot link, and what does that number depend on?
A strong answer covers
- A shortener is almost all reads, and the answer for a code is the same for everyone, so it is ideal to cache near the user.
- Without caching, one viral link can saturate the origin, and then every other link fails too.
- Put a CDN in front and let it keep each redirect briefly, for example Cache-Control: max-age=5.
- Origin load then depends on the number of edge locations and the cache lifetime, not on how many people click.
- The costs: a change takes a few seconds to appear, and clicks are counted from edge logs.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: RFC 9111: HTTP caching ↗ · Scaling Memcache at Facebook (NSDI '13) ↗ · High Scalability: Bitly, lessons learned building a distributed system ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.