Mirage CDN
HTTP caching / field guide 04

HTTP caching headers explained

Understand Cache-Control, Age, ETag and Vary, then choose a caching policy that fits public static files without exposing private responses.

By Mirage CDN · Updated

HTTP caching headers answer different questions: may a response be stored, when may it be reused, and how can a stored copy be checked? Treating every header as a duration setting creates confusing configurations. Start with the content and the people allowed to receive it, then choose freshness and validation rules.

The examples below describe HTTP behavior. They are not evidence of a particular CDN implementation. Use them to prepare an origin policy and a test plan before connecting a shared cache.

Decide whether storage is appropriate

A browser cache usually serves one user. A shared cache can reuse responses for several users. RFC 9111 defines the storage and reuse rules for both. An unqualified private response directive excludes shared caches; no-store instructs caches not to store the response; no-cache permits storage but requires successful validation before reuse. These instructions are not interchangeable. RFC 9111, sections 3 and 5.2

Consider three hypothetical resources. A public stylesheet is a candidate for shared storage. A personalized account page needs a policy that keeps it out of shared caches. A sensitive one-time response may need no-store in addition to the application’s authentication and data-handling controls. Do not choose policies by file extension alone: a URL ending in .json might contain either public reference data or a personal record.

public explicitly permits shared caching in cases where it would otherwise be restricted. That is a consequential instruction. Adding it to an authenticated response simply to improve a dashboard number is an unsafe shortcut. Review the actual response body and authorization requirements first. Cache-Control reference

Choose a freshness lifetime

The response directive max-age expresses a freshness lifetime in seconds. s-maxage provides a lifetime specifically for shared caches and takes precedence there. This illustrative public-file policy gives browsers a shorter lifetime than shared caches:

Cache-Control: public, max-age=300, s-maxage=1800

Those values are example policy choices, not recommended defaults. For a public diagram updated weekly, five minutes might be unnecessarily short. For a document that must reflect a correction promptly, thirty minutes could be too long. Write down the acceptable update delay before picking the number. must-revalidate requires successful validation before a stale response can be reused; it does not require a network check while the response remains fresh. Cache-Control directives

Freshness is evaluated using the response’s age, including time already spent in caches. A cache does not receive a new full lifetime merely by forwarding an old response to another cache. RFC 9111, freshness calculations

Read Age with the other headers

Age is expressed in seconds and helps explain the history of a response served through a cache. It is not a remaining-lifetime counter. Read it alongside Date, the caching policy and the request you actually sent. Age reference

For a simplified exercise, suppose a response has a freshness lifetime of 300 seconds and a calculated current age of 120 seconds. The remaining fresh interval is 180 seconds. That arithmetic does not establish whether the response is appropriate for this user, whether a request directive requires validation, or whether the cache key matches. Those are separate checks.

Expires expresses an expiration as an HTTP date. A relevant max-age or shared-cache s-maxage overrides it. If both are emitted, keep them consistent so a person inspecting the response can understand the intended policy without resolving contradictory clues. Expires reference

Give caches a validator

An ETag identifies a representation version. A conditional request can send that identifier in If-None-Match. When the representation is unchanged, a suitable 304 Not Modified response allows reuse of the stored body. The server chooses the tag; the client should treat it as opaque. A weak tag, marked with W/, does not assert byte-for-byte identity. ETag reference

Last-Modified provides a timestamp validator, commonly used with If-Modified-Since. It can serve as a fallback when an entity tag is unavailable, but timestamps are less precise for distinguishing contents. Neither validator explicitly sets a freshness lifetime. Last-Modified reference

Without an explicit lifetime, a cache may calculate permitted heuristic freshness using metadata such as Last-Modified. Supply an explicit policy when the application’s update requirements matter. RFC 9111, heuristic freshness

For a test, publish a small fixture containing a visible revision marker. Fetch it, retain its validator, then issue a conditional request. Change the fixture and repeat. Record the response status, validator and body separately. A validator that stays unchanged after a meaningful content edit deserves investigation.

Keep negotiated representations separate

Vary identifies request headers that affected the selected representation. For example, Vary: Accept-Encoding tells caches to account for content-coding negotiation when matching a stored response. Use the same relevant variation information consistently, including on validation responses. Vary reference

Test each intended variation rather than deleting Vary to improve reuse. Request the fixture with different supported encodings, inspect the returned metadata, and confirm that each body can be decoded correctly. Keep authentication tests separate from encoding tests so a successful compression check cannot conceal an access-control problem.

Review the whole policy

Before release, maintain a short resource inventory: public assets, mutable public documents, personalized pages and sensitive responses. Give each group an explicit owner and expected behavior. Test an initial request, a repeat request, a conditional request and a changed version. Include error responses in the review. Save the headers and fixture revision with the results so the next developer can reproduce the decision.

Sources

This guide explains technical concepts. For your delivery setup, review the documentation and request an account from our team.

Read the integration docs · Explore more CDN guides