Sub topic 11
Cache-Control, ETag, and Last-Modified
This chapter assumes you know basic HTML, JSON, HTTP responses, and how search crawlers request pages. You should also understand that a browser or content delivery network (CDN) can store a response and serve it without contacting the origin server for every request.
HTTP caching affects how quickly users and crawlers see changed facts. A page can have the correct price at the origin while a CDN still serves an older response. Use cache controls and validators to make freshness predictable.
How HTTP caching works
A server response can tell a cache three things:
- How long it may reuse the response.
- Which version identifier represents the response.
- How to ask whether the stored response is still current.
The main headers are:
| Header | Purpose | Example |
|---|---|---|
| `Cache-Control` | Defines caching and revalidation rules | `public, max-age=300` |
| `ETag` | Identifies a specific representation | `"product-v42"` |
| `Last-Modified` | Gives the last meaningful modification time | `Fri, 20 Nov 2026 08:00:00 GMT` |
Cache-Control: public, max-age=300 permits shared caches to reuse the response for 300 seconds. After that period, the cache should revalidate it. stale-while-revalidate=60 can allow a cache to serve the old response briefly while it checks for a new one.
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Cache-Control: public, max-age=300, stale-while-revalidate=60
ETag: "product-trail-runner-2-v42"
Last-Modified: Fri, 20 Nov 2026 08:00:00 GMT
<html>
<body>
<span class="price">89.00 EUR</span>
</body>
</html>The five-minute max-age is a freshness window, not a guarantee that every visitor sees a new price within exactly five minutes. A cache may have its own rules, and an application may need to purge cached objects immediately after an urgent change.
Validators and conditional requests
An ETag is a validator, which is a token used to check whether a cached representation changed. When the cache wants to revalidate, it sends the token back:
GET /p/trail-runner-2 HTTP/1.1
Host: example.com
If-None-Match: "product-trail-runner-2-v42"If the content is unchanged, the server returns:
HTTP/1.1 304 Not Modified
ETag: "product-trail-runner-2-v42"A 304 Not Modified response has no page body. The cache reuses its stored body, saving bandwidth while confirming that the cached facts remain valid.
Last-Modified supports a similar flow:
GET /p/trail-runner-2 HTTP/1.1
Host: example.com
If-Modified-Since: Fri, 20 Nov 2026 08:00:00 GMTThe server can return 304 when the resource has not changed since that timestamp. ETags are usually more precise. Timestamps can miss changes when a system records modifications only to the nearest second, or when separate servers have slightly different clocks.
Choosing cache rules for changing facts
Use longer cache lifetimes for stable content and shorter lifetimes for facts that can change quickly.
For a product page, a practical pattern is:
- Update the product record.
- Render the visible price, JSON-LD, and feed from that record.
- Generate a new ETag when the rendered representation changes.
- Purge the CDN object if the change must appear immediately.
- Let later requests receive the new response and validators.
Cache invalidation
When a price, availability value, or product description changes, expiration alone may be too slow. Configure the application or CDN to purge the affected URL, cache key, or tag. Then verify the response from multiple cache locations.
Do not confuse Last-Modified with cache invalidation. It describes when content changed. It does not force every cache to discard its stored copy. Similarly, changing an ETag helps on the next revalidation but does not remove a still-fresh cached response.
Exercise
Exercise
A product price changes, but the CDN has a response with max-age=3600 and an old ETag. What should the deployment process do if the new price must appear immediately?
Show answer
Update the authoritative product record, regenerate the page and its ETag, purge the CDN entry, and verify that the next response contains the new price and validators.