So you’ve just purchased a subscription to your shiny new CDN, and set it up on your site. You wait a short while and check the “hit percentage” tab on your CDN dashboard (different hosts might call it something else). And you’re disappointed to see <50% hit ratio!
Many CDN providers say that they offer as high as a 90% cache hit ratio. And for high traffic sites, it might even reach that number. But apart from a handful of providers, most people don’t see these high cache rates. In this article, I’ll explain your cache rates are probably low, and what you can do about it.
Reason 1: Cloudflare Cache Hit Percentage is Misleading
Because Cloudflare doubles up as both a CDN and a reverse proxy server, the cache ratio calculation isn’t as simple as it would be for any other CDN provider. That’s because unlike with a regular CDN, Cloudflare also servers your dynamic content in addition to static content.
And dynamic content is never cached, unless you’ve set up specific page rules to that effect. And despite Cloudflare’s claim to the contrary, I wouldn’t enable Railgun if I had a choice. So when Cloudflare calculates its cache hit percentage, this is the number they’re using:
Cached Requests/(Dynamic Requests + Static Requests)*100
Instead, we need to subtract all the HTML requests to the server. You can find this number in the “Performance” section of the “Analytics” tab in Cloudflare as shown here:
Once you make this correction, you’ll see a much better cache hit percentage on Cloudflare!
Reason 2: Too Many POP Servers
This might seem weird. After all, it’s a good thing if a CDN network has a lot of POPs right? Yes…and no. The thing is that each EDGE location requires at least one hit to cache resources. Sometimes, more than one hit! And when you have dozens or even hundreds of POPs, that means each has to get a request for every single static file on your site in order to cache it.
This is why cache rates take time to “build up”. For a short time, the cache hit percentage will be 0%. Then it’ll slowly start increasing as the caches fill up. But with a lot of POP servers, that build-up can take a while. Which is why you should only flush your cache rarely. A lot of work goes into “warming” it for everyone!
Reason 3: CDN Settings like Cache Control and Cookie Caching
From a technical standpoint, your origin server should be setting the “Cache-Control” header via something like this in .htaccess:
Add cache control and stale while revalidate or error directives like this:
#Add cache control and stale while revalidate or error directives <filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=2592000, stale-while-revalidate=604800, stale-if-error=604800" </filesMatch>
If not, make sure your CDN settings are configured to ignore cache-control headers.
Similarly, ensure that your CDN is stripping all cookies before caching static assets. Otherwise, each static file with a cookie will have its own cache, rendering everything useless.
Reason 4: Infrequently Accessed Content
Despite the cache-control settings above, most CDNs will still flush out your content if it’s not being accessed frequently enough. My personal tests put this at between 2-3 days. If a file hasn’t been accessed in that time, then your CDN will toss it out of that particular EDGE server even if it’s within the “max-expires” time frame.
This why Cloudflare’s “Cache Everything” can be detrimental to your site. If you have a page that only gets a few views every week, it’s absolutely useless to cache the dynamic content. And this is why caching works best only for frequently accessed content – and best when the resources are required to be loaded for every page on your site.
These reasons should help you troubleshoot why you feel you might be getting a low cache hit ratio on your CDN. I hope you find this useful!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply