HTTP Caching Explained: Cache-Control, ETag, CDN

max-age, no-cache vs no-store, ETag and 304, stale-while-revalidate, Vary, CDN caching and the hashed-assets deploy pattern, with examples.

HTTP caching: common questions

What is the difference between no-cache and no-store?

no-cache lets the browser keep a copy but it must check with the server before using it. no-store means do not keep a copy at all.

How should static assets be cached?

Put a content hash in the file name and serve it with Cache-Control: public, max-age=31536000, immutable. Serve the HTML that references them with no-cache.

Related chapters

Keep learning

Overview

Memory cache this tab Service worker your code HTTP cache browser disk CDN edge shared, nearby your server Same logo twice on one page The page already decoded it. No request at all, not even to disk. App shell, offline A service worker intercepted the fetch and answered from its own Cache Storage. It follows your code, not Cache-Control. app.3f9a.js on the next visit Still fresh (max-age=1 year, immutable). Read from disk. Zero network. index.html, another visitor fetched it Shared cache: one visitor’s miss becomes everyone’s hit. s-maxage controls how long the edge keeps it. Personal data. No layer may keep it. Every request pays the full trip. Chapter 70 · http caching The fastest request never leaves the browser Every response carries rules: who may keep it, how long it stays fresh, and how to check it later. The browser and the CDN follow those rules exactly.

4. What you decide in production

no-cache ≠ no-store What each directive allows asks first no-cache: keep it, but ask before every use (a cheap 304). no-store: never write it down. Use no-store for bank pages and tokens, not for HTML you just want fresh. private vs shared A private response may be kept by the browser, never by the CDN. browser ✓ CDN ✗ private: only the browser may keep it. Anything personalized needs it, or a CDN will hand one user’s page to the next. s-maxage (or CDN-Cache-Control) sets the edge TTL separately. Vary is the cache key CDN hit rate. Vary Accept-Encoding keeps about 90 percent. Vary Cookie falls to about 4 percent. 90% hits Vary: Accept-Encoding is fine. Vary: Cookie or User-Agent splits the CDN cache into thousands of copies and the hit rate falls to near zero. Silence is not “don’t cache” A file last modified 30 days ago may be reused for 3 days with no headers. file age Last-Modified: 30 days ago reused ~3d No Cache-Control but a Last-Modified? Browsers guess a lifetime, often 10% of the file’s age. A month-old file can be reused for days. Always send explicit headers. You cannot purge a browser A CDN purge works. A browser copy stays until it expires. A new URL is the only way around it. purge CDN ✓ browser copy ✗ new URL ✓ You can purge a CDN in seconds. A browser keeps its copy until max-age runs out. The only way to change a browser-cached URL early is to change the URL. Clear-Site-Data: "cache" exists for logout. Validators must agree ETags across servers server A "a1", server B "b7" → 200 every time content hash, same everywhere → 304 An ETag built from inode or server id differs per machine behind a load balancer, so every revalidation becomes a full 200. Hash the content. max-age 0 with a 60 second stale-while-revalidate window, then a full check. stale ok Lists that may be a little old: max-age=0, stale-while-revalidate=60. Never cache a 500 for long. Version-bump query keys rather than trusting long TTLs on data. Reading DevTools Size column values from fastest to slowest. (memory cache) (disk cache) Size column: (memory cache), (disk cache), (ServiceWorker), or a 304. From the CDN, Age and x-cache: HIT say the edge answered.

1. Five places can answer a request

The request walks right until someone has a usable copy. Every hop it skips is latency it never pays. 1 min 1 hour 1 year 5 min 2 hours 30 days 200 · full download 1 round trip Nothing was stored, so there is nothing to reuse or to check. Fresh · from cache 0 round trips Stale, served instantly 0 round trips to the reader 0 KB now The old copy is shown now. A background request refreshes it for the next visit. 304 Not Modified The browser sent its ETag. The server said “still the same”. The cached body is reused and its clock restarts. 200 · new version The ETag did not match, so the server sent the new body. Stale and nothing to check with. Without a validator, the browser can only download it again.

2. Fresh, stale, check

Build a header. Then come back later and see what the browser does. Validator and server File changed on server Request again after The browser answers from its own cache. No message to the server. BrowserGET /app.js → disk ✓ refresh in the background… (not asked) If-None-Match: "v1-9c2f" 304 · no body 200 · 48 KB body revalidated → 200 revalidated → 304 new URL → download fresh · disk HTML is always checked, so it found the new script URL. The old app.3f9a.js sits unused in the cache, harmless. One cheap 304 for the HTML. The script is fresh for a year and never even checked. fresh · disk (40 min old) expired → download The HTML was still fresh (v1) but app.js had expired and came back as v2. Old markup, new code: a broken page for up to an hour. Fast, zero trips. Until a deploy lands between two expiry clocks. Always correct, never fast. Each of the page’s files pays a round trip on every visit, even when nothing changed.

3. The deploy pattern

A returning visitor, 40 minutes after their last visit. Ship a new version and see what they get. Hashed + immutable 1 hour on both v2 deployed Deploy v2 loads → v1 HTML + v2 JS

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.