HTTP Caching: Freshness, Validation, and Safe Directives
Learn HTTP Cache-Control directives, ETag validation, conditional requests, 304 responses, and safe caching policies for public and private content.
HTTP caching can reduce repeat downloads while keeping changing content fresh. Learn how Cache-Control, ETag, conditional requests, and 304 responses work together, and choose directives based on whether a response is public, private, or sensitive.
A browser that caches a response can often avoid downloading the same bytes again. A shared cache or CDN may serve a reusable response to multiple users. These benefits depend on clear HTTP rules: how long a response is fresh, whether it may be stored, and how a stale copy can be checked against the origin.
Freshness and validation are different
Freshness says whether a stored response can be reused without contacting the origin. Validation checks whether a stale response is still current. Cache-Control: max-age=... primarily sets a freshness lifetime. An ETag or Last-Modified value can help validate a response once it becomes stale.
For example, a versioned static asset can be cached for a long time when its URL changes whenever its contents change:
Cache-Control: public, max-age=31536000, immutable
Use this only when the URL is genuinely versioned or fingerprinted. If the file can change while its URL stays the same, a long freshness lifetime may keep users on old content.
Choose directives for the kind of response
publicallows shared caches to store a response where other rules permit it. Use it only when the response is safe to reuse across users.privaterestricts storage to a private cache, such as a user’s browser, and prevents shared caches from reusing that response.no-cachedoes not mean “do not store.” It means a stored response must be validated before reuse.no-storetells caches not to store the response. Use for data that should not be retained by caches; it does not replace access control or erase copies already made.max-agesets the freshness lifetime in seconds for a response.
For a frequently changing page that is safe to retain but must be checked before reuse, a policy might include Cache-Control: no-cache. For sensitive account data, consider private, no-store according to the application’s threat model. Choose the policy at the server based on the response content, not just the URL path.
Use validators for efficient revalidation
An entity tag identifies a representation version. A client with a stale response can send its saved tag in If-None-Match. If the representation is unchanged, the server can return 304 Not Modified with no response body, and the cache can reuse its stored copy. If it changed, the server sends a new 200 response and a new validator.
Response:
ETag: "catalog-v17"
Cache-Control: public, max-age=60
Later request:
If-None-Match: "catalog-v17"
Unchanged response:
304 Not Modified
ETags may be strong or weak. A strong tag represents byte-for-byte equivalence; a weak tag, prefixed by W/, indicates semantic equivalence where byte identity is not required. Servers should generate validators consistently for the representation they send. Content negotiation and compression can affect which representation is being validated.
What Last-Modified adds
Last-Modified records a modification time. A client can later send If-Modified-Since; an unchanged resource can receive a 304. Timestamp precision and clocks make this validator less exact than an ETag in some cases. Servers can provide both validators; clients and intermediaries use conditional request rules to select the appropriate check.
Account for shared caches and variation
A shared cache must not accidentally serve one user’s personalized response to another. Mark user-specific responses private or no-store as appropriate, and avoid mixing authorization, cookies, and public cache rules carelessly. The Vary header tells caches which request headers distinguish representations, such as language or accepted encoding. Excessive variation can reduce cache reuse, so include only dimensions that actually change the response.
Do not assume a response is uncacheable just because the server omitted Cache-Control. HTTP caches can apply heuristic freshness in some circumstances. Set an explicit policy for important resources and inspect the actual response headers in developer tools or with an HTTP client.
Debug caching step by step
- Inspect the response’s
Cache-Control,ETag,Last-Modified, andVaryheaders. - Check whether the response is fresh or whether the browser sends a conditional request.
- Look for
If-None-MatchorIf-Modified-Sinceon the request and a matching304response. - Check the URL and any version fingerprint before concluding that a long-lived asset is stale.
- Test personalized and anonymous requests separately, including language or encoding variants.
Good caching is a contract between the server and every cache on the path. Set freshness deliberately, use validators for revalidation, and treat user-specific data as a separate policy decision. A 304 saves a body transfer, but it still involves a request to validate; it is not the same as a fresh cache hit.
Want a structured Java learning path?
Explore Jaipur Engineers course options to connect Java fundamentals, debugging, backend development and project practice in a guided learning plan.