Skip to main content

Cloudflare Now Lets You Cache Based on the Response

· Do Minh Tuan · Future Pulse 🔮

Cloudflare Now Lets You Cache Based on the Response

Cloudflare has shipped Cache Response Rules, a new phase in how its edge decides what to keep. Until now, Cache Rules looked at the incoming request: the path, the query string, the request headers. The edge had to guess whether something was cacheable before your server had produced a single byte.

The new rules wait for the origin to answer. Then they look at the status code, the origin headers and the content type, and decide what to store based on that.

The announcement is a feature release from Cloudflare, and it is a smaller story than the headlines around most infrastructure launches. It also fixes a failure that is easy to hit once your origin has a bad day.

The problem in plain terms

A CDN is a set of servers around the world that keep copies of your pages close to the people reading them. Instead of every visitor travelling to your origin server, they get a copy from a nearby edge location.

The old model works well for a static page. It works badly when the origin is having a bad day. A request comes in for a product page, the database times out, the server returns a 500 with an error page, and the edge caches it because the rule matched the URL. Now every visitor in that region gets the error page, from cache, for the length of the TTL, even after the database has recovered.

If you have never had this happen, you have either been lucky or you have not looked at your cache hit ratio during an incident. The first time it happens, it looks like a mysterious regional outage, and you spend an hour debugging a server that is actually fine.

What the new phase buys you

Three things, and they are all about saying no.

You can refuse to cache error responses. A 500 or a 503 passes through and is never stored, so the next visitor gets a fresh attempt.

You can cache based on a header the origin sets. If your backend knows a response is public and safe to share, it can say so in a response header, and the edge can honour that. If the backend knows the page is personalised, it can say that too, and the edge keeps it out of shared storage.

You can override the TTL based on what the response actually contains, rather than on a rule written months ago for a URL pattern that has since changed meaning.

Cloudflare groups these as selective error handling, dynamic content classification and granular TTL overrides. All three come from the same idea: let the thing that knows the answer make the decision.

The question I would ask first

Why not set proper Cache-Control headers at the origin?

That is the honest question, because if your backend can set Cache-Control: private, max-age=0 or s-maxage=300 correctly, the edge does not need a second policy language. HTTP already has a way to describe this, and it has had one for a long time.

The answer, as I read the feature, is that plenty of origins cannot do that. Legacy applications, third-party platforms, a marketing site behind a CMS nobody wants to touch, an API that sets no cache headers at all. Cache Response Rules let you apply a policy at the edge for systems you cannot change, or cannot change this quarter. That is a legitimate need. It is also a way to accumulate a second, invisible caching configuration that nobody remembers in a year.

So my advice is boring: if you control the origin, fix the headers there. Use the response rules for the systems you do not control. Mixing both without a convention is how you get a page that is cached for a day because an old rule matched a path before the new header was added.

Every caching feature increases the number of places where a wrong decision can hide. That is not an argument against the feature. It is an argument for treating cache configuration as code.

I would want the rules in version control, reviewed like any other deployment, with a comment explaining what each one is for. I would want a test that fetches a known error URL through the edge and asserts it is not cached. And I would want someone to own the question of what happens when the origin starts returning a new status code that no rule anticipated.

The TTL override is the part I would watch most closely. A rule that extends TTL based on origin conditions can be great for a product catalogue and terrible for a price or an inventory count. The failure is quiet: the page looks fine, it is just wrong, and it stays wrong until the TTL runs out. That is harder to notice than a 500, and it can be more expensive.

If you run a handful of sites for clients, this is genuinely useful. The pattern I would set up is simple. The backend sets one response header describing the caching intent for that response, for example a value like long-term for assets and dynamic-short for a feed, and a single edge rule reads it. One header, one rule, no path-based guessing.

That keeps the policy next to the code that knows the truth, which is the only place it stays accurate. It also means a new page gets the right behaviour without anyone editing a dashboard at 11pm.

For agencies in Vietnam managing sites for clients abroad, there is an extra benefit: the client's origin can stay as it is, and the caching behaviour can be corrected at the edge without a server migration. That is a much easier conversation than asking for a deploy window.

Three things I would still want to confirm before trusting it with production traffic.

How the new phase interacts with the existing Cache Rules, and in what order they run when both match. The docs will say, and I would read them twice.

Whether the rule language can see the response body, or only headers and status. Headers are enough for most cases, but the interesting cases are often the ones where the origin sets no header and the body tells you everything.

And what the debugging experience looks like when a response is not cached and you cannot tell which rule decided. Cache debugging is already unpleasant. A new phase gives you one more layer to check, and the tooling around it matters as much as the feature.

My read is that this is a good, unglamorous addition for teams that have been bitten by caching an error page. If you set your headers properly at the origin already, you can skip it. If you have ever had to purge a whole zone during an incident, you will know exactly why it exists.

Written with AI assistance, reviewed and edited by Do Minh Tuan.