Conrad caching

HTTP caching

Cache-Control directives, freshness and validation with ETag and Last-Modified, 304 responses, private versus shared caches, and what a reverse proxy is — the HTTP caching part of your Conrad Caching note.

From the HTTP caching, validation and cache-types parts of your Conrad Caching note.

Know

  • Who may store it: public (any cache), private (the browser only), no-store (nobody).
  • How long it is fresh: max-age in seconds; s-maxage the same but only for shared caches, so the CDN can keep something longer than the browser.
  • When it is stale: the copy is not thrown away, it is validated. no-cache means validate before every use. must-revalidate forbids serving stale when the origin cannot be reached (proxy-revalidate for shared caches). immutable means do not even revalidate while fresh. stale-while-revalidate serves stale and refreshes in the background; stale-if-error serves stale when the origin fails.
  • Validators: ETag (a token for the content, sent back as If-None-Match) and Last-Modified (sent back as If-Modified-Since). A 304 Not Modified has no body and makes the copy fresh again. ETag is the stronger of the two.
  • Weak ETags: W/"…" promises equivalence, not identical bytes; rendered HTML gets weak tags because compression changes the bytes, and a 304 is still right. Static files get strong tags, which byte ranges need.
  • Caches: private (the browser), shared proxy caches you do not control (rare now, HTTPS hides the content), managed caches you do control (a CDN, Varnish, nginx), with their own headers such as CDN-Cache-Control.
  • Proxies: a reverse proxy stands in front of servers and takes requests for them; a CDN is a reverse proxy spread around the world. A forward proxy stands in front of users.

Fact check of your notes

Gotcha· must-revalidate is about stale copies, not a ban on caching

"After the max-age expires, can't serve the cached page" is close: once stale, the cache must check with the origin before using the copy, and if the origin is unreachable it must return an error rather than the stale copy. While fresh, the copy is served normally.

Gotcha· Cloudflare's extra headers control lifetimes, not cookies

The managed-cache line says Cloudflare "provides own headers for cookie control". The headers are CDN-Cache-Control and Cloudflare-CDN-Cache-Control, and they give the CDN its own lifetime, separate from the browser's. Cookies affect caching differently: a response with Set-Cookie is bypassed by default.

Gotcha· Spelling to get right in the room

stale-while-revalidate, s-maxage, If-None-Match. Last-Modified has one-second resolution, which is one reason ETag is preferred.

Exercise

Exercise
  • Open a product page on a large shop with the network panel and classify every response by its Cache-Control: who may cache it, for how long, and what happens when it goes stale.
  • Reload the page and find the 304 responses; find which validator was used.
  • Write the headers you would send for: a hashed script, a product page, an account page, a public price list API.

Be able to say

Be able to say· What is the difference between no-cache and no-store?

"no-cache is badly named. It allows the cache to keep a copy but requires a check with the origin before every use, usually a conditional request with the ETag, which gets a 304 when nothing changed. That is the normal setting for HTML. no-store means no copy anywhere, for sensitive responses like account data. It also switches off the back-forward cache, so I would not put it on ordinary pages."

Be able to say· How does a browser know whether its cached copy of a page is still good?

"While the copy is within max-age it just uses it. When it is stale, or always with no-cache, it asks the origin: it sends the ETag back as If-None-Match, or the Last-Modified date as If-Modified-Since. If nothing changed the origin answers 304 with no body and the copy is fresh again; otherwise it gets a 200 with the new content. That round trip is cheap, which is why HTML can be revalidated on every visit without hurting much."

Your notes

Open in Notes

Nothing yet. Notes filed here show up on this page and the section overview.