Menu
Cloudflare Blog·July 23, 2026

Optimizing CDN Caching with Response Rules

Cloudflare's new Cache Response Rules provide a crucial mechanism for enhancing CDN cache hit ratios by allowing modifications to origin server responses *before* content is cached. This enables engineers to correct misconfigured or problematic response headers like `Set-Cookie` or `Cache-Control` without requiring changes to the origin server, directly impacting performance and reducing infrastructure costs.

Read original on Cloudflare Blog

The Challenge of Efficient CDN Caching

CDNs aim to serve content from the edge cache as much as possible, offloading traffic from origin servers and improving user experience. However, an origin server's response headers often dictate caching behavior, and misconfigurations can severely limit cache efficiency. Common issues include `Set-Cookie` headers on static assets, incorrect `Cache-Control` directives (e.g., `no-cache` where it's not needed), or overly aggressive `ETag` headers leading to revalidation thrash. These problems manifest *after* the origin replies, making request-time rules insufficient.

Introducing Cache Response Rules

Cloudflare's Cache Response Rules operate in the "response phase" – after the origin server sends its reply but *before* Cloudflare's cache stores the content. This allows for dynamic adjustments to the response headers, effectively overriding origin misconfigurations without requiring changes to the origin application code itself. This is particularly valuable in large organizations where coordinating origin changes can be slow and complex.

ℹ️

Request vs. Response Phase Caching

Cache Rules (request phase) decide *whether*, *what*, and *how* to cache based on the incoming request. Cache Response Rules (response phase) get the final say on *whether* and *how* a response is cached, adjusting directives after seeing the origin's actual response. They cannot change the cache key (the "what") once fixed by request-time rules.

Key Capabilities and Use Cases

  • Strip Headers: Remove problematic headers like `Set-Cookie`, `ETag`, or `Last-Modified` from origin responses to make otherwise uncacheable assets cacheable. This is crucial for static content served by applications that indiscriminately attach session cookies.
  • Manage Cache Tags: Add, remove, or set cache tags on responses. This is vital for implementing granular cache invalidation strategies, especially during CDN migrations where existing `Surrogate-Keys` or similar headers need to be translated.
  • Modify Cache-Control Directives: Adjust `Cache-Control` directives such as `max-age`, `s-maxage`, `no-store`, or `must-revalidate`. The `cloudflare_only: true` option is particularly powerful, allowing Cloudflare to cache content for a specific duration while sending different (e.g., shorter) `Cache-Control` values to browsers, optimizing both CDN and browser caching.
json
{
  "expression": "(http.request.uri.path matches \"^/static/.*\\.(css|js|jpg|jpeg|png|gif|svg|webp|ico|pdf|woff|woff2|ttf|eot)$\")",
  "action": {
    "name": "set_cache_settings",
    "value": {
      "strip_set_cookie": true
    }
  }
}

This functionality provides system architects and DevOps engineers with more precise control over CDN behavior, directly impacting web performance, origin server load, and overall infrastructure cost efficiency by maximizing cache hit ratios.

CDNCachingCloudflareHTTP HeadersPerformance OptimizationEdge ComputingInfrastructure

Comments

Loading comments...