Caching responses whose visibility depends on roles, permissions, tokens, or policy evaluation.
Authorization-aware caching means the cache design understands that visibility depends on identity and policy, not just on resource ID. A response may be correct for one requester and forbidden for another. If the cache ignores the authorization context, it can serve an answer that was valid for someone else but is unsafe for the current caller.
This is especially tricky because authorization can change over time. A user may lose a role, move tenants, or have a token revoked. Cached responses that were safe a minute ago may no longer be safe now.
sequenceDiagram
participant UserA
participant UserB
participant App
participant Cache
participant Policy
UserA->>App: Request document 42
App->>Policy: evaluate permissions
Policy-->>App: allow
App->>Cache: store response with auth scope
UserB->>App: Request document 42
App->>Cache: check scoped key
Note over App,Cache: Response is safe only if scope matches UserB's permissions
Authorization mistakes in caches are often harder than simple user scoping because two users may share some permissions but not others. The key question becomes whether responses are safe to share across an authorization bucket or whether they must be cached per actor.
Useful patterns include:
This example scopes a document response by tenant and a coarse permission bucket rather than by path alone.
1function documentCacheKey(
2 documentId: string,
3 tenantId: string,
4 permissionBucket: "viewer" | "editor" | "admin"
5) {
6 return `document:${documentId}:tenant:${tenantId}:scope:${permissionBucket}`;
7}
What to notice:
Authorization-aware caching trades off reuse against safety.
The safer default is to under-share rather than over-share.
When is it safe to share a cached response across more than one user?
The stronger answer is that sharing is safe only when the system can prove the response is identical in content and visibility for the whole sharing group, and when changes to that group’s permissions are reflected quickly enough. If that proof is weak, per-user or very narrow scoping is the safer design.