CDNPSRockSolid CDN

A Client Said Their Pages Were Slow. The Cache Key Was the Problem.

ArticlesCDNPS

A client reported a three-second homepage load. Hit rate looked fine at 87%, but TTFB spiked every evening. The culprit wasn't the nodes — it was tracking parameters polluting the cache key, splitting one homepage into dozens of cached copies. Here's how I traced it and what I changed.

Last Wednesday afternoon, a client running a corporate site dropped a screenshot in the group chat: their homepage took over three seconds to open, and they wanted to know if our nodes were the problem. I didn't reply right away. I logged into the console and pulled the hit rate and TTFB for that domain first.

Hit rate was 87%, which doesn't look bad. But the TTFB line was interesting — around 80ms during the day, then climbing past 400ms once 8pm hit. Hit rate flat, TTFB rising. Put those two together and you can basically rule out the nodes themselves. If a node were actually failing, the hit rate would collapse first.

So the problem was on the origin side. Or more precisely, in what counts as "the same cache object."

Read the logs, don't guess

I had the client export 72 hours of access logs — you can view them straight in the console, no need for them to stand up their own logging stack. Filtering for origin requests turned up something pretty absurd: the same homepage was hitting origin under a whole zoo of URLs.

/index.html?utm_source=wechat, /index.html?from=baidu, /index.html?_t=1698..., plus a pile with spm in them. These parameters come from front-end analytics and campaign links. They have zero effect on page content, but by default the CDN treats the entire URL as the cache key, so a different parameter means a different object.

The result: one homepage cached in dozens of copies across edge nodes, each with a pitifully low hit count, expiring before it ever warms up, and the next wave of users triggers origin again. That 87% was propped up by the genuinely large files. For small files like HTML, the hit rate was actually dismal.

Why did TTFB rise at night? Because more people were hitting origin at night, and the origin was queuing. The origin is a 2-core, 4GB cloud server. It handles daytime fine, but once dozens of origin requests arrive at once in the evening, it starts gasping.

The cache key had things in it that didn't belong

The cache key is something nobody manages day to day, and when it goes wrong it's especially sneaky. It doesn't throw errors, it doesn't fire alerts. It just quietly inflates your hit rate and deflates your origin traffic, and then one evening it all blows up at once.

I usually check three places for cache key pollution.

First, URL parameters. Analytics params, campaign params, timestamps, random numbers — these are the most common. _t= cache-busters are especially annoying. Some old templates hardcode them, so every request carries a fresh one, which means that page never hits cache. Ever.

Second, request headers. Some sites put Accept-Encoding, User-Agent, or even Cookie into the cache key, intending to differentiate. The actual effect is splitting one page into dozens of versions. Mobile and desktop do need separate caching, but there's no need to split on every individual UA string.

Third, protocol and port. http and https counted separately, with and without www counted separately — those are reasonable. But if a site is reachable from two domains without normalization, the cache gets split in half.

This client's case was the first one. I had them add utm_*, from, spm, and _t to the ignore-parameters list in their cache rules, matching HTML by suffix with the ignore-parameters option on. About ten minutes after the change, origin QPS in the evening dropped from 200-something to around 40.

TTFB came down with it. Checking again at 9pm, it was steady around 120ms. The client said the homepage went from over three seconds to just over one.

While I was in there, I cleaned up prewarm and purge too

There was a side effect after fixing the cache key: those dozens of parameterized HTML copies were still sitting on the nodes, waiting to expire naturally. I had them do a full-site purge, then prewarm the homepage and a few section pages.

A lot of people think prewarming is for large files. Actually, HTML needs it more. One homepage origin fetch might only take a few hundred milliseconds, but during evening peak, hundreds of users trigger origin at once and the origin can't keep up. Pushing hot pages to the edge ahead of time flattens that origin spike.

I wrote about the specifics of tuning prewarm and merged origin requests before in Small-file sites hitting origin too often: how I tuned merged origin requests and prewarm. The approach carries over, so I won't repeat it here.

One more thing: this client's origin is connected by domain name, and DNS resolution occasionally drifts to a slow IP during origin fetches. RockCDN's origin optimization automatically picks the fastest one, and it recognizes CloudFront origins too. They hadn't turned this on before, so I enabled it while I was in there.

Don't just look at the total hit rate

One habit of mine to close with. I never look at just the total hit rate — I always break it down. By file type, by directory, by status code.

A site with 95% overall hit rate sounds great, but if HTML is only hitting 30%, the speed users actually feel is a different story. HTML is the first screen, it's the direct source of TTFB. If it's slow, it doesn't matter how well the images behind it are cached.

Conversely, a download site with 70% overall hit rate but 99% on APKs and archives is fine, because those are where the traffic actually is. For how to set caching on large files like APKs, see How I set up caching for an APK download site these days.

So when a client reports "slow," my order now is: look at the shape of the TTFB curve first, then the hit rate breakdown, and only then dig through logs for specific URLs. Most of the time the problem is identifiable at step two, and cache keys are the most common category.

The one thing not fully solved this time is that _t parameter — it's hardcoded in the front-end template, so their developers have to change the code. Ignoring parameters on the CDN side covers it, but every request still carries a useless string of characters, which wastes some bandwidth. I told them to fix it when they get a chance. Probably lands next sprint.