Cache keys for authenticated responses: keep user data isolated
When an application cache stores authenticated responses, its key must reflect the access scope that can change the response. Two callers must not share a key when they can see different data, even if they request the same URL. A cache key does not replace authorization: check the caller's access before reading the cache.
When an application cache stores authenticated responses, its key must reflect the access scope that can change the response. Two callers must not share a key when they can see different data, even if they request the same URL. A cache key does not replace authorization: check the caller's access before reading the cache.
This article focuses on application caches inside API services. Browser and CDN HTTP caches follow separate rules.
Which inputs belong in the key?
Start from the response path and list inputs that change either the result or who may see it:
| Input | Include it when | Example |
|---|---|---|
| Resource ID and route parameters | They select different data | invoice:42 |
| Tenant ID | IDs are scoped to a tenant | tenant:acme |
| Principal or permission scope | Ownership, roles, or ACLs change visibility | Stable user ID or permission-set version |
| Query and representation options | Filters, sorting, page, currency, or language changes output | normalized currency=KRW |
| Data or authorization version | Updates or revocation must stop reuse of old values | resource or policy version |
Include only inputs that actually affect output or access. A tenant-only key is sufficient only if every tenant member has the same visibility. Per-user ownership, field masking, or ACLs require a narrower scope.
Build and review a key
For an illustrative GET /invoices/42, a conceptual key could be:
invoice:v3:tenant=acme:principal=user-81:authz=17:resource=42:currency=KRWHere v3 versions the key layout, and authz=17 illustrates a version that changes when effective permissions change. The actual version and its update rules must match the service's authorization model.
Authenticate the caller and authorize access to the resource before looking up the cache. A hit alone must not grant access.
Select the tenant, stable principal, role/ACL version, resource, and query or representation inputs that change the response.
Serialize fields unambiguously. Normalize only values known to have identical meaning; dropping an option can merge different requests.
Never put raw access tokens or session cookies in keys. Use an internal stable identity and permission version instead. Protect even internal identifiers in logs.
Connect data and permission changes to invalidation, version changes, or a suitable TTL. A TTL does not guarantee immediate permission revocation.
If authorization cannot be checked on each request, define and review the authorization-decision cache's expiry and revocation behavior separately. Until then, disable caching of authenticated responses.
Check for cross-account reuse
Use two test accounts and different tenants. Let caller A populate the cache with an invoice that only A may see, then request the same route as caller B in the same tenant. B must receive only an authorization denial or data B is allowed to see. Repeat with the same resource ID in another tenant. This is a proposed reproducible check, not a claim that a particular service was tested.
Also verify that authorization runs on both misses and hits; role or ACL changes take effect; filters, currency, and language separate representations when needed; secrets do not appear in keys or diagnostics; and failures in authorization or key generation never return cached data.
Application caches versus shared HTTP caches
An application cache can use internal identity dimensions and code-level authorization checks. For shared HTTP caches, RFC 9111 says a response to a request with Authorization can be reused for a later request only when a response directive such as must-revalidate, public, or s-maxage permits shared storage and its conditions are followed. For personal responses, Cache-Control: private prevents shared-cache storage; use no-store when the response must not be stored. private limits where a response is stored; it does not itself protect the message contents. (RFC 9111 §3.5, §5.2.2)
Vary tells HTTP caches to distinguish nominated request headers, such as Accept-Language; it does not perform application role or ACL checks. CDN custom-key and bypass behavior is product-specific, so inspect the deployed key and verify response isolation. (RFC 9111 §4.1, Cloudflare Cache Keys)
References
[RFC 9111: HTTP Caching](https://www.rfc-editor.org/rfc/rfc9111.html)
[Cloudflare Cache Keys](https://developers.cloudflare.com/cache/how-to/cache-keys/)