how cloudflare determines what to cache and what not to cache
Cloudflare uses a variety of mechanisms to determine what content to cache and what not to cache, aiming to optimize performance while ensuring that dynamic or sensitive content is handled appropriately. Here’s how Cloudflare determines caching behavior:
1. Cache-Control Headers
Cloudflare respects standard HTTP headers, particularly the Cache-Control
header, which is set by your origin server to control caching behavior. Common directives include:
- max-age: Specifies how long (in seconds) the content can be cached before it's considered stale.
- s-maxage: Similar to
max-age
, but specifically for shared caches (like CDNs). - no-cache: Instructs Cloudflare to revalidate the content with the origin server before serving it to clients.
- no-store: Instructs Cloudflare not to cache the content at all.
By default, Cloudflare follows these directives to determine how long content should be cached before revalidation with the origin server.
2. Edge Cache TTL
Cloudflare allows you to set a Time To Live (TTL) value for cached content directly within the Cloudflare dashboard. This value overrides any Cache-Control
headers received from the origin server, enabling finer control over cache duration at the edge.
3. Cache Key and Cache Keys Bypass
Cloudflare uses a cache key to uniquely identify cached objects. By default, the cache key includes the request's URI and host name. However, you can customize the cache key using Cache Keys Bypass to exclude specific parameters or headers from the cache key calculation.
4. Page Rules
Cloudflare offers Page Rules, which allow you to define specific caching behavior based on URL patterns and conditions. This includes:
- Bypass Cache: Directs Cloudflare to bypass caching for specific URLs or URL patterns, ensuring that certain content is always fetched from the origin server.
- Cache Everything: Forces Cloudflare to cache every request matching the URL pattern, overriding default caching behaviors.
5. Origin Cache-Control Directives
Cloudflare respects Cache-Control
directives set by your origin server in responses. If your origin server sends specific directives like private
, no-store
, or no-cache
, Cloudflare will follow these instructions when caching content.
6. Query String Handling
By default, Cloudflare caches static content regardless of query strings. However, you can configure caching behavior for specific query strings using Page Rules. This allows you to cache different versions of content based on query string parameters.
7. Dynamic Content Caching
Cloudflare distinguishes between static and dynamic content to optimize caching. Static assets like images, CSS, and JavaScript files are typically cached for longer periods, while dynamic content that changes frequently or personalized content may have shorter cache durations or no caching at all.
8. Edge Side Includes (ESI)
Cloudflare supports Edge Side Includes (ESI), which allows dynamic content assembly at the edge. ESI lets you cache parts of a page separately, serving cached components while fetching dynamic content from the origin server as needed.