| |

Internet Frontend 07 ๐ŸŒ HTTP Caching and Validation Headers

โฑ๏ธ Estimated read: 10 min | |

HTTP caching is the mechanism that lets a browser reuse a previously downloaded response instead of requesting it again. It is controlled by two kinds of headers: the freshness headers, which tell the browser how long a response is valid without checking back with the server, and the validation headers, which let the browser ask the server whether a stale response is still usable. The two work together. The freshness headers are the fast path; the validation headers are the fallback when the freshness expires. The distinction between them is the difference between a request that never leaves the browser and a request that makes a round trip but does not transfer the body.

The freshness mechanism is Cache-Control with max-age. A response with Cache-Control: max-age=3600 is considered fresh for one hour, and the browser serves it from the cache without any network request during that window. The validation mechanism is ETag and Last-Modified, paired with the conditional request headers If-None-Match and If-Modified-Since. When the response is stale, the browser sends the validator back to the server, and the server responds with either 304 Not Modified (reuse the cached body) or 200 OK with the new body .

Key point: Cache-Control: max-age controls freshness and is the fast path. ETag and Last-Modified control validation and are the fallback when the freshness expires. A 304 Not Modified response has no body and tells the browser to reuse the cached body. The no-cache directive does not prevent storage; it forces validation before reuse. The no-store directive prevents storage entirely.


Why the two header families exist

The freshness problem. A response that does not change for a known period can be served from the cache without any network request. The max-age directive is the declaration of that period. The browser calculates the age of the cached response from the Date header or the time of the request, and if the age is less than the max-age, the response is fresh and is served directly . The freshness is the zero-request path, and it is the fastest path.

The validation problem. A response that changes unpredictably cannot have a fixed max-age. The ETag is the opaque validator that the server generates for a specific representation, and the Last-Modified is the timestamp of the last change. When the response is stale, the browser sends the validator in a conditional request. The server compares the validator with the current state, and if the state has not changed, it responds with 304 Not Modified, which tells the browser to reuse the cached body . The validation is the round-trip path, and it saves the body transfer even when it does not save the request.

The no-cache and no-store distinction. The two directives are often confused. The no-cache directive means the response can be stored, but it must be validated with the server before every reuse. The no-store directive means the response must not be stored at all . A response with no-cache still benefits from the 304 response, because the body is not transferred. A response with no-store does not benefit from any cache, because nothing is stored.

The trade-off. The max-age is a guess about the future. If the guess is wrong and the response changes before the max-age expires, the browser serves a stale response. The stale-while-revalidate directive is the mechanism that softens the guess: the browser serves the stale response immediately and revalidates it in the background, so the next request gets the fresh version . The trade-off is the freshness versus the latency, and the directive is the middle ground.


a. The freshness headers and the Cache-Control directives

The Cache-Control header is the primary freshness mechanism. The max-age directive is the duration in seconds, and the s-maxage is the equivalent for the shared caches (CDNs, proxies). The private directive restricts the response to the browser cache, and the public directive allows the shared caches to store it.

HTTP/1.1 200 OK
Cache-Control: public, max-age=3600, s-maxage=86400
ETag: "abc123"
Last-Modified: Tue, 22 Feb 2022 20:20:20 GMT

The max-age=3600 means the browser considers the response fresh for one hour. The s-maxage=86400 means the CDN considers it fresh for one day. The public allows the shared caches to store it, and the private would restrict it to the browser .

The no-cache directive is the validation-forcing mechanism. The response can be stored, but it must be validated before every reuse.

Cache-Control: no-cache

The no-store directive is the storage-prevention mechanism.

Cache-Control: no-store

The stale-while-revalidate directive is the background-revalidation mechanism. The browser serves the stale response for the specified window and revalidates it in the background .

Cache-Control: max-age=3600, stale-while-revalidate=59

The response is fresh for one hour. After that, it is stale but is served for another 59 seconds while the browser revalidates it in the background. After the 59 seconds, the browser waits for the revalidation before serving the response .


b. The validation headers and the conditional requests

The ETag is the opaque validator. It is a string that the server generates for a specific representation, and it changes when the representation changes. The Last-Modified is the timestamp of the last change, and it is the weaker validator because it has a one-second resolution .

HTTP/1.1 200 OK
ETag: "abc123"
Last-Modified: Tue, 22 Feb 2022 20:20:20 GMT

The browser stores the validators with the cached response. When the response is stale, the browser sends the validators in the conditional request headers.

GET /data HTTP/1.1
If-None-Match: "abc123"
If-Modified-Since: Tue, 22 Feb 2022 20:20:20 GMT

The If-None-Match is the ETag-based condition. The server compares the value with the current ETag, and if they match, it responds with 304 Not Modified. The If-Modified-Since is the date-based condition. The server compares the value with the Last-Modified, and if the resource has not been modified since that date, it responds with 304 Not Modified .

HTTP/1.1 304 Not Modified
ETag: "abc123"
Cache-Control: public, max-age=3600

The 304 response has no body. The browser updates the cached response’s headers with the new headers and resets the freshness timer. The cached body is reused .


Complete Example Session

// ============================================
// PART 1: THE INITIAL RESPONSE
// ============================================

HTTP/1.1 200 OK
Content-Type: application/javascript
Cache-Control: public, max-age=31536000, immutable
ETag: "hash-abc123"
Last-Modified: Tue, 22 Feb 2022 20:20:20 GMT

// The response is fresh for one year.
// The immutable directive tells the browser not to revalidate on reload.

// ============================================
// PART 2: THE FRESH HIT
// ============================================

// The browser serves the response from the cache.
// No network request is made.

// ============================================
// PART 3: THE STALE HIT
// ============================================

// After the max-age expires, the browser sends:
GET /app.js HTTP/1.1
If-None-Match: "hash-abc123"
If-Modified-Since: Tue, 22 Feb 2022 20:20:20 GMT

// ============================================
// PART 4: THE 304 RESPONSE
// ============================================

HTTP/1.1 304 Not Modified
ETag: "hash-abc123"
Cache-Control: public, max-age=31536000, immutable

// The browser reuses the cached body.
// The freshness timer is reset.

// ============================================
// PART 5: THE 200 RESPONSE
// ============================================

// If the resource changed, the server sends:
HTTP/1.1 200 OK
Content-Type: application/javascript
Cache-Control: public, max-age=31536000, immutable
ETag: "hash-def456"

// The browser replaces the cached response with the new body.

// ============================================
// PART 6: THE NO-CACHE DIRECTIVE
// ============================================

HTTP/1.1 200 OK
Cache-Control: no-cache
ETag: "abc123"

// The response is stored.
// Every reuse requires a validation request.
// The 304 response avoids the body transfer.

// ============================================
// PART 7: THE NO-STORE DIRECTIVE
// ============================================

HTTP/1.1 200 OK
Cache-Control: no-store

// The response is not stored.
// Every request goes to the network.

// ============================================
// PART 8: THE STALE-WHILE-REVALIDATE
// ============================================

HTTP/1.1 200 OK
Cache-Control: max-age=60, stale-while-revalidate=30

// 0-60s: fresh, served from cache.
// 60-90s: stale but served from cache.
// The browser revalidates in the background.
// After 90s: the browser waits for the revalidation.

// ============================================
// PART 9: THE VARY HEADER
// ============================================

HTTP/1.1 200 OK
Vary: Accept-Encoding, User-Agent

// The cache stores separate entries for each
// Accept-Encoding and User-Agent combination.

// ============================================
// PART 10: THE CHECKLIST
// ============================================

// [ ] The static assets have a long max-age and a hash in the filename.
// [ ] The HTML has a short max-age or no-cache.
// [ ] The ETag is set for the validated resources.
// [ ] The Last-Modified is set if the ETag is not feasible.
// [ ] The private directive is set for the personalized responses.
// [ ] The stale-while-revalidate is set for the frequently updated resources.
// [ ] The Vary header is set if the response depends on the request headers.

The ten parts covered the initial response, the fresh hit, the stale hit, the 304 response, the 200 response, the no-cache directive, the no-store directive, the stale-while-revalidate, the Vary header, and the checklist. The through-line is that the freshness headers are the fast path and the validation headers are the fallback.


Quick Reference

The Cache-Control directives

DirectivePurpose
max-age=<seconds>The freshness lifetime for the browser
s-maxage=<seconds>The freshness lifetime for the shared caches
no-cacheStore but validate before every reuse
no-storeDo not store
privateThe browser cache only
publicThe shared caches allowed
immutableDo not revalidate on reload
stale-while-revalidate=<seconds>Serve stale and revalidate in the background

The validation headers

HeaderDirectionPurpose
ETagResponseThe opaque validator
Last-ModifiedResponseThe timestamp validator
If-None-MatchRequestThe ETag condition
If-Modified-SinceRequestThe date condition
304 Not ModifiedResponseThe cached body is still valid

The cache states

StateConditionBehaviour
FreshAge < max-ageServed from cache, no request
StaleAge > max-ageConditional request, 304 or 200
Stale-while-revalidateAge within the windowServed stale, revalidated in background
No-storeno-store directiveNot stored, every request goes to network

Best Practices

โœ… Do This:

Cache-Control: public, max-age=31536000, immutable
ETag: "hash-abc123"
Cache-Control: no-cache
ETag: "abc123"
Cache-Control: max-age=60, stale-while-revalidate=30

โŒ Don’t Do This:

Cache-Control: no-store
// Used for a static asset that could be cached
Cache-Control: max-age=31536000
// Used for HTML that changes on every deploy

Common Pitfalls

PitfallWhy It HappensFix
The browser serves a stale responseThe max-age is too long for a changing resourceUse no-cache or a shorter max-age
The response is not cachedThe no-store directive is setUse no-cache instead
The personalized response is cached by a CDNThe private directive is missingAdd private
The 304 is not returnedThe ETag or Last-Modified is missingAdd the validator
The cache key ignores the encodingThe Vary header is missingAdd Vary: Accept-Encoding

Summary

ItemValue
FreshnessCache-Control: max-age
ValidationETag / Last-Modified with If-None-Match / If-Modified-Since
304The cached body is still valid
no-cacheStore but validate
no-storeDo not store
stale-while-revalidateServe stale and revalidate in the background
VaryThe cache key varies by the request headers

Key takeaways:

  • The freshness headers are the fast path, and the validation headers are the fallback. max-age lets the browser serve a response without any network request. When the freshness expires, ETag and Last-Modified let the browser validate the response and reuse the cached body if the resource has not changed .
  • A 304 Not Modified response has no body. The browser reuses the cached body and resets the freshness timer. The validation saves the body transfer even when it does not save the request.
  • no-cache and no-store are different. no-cache allows storage but forces validation before every reuse. no-store prevents storage entirely . A response with no-cache still benefits from the 304 response.
  • stale-while-revalidate is the freshness-latency trade-off. The browser serves the stale response immediately and revalidates it in the background, so the next request gets the fresh version. The window is the extra time beyond the max-age .
  • The Vary header is the cache-key mechanism. If the response depends on a request header (such as Accept-Encoding or User-Agent), the Vary header tells the cache to store separate entries for each value of that header .

Remember: HTTP caching is two mechanisms: the freshness headers tell the browser how long a response is valid, and the validation headers let the browser ask the server whether a stale response is still usable. The max-age is the fast path, the ETag and Last-Modified are the fallback, and the 304 Not Modified is the response that saves the body transfer. The no-cache directive forces validation, the no-store directive prevents storage, and the stale-while-revalidate directive softens the freshness guess. The headers are the contract, and the browser is the cache.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!