A 304 keeps the cached body and updates stored header fields except Content-Length. Four caches on a local origin: empty bodies, extra requests and the RFC 9111 rule.
A response with no body that changes the cache
The catalogue service at Belgavi's Book Store publishes /catalog.json, a list of titles that the storefront, the help-desk agent and a reporting job all read. Responses carry an entity tag and a short freshness lifetime: ETag: "cat-v1" and Cache-Control: max-age=2. When a cached copy goes stale, a client sends If-None-Match: "cat-v1", and if nothing changed the server answers 304 Not Modified with no body.
Three client teams wrote their own small caches, and all three got the 304 wrong in a different way. One stored the 304 response itself and served an empty catalogue until the next full download. One kept the body but ignored the headers in the 304, so it kept revalidating every two seconds even after the server lengthened the lifetime to a minute. One copied every header from the 304 into the stored response, including a Content-Length: 0 the server sent, and then framed the cached body with it.
All three misread what a 304 is. It is not an empty response and not a no-op. It is a statement that the stored representation is still the current one, together with fresh metadata about it. The cache must keep its body and update its stored header fields.
What the standards say
RFC 9110 defines the 304 status: the server sends it to a conditional GET or HEAD when the condition evaluates to false, and the response cannot contain content. Of the header fields that would have been sent in a 200 response to the same request, the server must send Content-Location, Date, ETag, Vary, Cache-Control and Expires, and should not send other representation metadata unless it exists to guide cache updates.
RFC 9111 then says what a cache does with it. Section 4.3.4, freshening stored responses upon validation, selects which stored responses the 304 applies to: those with a matching strong validator if the 304 carries one, otherwise the most recent with a matching weak validator, otherwise a lone stored response without a validator when the 304 has none either. For each selected response, the cache must update its header fields with the fields in the 304, as described in section 3.2.
Section 3.2 is precise about the exceptions. The cache adds each header field from the new response, replacing existing values, except fields excepted from storage, fields the stored response depends on, fields the recipient processes and removes automatically such as Content-Range, and the Content-Length header field. A cache that stores decoded content or a parsed tree may also decline to update fields like Content-Encoding or Content-Type whose change would make the stored data inconsistent with its metadata.
Freshness is recomputed from the updated fields. If the 304 says max-age=60, the stored response is now fresh for sixty seconds from the time the 304 was received.
A small cache that does it right
The companion example runs a local origin on 127.0.0.1 with Python's http.server and four caches that talk to it over real HTTP with http.client. Only the clock is simulated, so the timeline is reproducible. The correct update path is a few lines:
if r.status == 304:
for name, value in headers.items():
if name == 'content-length':
continue # RFC 9111 section 3.2: never update Content-Length from a 304
entry['headers'][name] = value
entry['stored_at'] = now # freshness restarts from the validation time
return entry['body'] # the stored body is still the representationThe origin returns version 1 of the catalogue with max-age=2, answers matching revalidations with 304, max-age=60 and, like some real servers, Content-Length: 0, and publishes version 2 at second 65. Each cache replays requests at seconds 0, 1, 3, 4, 10, 30, 61, 62, 70 and 100.
The worked example
| Cache behaviour on 304 | Origin requests | 304 answers | Wrong bodies served |
|---|---|---|---|
| Store the 304 itself | 4 | 2 | 7 of 10 |
| Keep body and old headers | 7 | 5 | 0 of 10 |
| Merge every header, Content-Length included | 4 | 2 | 7 of 10 |
| Merge headers except Content-Length (RFC 9111) | 4 | 2 | 0 of 10 |
The first and third caches served an empty catalogue from second 3 until the new version arrived at second 70, and again after the second 304 at second 100. They made as few origin requests as the correct cache, so a dashboard that measured hit rate would have called them efficient. The second cache always served correct data but made seven origin requests instead of four, because it never learned that the lifetime had changed. Only the fourth was both correct and economical.
Every cache picked up version 2 at second 70, because the stored entity tag no longer matched and the origin answered with a full 200. Revalidation protects correctness at the moment of change; what goes wrong in between is entirely the cache's handling of the 304.
The client library trap
Not every client hands you a 304 as a normal response. Python's urllib.request raises HTTPError for it, as the example shows: the conditional request with If-None-Match raised HTTPError 304 with a zero-byte body. Code that wraps urlopen in a try block that treats every HTTPError as a failure will log errors and fall back to a full download on every revalidation. Other libraries return the 304 with an empty body and leave the merge to you. Read your client's documentation for which of these it does before writing cache logic on top of it.
Browsers and shared caches such as CDNs and reverse proxies implement section 4.3.4 themselves. The problems in this guide appear in application-level caches: an SDK that memoises API responses, a service that caches a configuration document, an agent tool that stores fetched pages. Those are exactly the places where people write the update logic by hand.
Validators and when to send them
The server chooses what makes a 304 possible. A strong entity tag says the bytes are identical; a weak one, written W/"...", says the representations are semantically equivalent. Both work for If-None-Match on a GET, because weak comparison is used there. They differ elsewhere: If-Range and range requests require a strong validator, because combining byte ranges from two different encodings of the same document corrupts it. A server that compresses on the fly and marks tags weak is doing the right thing, and a client that resumes downloads with those tags will be refused a range and receive the full body.
Last-Modified with If-Modified-Since is the older validator. It has one-second resolution, so a document changed twice within a second can validate as unchanged. Prefer entity tags derived from content or from a version number, and send both if older clients depend on dates.
Failure modes
- 304 counted as an error. Monitoring that alerts on non-2xx responses floods when a cache starts revalidating correctly. Treat 304 as success.
- Validator lost on store. A cache that drops
ETagwhen storing can never send a conditional request and downloads the full body every time. - Vary ignored. If the stored response depends on
Accept-EncodingorAccept-Language, a 304 to one variant must not update another. Section 4.3.4's selection rules exist for this. - Clock in the wrong place. Freshness after validation is measured from when the 304 was received, not from the original 200.
- Body replaced by a cached transformation. A cache that stores decompressed content must not accept a new
Content-Encodingfrom a 304; the section 3.2 exceptions allow it to keep the old value.
Verification
Test the cache against a local origin, as the example does, rather than against mocks that return whatever the test author expects. The checks that matter are short: after a 304 the body is unchanged, the stored Cache-Control and ETag come from the 304, Content-Length and Content-Range do not, and the number of origin requests drops when the server extends freshness. Add one test with a server that sends Content-Length: 0 on its 304s, because some do, and your cache will meet one eventually.