What Is HTTP Caching - and When You Actually Don't Need It

HTTP caching is the part of the HTTP protocol that lets a browser, a proxy, or a CDN reuse a response it already holds instead of asking your server for it again. When someone asks what is HTTP caching doing in their architecture diagram, the honest answer is that it is a contract: your server attaches headers describing how long a response may be reused and how to check whether it is still good, and every cache along the path obeys that contract.

It exists to solve one problem: the same bytes being computed and shipped over and over. A product image, a JavaScript bundle, a catalogue page that changes twice a day - all of these get requested far more often than they change, and every repeat is latency for the user and load for you.

The cost is that you no longer control when a client stops seeing an old version. Caching trades freshness for latency and load, and the whole design of the headers is about letting you choose that tradeoff per response instead of globally. From below of long thin blue cables connected to row of small white connectors on system block in data center

ConceptHTTP caching
Where it sitsProtocol-level infrastructure - sits between the client and your origin, in the browser and at every proxy and CDN on the path
In one sentenceResponse headers tell every cache on the path how long it may reuse a response and how to cheaply check whether it is still valid.

The 60-Second Answer

What it does: your response carries headers (Cache-Control, ETag, Last-Modified) that let browsers and CDNs serve it again without touching your origin.

The one problem it solves: repeat requests for content that changes far less often than it is read - you stop paying origin compute and round-trip latency for them.

The one cost it adds: you lose the ability to retract a response. Once a cache has it with a lifetime, you cannot reliably pull it back; you can only wait it out or change the URL.

Decision rule: if the response is identical for many users and tolerates being seconds-to-days stale, cache it. If it is personalised or must be exact, use revalidation instead of a lifetime, or skip caching entirely.

How It Actually Works

1. A cache stores responses under a key, and the key is mostly the URL

Method plus URL, widened by Vary

A cache is a map. The key is derived from the request method and the full URL - and then widened by whatever request headers the response names in Vary. If you send Vary: Accept-Encoding, the cache stores a separate entry per encoding; if you send Vary: Accept-Language, one per language.

This is why caching is a URL-design problem before it is a header problem. Two responses that differ must live at different keys, or something must tell the cache which request headers made them differ.

Vary: Accept-Encoding, Accept-Language

⚠️ In production — Vary: Cookie or Vary: User-Agent looks safe and quietly destroys your hit rate - every distinct cookie jar or UA string becomes its own cache entry, so a shared cache stores thousands of copies and hits almost none of them. Worse, forgetting Vary when the response really does depend on a request header is how one user's content gets served to another.


2. The server declares a lifetime, and the cache counts down

max-age makes a response fresh

Cache-Control: max-age=<seconds> says: for this many seconds after the response was generated, any cache may hand this out with no network call at all. That is the fresh state, and it is the only state that costs zero round trips.

The cache tracks elapsed time in the Age header, so a response can arrive at the browser already partly used up by a CDN that fetched it earlier. If no lifetime is given at all, a cache is permitted to guess one from Last-Modified - heuristic freshness - which means "I sent no caching headers" is not the same as "nothing will cache this".

Cache-Control: max-age=300

⚠️ In production — Choosing max-age is choosing your maximum staleness window, and there is no undo. A long lifetime on an HTML document is the classic production incident: you ship a bad page, fix it in a minute, and users keep getting the broken one for as long as you told them to.


3. When a response goes stale, the cache revalidates instead of refetching

Conditional requests and 304

Once max-age elapses the entry is stale - not deleted. The cache now asks your origin whether its copy is still good, using a conditional request built from the validators you sent: ETag becomes If-None-Match, Last-Modified becomes If-Modified-Since.

If nothing changed, you reply 304 Not Modified with no body, and the cache refreshes its countdown and serves the stored copy. You still pay the round trip, but not the payload or the rendering work.

ETag: "a1b2c3"
If-None-Match: "a1b2c3"  ->  304 Not Modified

⚠️ In production — Revalidation only saves bandwidth if generating the ETag is cheaper than generating the response. If your handler renders the whole page, hashes it, and then returns 304, you paid full origin cost for an empty body - the saving shows up on the wire and nowhere else.


4. Private and shared caches get different instructions

One user's copy versus everyone's

A browser cache is private: one user, one device. A CDN or reverse proxy is a shared cache serving everyone. The same response usually wants different rules for the two, so the protocol separates them.

Cache-Control: private means only the end user's own cache may store it. public makes it storable by shared caches even when the request looked personalised. s-maxage overrides max-age for shared caches only - the standard pattern for "let the CDN hold this for a while, let the browser hold it briefly".

Cache-Control: public, max-age=60, s-maxage=600

⚠️ In production — Getting this backwards is the highest-severity caching bug there is: a logged-in response marked public and keyed only by URL will be served to the next visitor. Default to private for anything derived from a session, and treat public as something you add deliberately, per route.


5. Opting out is also explicit, and the names are misleading

no-store, no-cache, must-revalidate

no-store means do not write this to storage at all - the one to use for genuinely sensitive bodies. no-cache does not mean that; it means store it, but revalidate before every reuse. must-revalidate means once stale, never serve it without checking, even if the origin is unreachable.

The practical combination for "always current, but let me skip the payload when unchanged" is no-cache plus an ETag - you get a conditional request every time and a cheap 304 most of the time.

Cache-Control: no-cache
ETag: "a1b2c3"

⚠️ In production — no-store on everything is the reflex when a cache bug burns you, and it silently removes conditional requests too - now every request ships the full body. If the problem was wrong cache keys, fix Vary and private; no-store is for data that must not sit on disk, not for data that must be fresh.


6. You cannot invalidate reliably, so you change the URL instead

Content-hashed names and immutable

There is no mechanism in the protocol for your origin to reach out and purge a response already sitting in millions of browser caches. CDNs offer purge APIs for their own layer; the browser layer is beyond your reach until the lifetime expires.

The way out is to make the URL itself depend on the content. A bundler emits app.a1b2c3.js; when the code changes, the filename changes, and the old entry is simply never requested again. Because that URL can never mean anything else, it is safe to give it a very long lifetime and mark it immutable so caches skip revalidation entirely.

Cache-Control: public, max-age=31536000, immutable

⚠️ In production — This only works if the document that references those URLs is itself short-lived or revalidated. Long-caching your HTML alongside your hashed assets means clients keep requesting the old asset names forever - the pattern is immutable assets, near-zero-lifetime entry point.


7. Stale copies can absorb latency spikes and outages

Serve stale, refresh in background

Strict freshness makes every expiry a blocking origin call, which means your traffic pattern turns into a pattern of synchronised origin hits. The stale-serving extensions break that coupling: stale-while-revalidate lets a cache serve the stale copy immediately and refresh it in the background, and stale-if-error lets it serve the stale copy when the origin returns an error or is unreachable.

This is what turns a CDN from a bandwidth saver into an availability layer - users keep getting a slightly old page while your origin is down.

Cache-Control: max-age=60, stale-while-revalidate=600, stale-if-error=86400

⚠️ In production — Support for these directives varies by cache implementation, so verify behaviour on the specific CDN and client stack you actually run rather than assuming. And be clear about what you have agreed to: during an origin outage, users see data as old as your stale window - which is correct for a marketing page and wrong for an account balance.

Two professionals discussing project plans at whiteboard in office setting. ## Where You Meet It
  • A single-page application served from a CDN. The JavaScript and CSS bundles have content hashes in their filenames and a year-long immutable lifetime, so a returning user downloads none of them. index.html gets no-cache with an ETag, so each visit is one small conditional request that usually comes back 304 and immediately points at the already-cached bundles.
  • A public product catalogue API behind a reverse proxy. Listing endpoints carry a short s-maxage so the proxy absorbs the read storm during a campaign while the browser keeps almost nothing; the origin sees a trickle of refreshes instead of the full request rate. Prices stay at most one s-maxage window out of date, which the business accepted explicitly.
  • A logged-in dashboard. Every response is private, no-store because it is derived from a session, and the speedup comes from caching inside the application - query results, computed aggregates - rather than from HTTP caching. The only HTTP-cached things on the page are the static assets and the avatar images.
  • A news homepage during a traffic spike. max-age is tens of seconds with a much larger stale-while-revalidate, so the CDN serves instantly from its stored copy and refreshes out of band. When the origin later fails a deploy, stale-if-error keeps the site readable instead of showing an error page.
  • An image service. Original uploads live at URLs containing a hash of the file, cached long and public; the derived sizes are separate URLs with the same treatment. Replacing a user's photo writes a new URL and updates the reference, because purging the old one everywhere was never an option.

When You Actually Need It

  • Your access logs show the same URLs returning identical bodies repeatedly - the same bundle, image, or listing page dominating request counts with an unchanging response.
  • A measurable share of origin CPU goes to rendering responses that are not user-specific, and the content's real change rate is much lower than its request rate.
  • Your static assets are emitted with content-hashed filenames, which makes long lifetimes safe with no invalidation design work at all.
  • Users are geographically spread and the response is identical for all of them, so a shared cache near them removes a round trip you cannot otherwise shorten.
  • You can state a specific acceptable staleness for the response - "prices may be a minute old" - and a product owner agrees with that number.
  • You want read availability to survive origin incidents, and serving a slightly old version is better for your users than serving an error.

When You Don't

  • Low traffic with a comfortable origin. If your service handles its peak without strain and nobody is complaining about latency, caching headers add a failure mode and a debugging layer for no measured gain. Set sane defaults at your framework or CDN level, cache your static assets, and leave the dynamic routes alone until a profile points at them.
  • Per-user responses. A dashboard, an inbox, a cart - keying these in a shared cache is a data-leak risk, and keying them per user in a browser cache usually saves one request that was not the bottleneck. Cache the expensive query inside your application instead, where you control invalidation.
  • Data that must be exact at read time: balances, inventory counts during checkout, permission checks. Use no-store or no-cache plus an ETag and accept the round trip; a stale answer here is a correctness bug, not a performance tradeoff.
  • When the actual problem is a slow origin. If a page takes seconds because a query has no index or an N+1 is fanning out, caching hides it for the cached keys and leaves every cache miss, every new URL, and every deploy exactly as slow. Fix the query first - it is cheaper to operate than a cache layer.
  • When you cannot describe how a wrong response gets removed. If invalidation would mean coordinating purges across a CDN and millions of browsers, you do not have a caching design, you have a deployment hazard. Either make the URL content-addressed, or keep the lifetime at zero and rely on revalidation.
  • Write-heavy or one-shot endpoints: webhooks, POST handlers, generated reports fetched once. There is no reuse to capture, so the headers are noise - spend the effort on the read paths that actually repeat. Close-up of tower servers in a data center with blue and red lighting.

What People Get Wrong

"If I don't send any caching headers, nothing gets cached."

Caches are allowed to apply heuristic freshness to responses that look cacheable, typically inferring a lifetime from Last-Modified. Silence is a choice to let someone else decide; if you need a specific behaviour you have to state it.

"no-cache means don't cache it."

no-cache means store it but revalidate before each reuse - it is a freshness directive, not a storage ban. no-store is the one that forbids writing the response down, and it is the one to reach for with sensitive bodies.

"Putting a CDN in front of the app is the caching work."

A CDN obeys the headers your origin sends. Without correct Cache-Control, Vary, and validators it mostly becomes a proxy with extra hops - and with incorrect public or missing Vary it becomes an efficient way to serve the wrong user's response.

"A cache hit rate near 100% means the caching is well tuned."

Hit rate tells you nothing about whether the hits were correct or whether the staleness is acceptable. A long lifetime on content that changes often produces an excellent hit rate and users looking at yesterday's page.

Why It Was Built This Way

HTTP caching is declarative because of a constraint the designers could not engineer around: the origin does not know who holds its responses. Between your server and a user there may be a CDN, a corporate proxy, a service worker, and a browser cache, operated by four different parties with no channel back to you. Any design that required the origin to track or notify holders was impossible at the scale of the web, so the protocol inverted the problem - the response carries its own caching policy, and every participant enforces it locally with no coordination.

What that bought is enormous: a cache anywhere on the path can serve a request correctly knowing nothing about your application, and the web's read traffic scales out horizontally without any application-level agreement. What it traded away is revocation. Because there is no path from origin back to holder, a lifetime you hand out is a promise you cannot withdraw - which is exactly why max-age is the single most consequential number in the system, and why content-addressed URLs became the standard answer. If you cannot recall a response, you change the question instead.

The two-tier design - freshness for the zero-round-trip case, validators for the cheap-check case - exists because those are genuinely different economics. A fresh hit costs nothing and risks staleness; a revalidation costs a round trip and risks nothing. Most real caching decisions are just picking where on that line each response sits, per route, with an eye on what a wrong answer would cost you. The reason stale-serving extensions were added later is that strict expiry synchronises origin load at expiry boundaries, and the fix was to let caches decouple serving from refreshing - which quietly turned caching from a performance feature into an availability one.

Related Briefs

What to Read Next

  • cache control headers explained
  • how CDNs work
  • etag vs last-modified

HTTP caching is worth reaching for when the same response is read far more often than it changes, and when you can say out loud how stale it may be. The moment you cannot answer that - because the response is per-user, must be exact, or has no sane invalidation story - the correct move is a zero lifetime with an ETag, or no caching at all and a fix inside the application. Every lifetime you publish is a promise you cannot take back, so set it deliberately and keep your entry-point document short-lived.

댓글

이 블로그의 인기 게시물

What Is a Message Queue - and When You Actually Don't Need One

What Is a Bloom Filter - and When You Actually Don't Need One

When to Use GraphQL Over REST API - and When REST Is Still the Right Call