Cloudflare Cache with manual revalidate

Hello I am working with cloudflare for few years. I am still learning a lot.

right now i have cache setting like below
-> Browser cache ttl 30min
-> Always online on
-> Cache everything
-> Edge Cache TTL a month

I don’t want strong cache on browser side.
I want hard cache on cloudflare side.

even if the server is down cloudflare should response from cache. it does not need to re-validate the content

cloudflare only re-validate when i purge the cache from cloudflare.

I found a term Cache-Control: Immutable
I am not sure but I believe this will cache on browser and I can’t purge them on need.

any help?

As far as I can see, you already have browser cache TTL 30 min and Edge cache TTL 1 month. This means that Cloudflare CDN will cache requests for up to a month, but browser will only cache request for 30 minutes. You can of course set browser cache TTL to 0, which means you will get Cloudflare cache, but not browser cache.

even if the server is down cloudflare should response from cache. it does not need to re-validate the content

Cloudflare has 100+ POP servers, and there is no guarantee that your requests are stored on their CDN for 1 month, at least not from all POP locations. If your server goes down and the request does not exist in Cloudflare CDN, you will get a general error page.

cloudflare only re-validate when i purge the cache from cloudflare.

It already works like this in your settings. When a request is made, Cloudflare will check if the requested file exists on CDN, and serve this file (without contacting your server). If the file does not exist, then it will load the file from your origin.

I found a term Cache-Control: Immutable

Cache-Control: Immutable does not have any additional effect on Cloudflare. It just tells browsers that “this file will NEVER change”. Yes, you should avoid “Immutable” if you don’t want the browser to cache the request.


It’s important to note that Cloudflare caching is based on request volume, and just because you set EDGE cache TTL to 1 month, does not mean Cloudflare will cache the request for one month. It will do so only if there is sufficient amount of requests for the request on that specific Cloudflare POP server.

Also, there are other ways to achieve what you are trying to do. You can use s-maxage in cache-control headers to specifically tell Cloudflare (or other CDN servers) to cache a request, without having the browser cache the request. For example:

cache-control: public, max-age=0, s-maxage=315360000

In the above, the request will be cached on Cloudflare CDN, while browsers will not cache the request. This means you can flush cache manually from dashboard or API. Keep in mind, if you are adding the above header to non-static assets like html, it will need to be combined with a “cache everything” Cloudflare rule.

2 Likes

Thank you for the info :slight_smile:
Let me clear one thing. even if i use s-maxage cloudflare will use it’s default rule right? if request volume is less or 0 then the cache will auto purge before the s-maxage right?

The “default rule” of Cloudflare is to cache all static assets (jpg, css, js etc), but not cache dynamic assets (like html). Also by default, static assets will be instructed to cache on Cloudflare as specified by the cache-control max-age header, unless the header is missing, in which case the request will cache for 4 hours (or as specified in your dashboard > cache settings). The only way to get dynamic assets like html to cache, would be to add a cloudflare rule “cache everything”, combined with either a cache-control header, or with a “Edge cache TTL” rule.

So, normally the standard max-age cache-control header will instruct Cloudflare how long requests should be cached on Cloudflare (and in browser). The only reason you would use s-maxage, is if you want cloudflare edge cache to be different from browser cache. In this case s-maxage is specifically instructing the cloudflare edge cache, without affecting browser cache.

Pretty much yes. And it doesn’t matter if you have instructed Cloudflare edge cache from s-maxage, maxage or a Cloudflare “Edge cache TTL” rule … Cloudflare will still only keep the request in cache for a specific edge server based on some internal CF algorithm. For example, a request might get purged within a day, unless the same resource is requested many times from the same pop server within a day. I’m sure Cloudflare use many factors to decide how long to cache requests, and this is not public. Essentially, you can set cache headers to cache a resource for 10 years, but that’s just the maximum.

1 Like

Thank you very much :slight_smile:

This topic was automatically closed after 30 days. New replies are no longer allowed.