Sub topic 10
How HTTP Caching Avoids Repeat Requests
This chapter assumes you can read an HTTP request and response (method, status code, headers) and know that the browser checks local state before it uses the network. Here you will see how the response headers decide whether a later request can skip the network entirely, or send a small check instead of downloading the body again.
Why caching exists
Every request costs DNS, TCP, TLS and server time. A cached copy of a response removes some or all of that cost. The server does not control the cache directly. It sends headers, and the browser (and any shared proxy or CDN in between) follows them.
There are two questions a cache answers:
- Is my stored copy still fresh? If yes, use it with no network request.
- If it is stale, is it still correct? Ask the server with a conditional request. If the content has not changed, the server replies without a body.
Freshness with Cache-Control
The response from the earlier chapter included this header:
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=3600
Content-Length: 1256max-age=3600 means the browser may reuse this response for 3600 seconds (one hour) without contacting the server. During that time the request never leaves the machine. DevTools shows it as served from memory cache or disk cache.
Common Cache-Control directives:
| Directive | Meaning |
|---|---|
| `max-age=N` | Fresh for N seconds after the response was received. |
| `no-cache` | May be stored, but must be revalidated before every reuse. |
| `no-store` | Do not store the response at all. Use for sensitive data. |
| `private` | Only the user's browser may store it, not shared caches. |
| `public` | Shared caches such as a CDN may store it too. |
| `immutable` | The content will never change while fresh, so skip revalidation on reload. |
Revalidation with validators
When a stored response becomes stale, the browser does not have to download it again. If the original response carried a validator, the browser can ask whether it is still current. The two common validators are ETag (an opaque version identifier) and Last-Modified (a timestamp).
HTTP/1.1 200 OK
Cache-Control: max-age=60
ETag: "v42"
Last-Modified: Mon, 30 Nov 2026 06:00:00 GMTAfter 60 seconds the copy is stale. The next request carries the validator back:
GET /docs/intro?lang=en HTTP/1.1
Host: example.com
If-None-Match: "v42"
If-Modified-Since: Mon, 30 Nov 2026 06:00:00 GMTIf the resource is unchanged, the server answers with no body:
HTTP/1.1 304 Not Modified
ETag: "v42"
Cache-Control: max-age=60The browser reuses its stored body and restarts the freshness timer. You still pay for the round trip, but not for the transfer. If the resource changed, the server returns a normal 200 with the new body.
Common mistakes
- Caching HTML for too long. If
index.htmlhas a longmax-age, users keep seeing old pages and you cannot force an update. - Confusing
no-cachewithno-store.no-cachestill stores the response. Onlyno-storeprevents storage. - Reusing file names for changed assets. A common pattern is to put a content hash in the file name, such as
app.3f9a1c.js, and serve it with a longmax-ageandimmutable. A new build produces a new URL, so the old cache entry is simply never requested. - Expecting a hard reload to behave like a normal load. A hard reload bypasses the cache on purpose, so it does not show what returning visitors experience.
Exercise
A script is served with Cache-Control: max-age=300 and ETag: "a1". Six minutes after the first load, the user reloads the page normally. What does the browser send, and what response lets it avoid downloading the script again?
Show answer
The copy is stale (300 seconds is five minutes), so the browser sends a conditional GET with If-None-Match: "a1". If the script is unchanged, the server replies 304 Not Modified with no body, and the browser reuses its stored copy.