Hot links
Redirects are spread unevenly: about 20% of links get 80% of all redirects. Most links are opened a few times and forgotten, while some link from the news gathers thousands of redirects a second. A design that holds the average 4,000 requests a second can fall over from one such link.
Imagine a cinema box office on a premiere day. Almost everyone asks for the same film, and the cashier keeps those tickets at hand instead of running to the storeroom for them. A cache does the same for popular codes. The analogy ends at the storeroom: the cashier has one, while our store is split into partitions and replicas.
A cache in front of the store
The redirect service looks the code up in the cache first. If the code is not there, the service reads the store, answers the client and puts the code, its long URL and its expiry date into the cache. The cache keeps the links clicked in the last day:
- each entry lives 24 hours;
- when memory runs out, the entries not accessed for the longest time are evicted first;
- by the estimate from the load lesson this is a few gigabytes, and it fits in one machine's memory.
The cache here is Redis. The databases course covers key TTLs, eviction and the "cache first, database on a miss" pattern in detail, in the lesson Cache patterns. In an interview it is enough to name the pattern and its cost: the first redirect of a link still goes to the store.
The cache hardly ever goes stale, because a link's URL cannot change after creation. The exceptions are expiry and deletion. The expiry date sits in the cache next to the URL, and the redirect service checks it on every answer. On deletion the link service removes the code from the cache at once, so a deleted link does not keep working until the entry's day runs out.
One viral link
If one code gets 10,000 redirects a second, that is 10,000 requests a second to one cache key. The key lives on one Redis node, and that node takes the whole wave alone.
So each redirect-service instance also keeps a hot link in its own memory for a few seconds. When the copy expires, one request goes to refresh it, and the others wait for its answer. This way the burst falls entirely neither on one cache node nor on the store: only rare misses reach the viral link's partition.
301 or 302
A redirect can be answered with two status codes, and the choice between them is a classic question:
301is a permanent redirect. The browser caches it, often for a long time, and later clicks go straight to the long URL, bypassing us.302is a temporary redirect. The browser does not cache it, and every click reaches the redirect service.
301 would cut the load, but repeat clicks would vanish from the counts: the browser tells us nothing about them. And by the requirements the owner sees how many times each link was opened. So the answer is 302, and the cache takes the load of up to 40,000 redirects a second.
What about a CDN
Users live all over the world, and a CDN — a network of content delivery nodes close to users — could answer redirects from the nearest node. But an answer the CDN served from its own copy never reaches our service, and the click does not get counted. Then statistics would have to be gathered from the CDN's logs.
This is the same choice as between 301 and 302: the closer to the user an answer is cached, the lighter our load and the less we see.
Now find both problems on a diagram.
At peak there are 40,000 redirects a second, 20% of links get 80% of them, and the owner sees how many times each link was opened. Right now some redirects never reach the statistics, and nearly all the rest go to the store. Fix the diagram.
Key points
- 20% of links get 80% of redirects, so a cache of a few gigabytes takes most reads off the store.
- A viral link is also kept in each redirect-service instance's memory for a few seconds.
301is cached by the browser, and repeat clicks never reach the counter;302is not.- A shortener with statistics answers
302, and the cache takes the load.