Sub topic 14
HTTP Caching and Validation
This chapter assumes you know what a cache hit and miss are, how TTL expiration works, and the basics of HTTP requests and response headers. HTTP caching applies those ideas to browsers, CDNs, and proxies. The server describes how long a response stays fresh, and caches along the path follow those rules.
Freshness with Cache-Control
A response is fresh while its age is below its allowed lifetime. During that time a cache answers without contacting the origin server. The Cache-Control header sets the lifetime and who may store the response.
| Directive | Meaning |
|---|---|
| `max-age=N` | Fresh for N seconds from when the response was generated. |
| `s-maxage=N` | Same as `max-age`, but only for shared caches such as CDNs. |
| `public` | Any cache may store it. |
| `private` | Only the user's own browser may store it. |
| `no-cache` | May be stored, but must be revalidated before every reuse. |
| `no-store` | Must not be stored at all. |
no-cache is often misread as "do not cache". The name is misleading. Use no-store for sensitive data that must never be written to a cache.
Validation with ETag
When a response goes stale, the cache does not have to discard it. It can ask the origin whether the stored copy is still current. This is called revalidation.
The server labels each version of a resource with an ETag, an opaque identifier such as a version number or content hash. The client sends that label back in an If-None-Match header. If the label still matches, the server replies 304 Not Modified with no body. The cache then reuses its stored copy and resets its freshness timer. If the resource changed, the server sends a normal 200 with the new body and a new ETag.
HTTP/1.1 200 OK
Cache-Control: public, max-age=3600
ETag: "v42"
--- later, after the entry is stale, the client revalidates ---
GET /report HTTP/1.1
If-None-Match: "v42"
HTTP/1.1 304 Not ModifiedA 304 still costs a round trip, but it saves transferring the body. For large resources that is a big saving.
placeholderThe request flow
Common errors
- Caching personalized responses as
public. A CDN may then serve one user's data to another. Useprivatefor per-user content. - Long
max-ageon content with a fixed URL. You cannot recall copies already in browsers. Put a content hash in asset filenames, such asapp.3f9c1.js, and use a longmax-ageon them. - Weak validators without a plan. An ETag that changes on every request makes every revalidation fail and wastes the round trip.
- Forgetting
Vary. If the response depends on a request header such asAccept-Encoding, includeVaryso caches key on it. This is cache key design applied to HTTP.
Exercise
A response has Cache-Control: max-age=60 and ETag: "a1". After 90 seconds a client asks again. What does the client send, and what does a 304 reply tell it?
Show answer
The copy is stale, so the client sends a conditional request with If-None-Match: "a1". A 304 reply means the resource is unchanged. The client reuses its stored body and treats it as fresh again, without downloading it a second time.