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.
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
Cache-Control: public, max-age=3600This permits caching by shared and private caches for up to one hour (subject to other constraints). Use it for suitable public resources.
Cache-Control: private, max-age=60This 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.
Cache-Control: no-storeThis 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:
Cache-Control: public, max-age=31536000, immutableThis 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:
ETag: "build-123"On a subsequent request, the browser may send:
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
Vary: Accept-EncodingVary 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.
Cache-Control: public, max-age=0, s-maxage=300This 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
curl -I https://example.com/
curl -I https://example.com/static/app.jsInspect 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
Understanding Virtual DOM Diffing in React
How React compares render output, uses element identity and keys, and decides whether to preserve or reset component state.
Read postUnderstanding WebAssembly: When and How to Use Wasm
Understand WebAssembly modules, JavaScript integration, browser sandboxing, and the workloads where Wasm helps performance.
Read postMemory Leaks in React: Causes, Cleanup, and Debugging
Find retained timers, listeners, subscriptions, and DOM references in React apps, then clean them up with effect lifecycles and browser memory tools.
Read postDiagnosing and Fixing Memory Leaks in Node.js
Track Node.js memory growth with process.memoryUsage, heap snapshots, retaining paths, and bounded caches instead of guessing from RSS alone.
Read postDiagnose Slow SQL Queries with EXPLAIN and Better Indexes
Use query plans, row estimates, buffer reads, and composite indexes to investigate slow SQL queries without adding indexes blindly.
Read post