Skip to content

HTTP Caching: The Performance Feature We Keep Reinventing

4 min read0 views[Backend]
Cover image for "HTTP Caching: The Performance Feature We Keep Reinventing"

Before adding a cache lookup to every handler, check what the HTTP response already tells browsers and intermediaries to do.

A public article might be downloaded thousands of times without changing. If every request reaches the application, queries the database, renders the page, and transfers identical bytes, there is repeated work at several layers. A Redis cache could remove some of it. HTTP caching can sometimes prevent the request from reaching that application in the first place.

The difficult part is defining when reuse is correct.

Start with the representation

A URL is not necessarily one fixed document. The response might vary with language, authentication, accepted encoding, or another request property.

Before choosing a lifetime, ask who is allowed to reuse which response. For a hypothetical public article, all anonymous readers of the English URL might receive the same representation. An account settings page is different: sharing its response across users would be a serious bug.

HTTP distinguishes private caches, such as a browser cache, from shared caches. Response directives describe storage and reuse rules. no-store prohibits storage by compliant caches; no-cache permits storage but requires validation before reuse. private prevents shared-cache storage rather than meaning “never store anywhere.” RFC 9111

The names are easy to misread. Treat them as protocol terms, not English hints.

Freshness avoids a round trip

For our public article, an illustrative response might include:

Cache-Control: public, max-age=60
ETag: "article-184-revision-7"

A fresh cached representation can be reused under the applicable cache rules without asking the origin whether it changed. The lifetime expresses a tolerance: this example permits some period of stale business content after an edit, even while the cached response is technically fresh.

If the product requires an edit to become visible immediately, that lifetime is not the whole solution. You need an invalidation mechanism, validation on reuse, a versioned URL, or another policy matching that requirement.

For public assets with content-hashed names, a long lifetime can fit because changed bytes get a different URL. HTML that points to those assets usually needs a different policy. MDN HTTP caching

Validation asks a cheaper question

When the client needs to validate the stored article, it can send its validator:

GET /en/blog/example HTTP/1.1
If-None-Match: "article-184-revision-7"

If the selected representation is unchanged, the server can respond with 304 instead of retransmitting the body. If it changed, the server returns the current representation and validator. ETags identify versions of representations; strong and weak validators have different comparison semantics. MDN ETag

Validation still has a network round trip. It may also have substantial server work if your application renders the entire document before computing its ETag.

For this article system, a stored revision number could make the decision inexpensive, provided that it changes whenever the representation changes. If changing the shared navigation alters the page, a validator based only on the article body is insufficient for the full HTML response.

That is a correctness detail, not just an optimization detail.

Cache keys decide whether reuse is safe

Suppose one route chooses a language using a request header. Caches must distinguish the relevant variants. The Vary header tells caches which request headers influenced selection. Every additional varying input can reduce reuse, and variation does not replace authorization. MDN Vary

I would prefer explicit language URLs for this hypothetical blog because they make the representation boundary easier to inspect. That is an application design preference, not a requirement of HTTP.

For personalized content, begin with a deliberately private policy and inspect actual CDN configuration. Do not assume that setting a cookie, using a recognizable URL prefix, or having an authentication check somewhere in the application automatically prevents every misconfigured intermediary from sharing the response.

An edit crosses several caches

Consider this sequence:

Editor updates article
Database contains revision 8
Application cache still contains revision 7
CDN has revision 7
Reader's browser has revision 7

Purging the CDN alone does not necessarily fix the application cache. Purging server caches does not reach into a browser and remove a fresh response already stored there.

Write down the path from edit to visibility. Which layers expire naturally? Which are invalidated? How long can each legitimately serve the old version? What happens if the purge request fails?

For a small blog, a short public lifetime plus reliable origin validation may be simpler than a complicated purge protocol. For stock availability or account permissions, the tolerances differ.

Verify the policy as a user journey

I would test an anonymous request, a repeat request, an authenticated request, and a request after editing. Inspect the response headers at the public edge as well as directly at the application, because intermediaries can change behavior.

A conditional check can look like this, using the actual ETag observed on the first response:

curl -i https://example.com/en/blog/example
curl -i -H 'If-None-Match: "article-184-revision-7"' \
  https://example.com/en/blog/example

Those example commands illustrate a check; they are not measurements of this site. Browser developer tools are also necessary when checking what the browser actually reused.

HTTP caching works best when the response's audience, identity, and acceptable age are explicit. Once those decisions are clear, the protocol can save work across the entire request path instead of only hiding a slow database query behind another service.

# Comments (0)

No comments yet. Be the first.

0/1000