In this lesson · topic 6 of 7
Topic 6
Hot links, caches and 301 against 302
6 min read
Cache the hot links
Click traffic follows a skewed curve. A small share of links draw most clicks, the pattern called a Zipf distribution. Web request studies have found this shape for a long time. A cache that holds the popular links answers most clicks without touching the table.
In the lab, a cache of 100 links gave a hit ratio of 38 percentmeasured. A cache of 1,000 gave 65 percentmeasured, and 5,000 gave 82 percentmeasured. Flatter traffic helps less. At a skew of 0.6 the hit ratio was 22 percentmeasured, and at 1.4 it was 95 percentmeasured.
The code scheme does not move it. Random codes hit 65 percentmeasured and a counter hit 65 percentmeasured. Size the cache from the click curve, not from the code.
Put the cache in front of the table, keyed by code. Spread it over several nodes with consistent hashing so adding a node moves few keys. One link that goes viral can still overload a single node. Keep a short local copy on each web server for the very hottest codes. Expired and deleted links need a purge or a short time to live, or the cache keeps serving them.
301 or 302
The redirect status tells the browser what to do next. RFC 9110 defines both. A 301 says the link has moved for good. Browsers and caches may remember it and skip the server on the next click. A 302 says the move is temporary, so every click comes back to the server.
The choice is a trade-off.
- A 301 is cheap for the service. In the lab the server handled 72 thousandmeasured clicks instead of 120 thousandmeasured.
- A 301 hides clicks. The server counted 60 percentmeasured of all clicks, which breaks analytics, expiry and any rule to change the target later.
If the product sells click counts or lets people edit targets, use 302. If links are permanent and cost is the concern, 301 is fine. Some services use 302 with a cache header that allows a short browser cache, which gives a middle path.
Scaling the rest
Split the table by code, using partitioning. Reads can use replicas, which may lag a little. A new link that is read at once from a lagging replica returns not found, so read your own writes from the primary for a moment. See replication and lag. Put the redirect behind a CDN if links are permanent.