Resources · 63
HTTP caching: protect personalized responses
Separate shared, private and application caches, inspect keys, revalidation and invalidation after access-right changes.
· 7 min
What this guide helps achieve
- Inventory layers
- Choose retention
- Inspect keys
- Exercise revalidation and change
Quick check
- Does no-cache mean nothing is retained?
- Does Vary protect access rights?
- Should every API be cached?
Step-by-step method
- 01
Inventory layers
List browser, CDN, proxy, service worker and application caches. Classify public and personalized responses. An HTTP policy does not automatically describe caching implemented in code.
Deliverable: layers and owners diagram.
- 02
Choose retention
RFC 9111 distinguishes directives: no-cache requires validation before reuse, no-store prohibits the relevant HTTP storage, and unqualified private excludes shared storage. None alone guarantees system confidentiality.
Deliverable: retention policy per response.
- 03
Inspect keys
Check method, URI and dimensions changing a representation. Vary concerns request fields; it is not authorisation. Seek omitted language, format negotiation and account context in application layers.
Deliverable: keys and separation tests.
- 04
Exercise revalidation and change
Test fresh and stale responses, data updates and revoked rights. Inspect ETag, conditional validation and origin-unavailable behaviour. A formerly correct response may become sensitive after revocation.
Deliverable: before-and-after traces.
- 05
Replay with two accounts
Use fictional identities and markers. Alternate requests to the same URI with warm and cold caches. Check logout, direct links and logging without recording real tokens.
Deliverable: negative tests and invalidation strategy.
Four situations for testing the method
Fictional teaching cases. Each situation connects a signal, a check and a decision bounded by evidence; none represents a client outcome.
| Fictional situation | Concrete check | Decision and limit |
|---|---|---|
| Do not share analysis results across private contexts — A voice-analysis response is reused in another session because the cache key omits the authorised context. | Use fictional data to cross-test two accounts, keys, permissions, retention and purge. Include errors and logs. | Prevent cross-context reuse and limit retained data. A technically cacheable response is not necessarily appropriate for a shared cache. |
| A cache must distinguish JSON representations — Two route versions return different schemas, and a client receives a cached response for the other version. | Test version selection and representation dimensions, then compare cache keys with client validation. | Separate representations and handle incompatibility explicitly. Parseable JSON may still violate the expected contract. |
| Check the same cache policy across clients — Python, PHP and JavaScript clients handle validators and retained content differently, producing divergent results after an update. | Replay the same request-response sequence including headers. Check when each client reuses, revalidates or replaces local content. | Document expected common behaviour instead of assuming a library enforces the intended policy. Include intermediate caches. |
| Measure freshness as well as distributed-cache speed — Two reads are fast, but one still shows old availability after the source changes. | Compare value, version, reading location and time since change. Test invalidation, expiry and source access against the system contract. | Define acceptable freshness per use and monitor divergence. A latency improvement does not establish consistency of served data. |
Reusable worksheet
Complete with your authorised observations. These fields are a working template, not observed results.
| Field | Information to record |
|---|---|
| Response | Public or personalized; dimensions |
| Layer | Cache, owner and key |
| Policy | Retention, freshness and validation |
| Trial | Fictional account, change and observation |
Worked example
Illustrative situation
Fictional example: a CDN reuses a profile response at the same URI for two test accounts.
Decision and expected evidence
The scenario detects leakage, examines policy and keys, then replays after correction and access revocation.
Distinguish the mechanisms
| Mechanism | Purpose | Check or limitation |
|---|---|---|
| no-cache | Permit storage with required validation | Do not read it as no storage |
| Unqualified private | Exclude shared caching | Do not read it as a confidentiality guarantee |
| no-store | Prevent the relevant HTTP storage | Check application caches and history separately |
Management indicators
| Indicator | What it measures | First action |
|---|---|---|
| Account separation | No marker from another account | Inspect body, links and metadata |
| Age and validation | Reuse consistent with policy | Test expiry and unavailable origin |
| Invalidation delay | Time until corrected representation | Name each layer still stale |
Common pitfalls
- Do not read it as no storage
- Do not read it as a confidentiality guarantee
- Check application caches and history separately
Frequently asked questions
Does no-cache mean nothing is retained?
No. It requires validation before reuse; it does not prohibit storage as no-store does.
Does Vary protect access rights?
No. It participates in representation selection; authorisation remains separate.
Should every API be cached?
No. Benefits depend on cost, freshness and risk. Sensitive responses may justify avoiding shared caches.
ETag, HTTP caching and concurrent updates
ETag: revalidate responses and prevent lost updates
An ETag is an opaque validator for the selected representation. It compares versions; it is neither authorisation nor an access token, and it need not be a file hash. Personalised responses require separate decisions about access, cache selection and HTTP conditions.
Local teaching model: a representation already exists; at most one conditional field contains a single ETag or *. GET, HEAD and PUT are covered. Tag lists, date validators, ranges, creation, combined conditions and the already-succeeded modification exception are excluded. No network call, storage or state change occurs; this does not test your API.
Strong and weak ETag comparison serve different purposes
If-Match requires strong comparison: neither tag may start with W/. If-None-Match uses weak comparison, matching the opaque values regardless of W/. Values remain case-sensitive. The * wildcard checks representation existence here, not a particular version.
| Current tag | Received tag | Strong comparison | Weak comparison |
|---|---|---|---|
| "v1" | "v1" | Matches | Matches |
| "v1" | W/"v1" | Does not match | Matches |
| W/"v1" | W/"v1" | Does not match | Matches |
| "v1" | "v2" | Does not match | Does not match |
| "v1" | "V1" | Does not match | Does not match |
Simulate an HTTP decision without contacting a server
The six parameters yield 432 combinations. Requiring a conditional field and selecting 204 for successful PUT are policies of this fictional scenario. Requiring any condition does not prevent every lost update: If-Match: * compares no version.
Model decision :
304: If-None-Match finds a weak match for GET or HEAD. Reuse the stored representation under cache rules with the revalidation metadata.
304 and 204: no response content.
Request excerpt
GET /demo/resource HTTP/1.1
Host: api.example.test
If-None-Match: W/"v1"Response excerpt
HTTP/1.1 304 Not Modified
Date: Thu, 22 Oct 2026 00:00:00 GMT
ETag: "v1"
Cache-Control: private, no-cacheFictional HTTP/1.1 excerpts for reading: bodies are omitted even where allowed. api.example.test is an example domain. The date is fixed. This model does not update the ETag after PUT; the separate walkthrough follows successive versions.
Ten reproducible situations
| Fictional situation | Condition and expected outcome | Interpretation |
|---|---|---|
| Read using a weak tag | GET · "v1"Access allowed · Conditional field optional | W/"v1" and "v1" match weakly. |
| Different client version | GET · "v1"Access allowed · Conditional field optional | The client needs the selected representation. |
| HEAD revalidation | HEAD · "v1"Access allowed · Conditional field optional | The status can be 304; the response has no content. |
| Write based on an old version | PUT · "v2"Access allowed · Conditional field optional | Read again and resolve the conflict before retrying. |
| Weak tag with If-Match | PUT · "v1"Access allowed · Conditional field optional | Strong comparison rejects a weak tag. |
| Required condition absent on PUT | PUT · "v1"Access allowed · Conditional field required | 428 follows the policy announced by this API. |
| Access denied despite matching tag | GET · "v1"Access denied · Conditional field optional | Authorisation precedes HTTP conditions. |
| Write on the correct version | PUT · "v1"Access allowed · Conditional field required | The model accepts PUT and selects a 204 success. |
| If-None-Match: * on existing data | PUT · "v1"Access allowed · Conditional field optional | The wildcard matches any existing representation. |
| If-Match: * on existing data | PUT · W/"v1"Access allowed · Conditional field required | The wildcard passes even with a weak current tag: it does not protect a version. |
Four rules for a personalised response
| Response field | HTTP effect within this scope | Evidence to retain |
|---|---|---|
| Cache-Control: no-cache | Unqualified no-cache permits storage, but any reuse needs successful validation with the origin server. | Observe a conditional round trip; do not read no-cache as a storage prohibition. |
| Cache-Control: no-store | Forbids storage by private and shared HTTP caches. It is not a general privacy guarantee. | Also review logs, history, screenshots and application storage. |
| Cache-Control: private | Unqualified private forbids storage by a shared cache; a private cache may store under the other constraints. | Distinguish browser, proxy and CDN. A directive does not replace access checks. |
| Vary | Adds the named request fields to selection of a stored response. It does not decide who may access a resource. | Test relevant variants. Do not assume a cookie or Authorization automatically prevents all caching. |
Useful API acceptance evidence
- Expected request and status: method, test resource, access rights, conditional field and current ETag. Use fictional data and remove secrets.
- Observed response: status, ETag, Date, Cache-Control, Vary and no content for HEAD, 204 or 304. A 304 must carry the required metadata that would have accompanied the 200.
- Concurrency: verify that a stale write fails without changing the resource; retain the conflict-resolution decision. Do not blindly replace the ETag and resend the same write.
Follow the two-client conflict →
Primary references and limits
The standards define HTTP conditions and caching. The ten cases, laboratory and two-client walkthrough are original illustrations. Displayed results are calculated by this local model, not measurements of this site or your infrastructure.
- IETF · RFC 9110 — HTTP Semantics · June 2022 · STD 97
§8.8.3.2; §13.1.1–13.1.2; §13.2; §15.3.5; §15.4.5 - IETF · RFC 9111 — HTTP Caching · June 2022 · STD 98
§4.1; §4.3; §5.2.2.4–5.2.2.5; §5.2.2.7 - IETF · RFC 6585 — Additional HTTP Status Codes · April 2012 · Standards Track
§3 · 428 Precondition Required
References reviewed
Official references
References consulted: . The method and worksheet propose checks to adapt to your context; they do not constitute certification.
