CDNPSRockSolid CDN

Small-File Sites Hit Origin Too Often, So I Retuned Request Collapsing and Preheating

ArticlesCDNPS

A friend's asset site had a 92% hit rate but more origin requests than edge requests. The culprit was a cache-busting timestamp on every thumbnail URL. I walk through ignoring parameters by directory, when request collapsing actually helps, why preheating is a narrow tool, and the three metrics I watch for three days after any change.

A friend who runs an asset site sent me a backend screenshot last week. The origin request count was higher than the edge request count. My first thought was cache misses, but he said no, the hit rate was 92%.

That made no sense.

Digging through the logs cleared it up. His site is all small images, a few KB to a few dozen KB each. One detail page pulls in more than forty thumbnails, and every thumbnail URL has a timestamp parameter stuck on the end. The hit rate is calculated by request count, so it looks great, but the first time any new page gets visited, all forty-plus small files have to go back to origin. A user flips through ten pages and the origin gets hammered four hundred times. This kind of site is the hardest to serve: small files, huge counts, messy URLs.

I've seen plenty of people go straight for the request collapsing switch, turn it on, see no effect, and then blame the CDN. The problem is usually not there.

The thumbnail URLs on that site looked like this: /thumb/8821.jpg?v=1712043811. That v parameter was being appended by the frontend with Date.now() on every render. The intent was cache busting, but it completely wrecked the CDN cache. The same file, visited by ten thousand users, became ten thousand different cache keys.

The fix was crude: after confirming with the developers that the parameter had no business meaning whatsoever, I set the cache rule to ignore parameters. That one change cut origin traffic by more than half the same day.

There's a trap here worth spelling out. Ignoring parameters works per rule, so don't apply it site-wide with one blunt setting. His site also has APIs like /api/list?page=2&cat=3, where the parameters are the content itself. If you ignore those too, page two returns page one's data, and user complaints hurt more than origin traffic. My habit is to split by directory: ignore parameters for static asset directories, allow zero parameter ignoring for API directories, and give the versioned stuff in between its own rule. Suffix-based and directory-based rules will conflict, so you need to know which wins. I put the most specific rules first and always leave the site-wide fallback at the bottom.

Once the cache keys are clean, the remaining origin requests are genuine first visits. That's when request collapsing starts to matter.

Its logic isn't complicated: when multiple users request the same uncached file at the same moment, the CDN sends only one request to origin and the rest wait at the edge, then all get served together. For sites where one page pulls dozens of small images, the effect is the most obvious. After turning it on, his evening peak origin concurrency dropped from over three thousand to around eight hundred, and the origin load curve flattened visibly.

But there are prerequisites. The files need to be small enough and concentrated enough. Collapsing waits on the same file; if forty thumbnails are all unrelated, you still get forty origin requests, just queued more neatly. The origin also can't be slow. The collapsing window is short, and if the origin itself stalls for three to five seconds, every request waiting at the edge sits on the edge of timeout and the user experience gets worse. So check origin TTFB first; if it's over a second, fix the origin before turning anything on.

Also, don't enable it for dynamic APIs. Some people enable collapsing site-wide for convenience, and then login and checkout endpoints get collapsed, two users' requests get squeezed into one, and I've heard that story more than once. For layer-4 forwarding and dynamic requests, just connect straight through.

On preheating, my attitude has always been: avoid it if you can, and if you use it, don't expect it to put out fires.

It fits a narrow set of scenarios. Say a new version drops at ten tomorrow morning, the installer sits on your own origin, and tens of thousands of users will pour in within half an hour. Push the file to the edge ahead of time, the first batch doesn't wait on origin, and the origin doesn't get punched through in that instant. For predictable concentrated access like that, preheating pays off.

The scenarios where it doesn't fit are more numerous. A content site publishing hundreds of articles a day, you can't preheat them one by one. User-uploaded images, you have no idea who'll look at them next. Relying on preheating for those is just wasting API calls.

One more detail: preheating works by URL, and if the URL has parameters, you have to get them right when preheating, otherwise you push a different cache key and users still hit origin when they arrive. I've seen someone preheat a thousand URLs and the hit rate didn't budge, and it turned out the parameter order didn't match the actual requests. On our side, preheating and purging go through the open API with AK/SK signed calls, so we just hang a step in the release script. More reliable than clicking around the console, the fields aren't many, and you wire it once and use it for a long time.

Changing config isn't hard. Knowing whether you changed it correctly is. I watch three numbers for three days.

Origin requests divided by edge requests is the ratio that tells you the most. For a healthy small-file site, I expect it under 5%. If it won't come down after fixing the cache keys, the URLs themselves are probably too scattered and you need to go back to the developers. Origin bandwidth and origin QPS need to be watched separately; sometimes QPS drops but bandwidth doesn't, which means the remaining origin requests are all large files, and that's a different optimization problem. The last one is easy to overlook: origin error rate. Request collapsing and preheating both change the rhythm of origin traffic, and if the origin was already unstable at some concurrency point, a config change can amplify the problem. Watch origin 5xx for the first two days after any change.

The real-time access logs for the last 72 hours on the nodes are handy. When the numbers don't add up after a config change, reading the logs beats guessing. That's also how I located things during a CC attack once; I wrote up the troubleshooting order in /en/news/what-i-check-first-under-cc-attack, and the approach carries over.

His site now holds an origin ratio around 4%, and origin CPU dropped from a constant 70% to just over 30%. But there's one problem I still haven't solved for him: those thumbnails are generated on the fly, and the same source image can produce seven or eight sizes, each with its own URL. In theory the sizes should be fixed at generation time, but that's three-year-old code, and touching it pulls in everything around it. For now we're holding it together with cache rules, stretching TTLs for the two or three common sizes and keeping the rare ones shorter. That's treating the symptom.

For sites with lots of small files, what ultimately matters often isn't the CDN config, it's the URL design. I've said this to several friends. Not many listen. Most come back to fix it only after the origin bill arrives.