HTTP Cache-Control
HTTP rules for cache storage, freshness and validation
What it is
HTTP Cache-Control is a header that carries rules for storing and reusing HTTP responses. A server can give a browser a freshness lifetime for a response. It can allow a shared cache to use a response or tell caches to check with the origin before reuse. A client can also put directives on a request to communicate particular requirements to caches along the path.
Freshness tells a cache how long it can use a stored response without checking
again. A max-age value gives the freshness lifetime in seconds. A cache can
store a response with an unqualified no-cache directive and needs successful
validation before reusing it. A response with no-store instructs caches to
avoid storing the request or the response. An unqualified private directive
prevents shared caches from storing the response. A private cache such as the
browser’s can store it when the other applicable rules allow it. These
distinctions matter when a service has public assets alongside account-specific
pages.
A validation step can use a validator such as an ETag. If the stored representation is still current, a server can reply with status 304 and let a recipient reuse its stored body. The cache’s rules also depend on its stored response and the request it received for that copy. Vary can tell intermediaries which request headers affect the selection of a representation.
Why it matters
Static files can be delivered quickly when readers use a recent local copy. A stylesheet whose path includes a strong content fingerprint can use a long freshness period, because different bytes should receive a different address. The HTML that names it can follow a shorter policy or require validation so that a reader learns about a new asset after a release. The two responses support different responsibilities in the same page load.
A public document and an account-specific page also carry different consequences. A shared cache can deliver one public representation to many users. A private response may contain data intended for one reader. Choose the storage rule before deploying, and verify the response headers at the public boundary where browser and proxy behaviour meets. Include the actual requests and authentication state the service supports.
A cache can also prolong an operational failure if an error message is stored as though it is useful content. Decide which responses can be cached and for how long. An origin can recover while a client still sees an earlier response. Make it possible to identify the served age, the cache involved and the headers that explain what happened.
What it looks like in practice
Consider a company website that links an HTML article to a generated PDF and a versioned stylesheet. A release changes the stylesheet’s bytes and publishes a new address for them. The page’s cache policy gives returning browsers a reason to retrieve or validate the new HTML promptly. The old stylesheet still exists at its old address for readers holding the earlier document. A test can load both versions and check the public response for each file.
Exercise more than one client state. A fresh client should receive the current asset references. A returning client with the earlier article should learn when that representation changed. A request for an account page, if present, should receive the policy designed for its private information. Inspect the headers after any CDN or reverse proxy, then confirm what each cache actually reuses.
Engineer’s small-server case study describes operating public services within a constrained host. It provides a practical reason to consider which responses can be reused safely across repeated visits. The illustrated cache policy explicitly protects the connection between a current document and the assets it names. Measure the resulting delivery experience as well as the reduction in requests to the origin.
Retain a command or browser replay for a release. Record the response status, policy and current revision on the representative routes. If a cache has a stored copy, show why it may reuse it and when it must check again. A direct response inspection can distinguish a policy error from an outdated resource address.
Where it goes wrong
A common error is giving the main document and fingerprinted assets an identical long freshness period. A browser can keep the older document and its valid older stylesheet long after the application has published a replacement. Put the appropriate policy on each response type and verify the production headers. Test the returning-reader path because a clean browser cache cannot show what that reader sees.
A response can also vary with a request header while its cache key treats the variants as interchangeable. If a route serves different representations by media type or content encoding, use the relevant cache selection rule. Test the same URL with two supported requests in an order that exposes a wrong reuse. Repeat the check at any shared layer that can store the response.
Plan how a changed policy reaches clients that already hold a stored response. A new header affects readers when they receive it. A still-fresh copy can remain eligible under the policy it originally received. If a release relies on a new representation becoming visible promptly, test the old policy state in the browser and the relevant shared cache. Keep the active document’s identity observable. A versioned resource address offers another way for a new document to request the bytes it needs.
Finally, exercise conditional requests and failures during the same rehearsal. A validation response should carry the information needed to continue using the stored representation. An error should have the intentionally selected storage behaviour. The correct policy is the one operators can explain for the actual public route and verify after an ordinary change or rollback.
Further reading
- RFC 9111 Cache-Control directives. Defines the HTTP request and response directives.
- Mozilla’s Cache-Control documentation. Provides examples of freshness, storage and validation rules.
- Caddy’s header directive. Explains how to configure response headers on a web server.