Skip to content
Back to all posts
Technology

Understanding HTTP Caching Headers: Cache-Control, ETag and Vary

Learn how HTTP Cache-Control, ETag, Last-Modified, Vary, and 304 responses work, with secure rules for public and private data.

3 min read

TechSilo

Technology writer & developer

HTTP caching reduces repeated downloads and can make websites faster, but an incorrect cache policy can serve outdated or private information. The most useful starting point is to distinguish browser caches from shared caches such as CDNs.

Cache-Control essentials

http
Cache-Control: public, max-age=3600

This permits caching by shared and private caches for up to one hour (subject to other constraints). Use it for suitable public resources.

http
Cache-Control: private, max-age=60

This allows browser-private caching but prevents shared caches from storing the response. For user-specific pages, it is still important to assess whether even a browser cache is appropriate.

http
Cache-Control: no-store

This tells caches not to store the response. Use it for sensitive data when storage in caches would be unsafe.

no-cache does not mean "do not cache." It allows a stored response but requires successful validation before reuse.

Cache immutable assets for longer

Versioned filenames can be cached aggressively:

http
Cache-Control: public, max-age=31536000, immutable

This is appropriate when the URL changes whenever the file changes, such as a content-hashed JavaScript bundle. Do not use a long immutable TTL for a mutable /latest.js filename.

ETag and conditional requests

An origin can return an entity tag:

http
ETag: "build-123"

On a subsequent request, the browser may send:

http
If-None-Match: "build-123"

If the resource has not changed, the server can return 304 Not Modified, often without sending the full response body. A 304 is a valid successful revalidation result, not an error. ETags should be designed to reflect the representation accurately.

Last-Modified and If-Modified-Since support related conditional behavior based on modification timestamps. A server may support both mechanisms.

Why Vary matters

http
Vary: Accept-Encoding

Vary tells caches which request headers affect the response representation. A response that varies by Origin for CORS may need Vary: Origin; otherwise a shared cache can serve a response generated for a different request origin.

Avoid Vary: * casually, as it prevents many normal cache reuse patterns.

CDN behavior

CDNs may also use s-maxage, which sets freshness for shared caches. Hosting services sometimes add their own cache rules or route-specific overrides.

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

This is an illustrative policy: the browser must revalidate, while a compliant shared cache may treat the response as fresh for five minutes. Check whether your actual hosting/CDN honors these directives.

Verify with real headers

bash
curl -I https://example.com/
curl -I https://example.com/static/app.js

Inspect Cache-Control, ETag, Vary, Age, and any CDN-specific headers. Remember that headers from a HEAD request can differ from GET on some servers.

Security and SEO implications

A stale cached HTML page may continue showing an old canonical URL or outdated article description even after deployment. Purge caches when changing critical metadata, and avoid caching private API responses in shared layers.

A good caching strategy is explicit, route-specific, and measurable—not simply the longest possible TTL.

Want to put this into practice?

Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.

Related blog posts