Quick revision
The short answer
The gateway may store and share only responses that explicitly allow it: under HTTP caching rules a shared cache must not reuse a response to a request with Authorization unless the response says public, s-maxage or must-revalidate, and we go further and cache nothing that is not explicitly marked. Per-tenant responses carry the tenant in the cache key or are marked private, never keyed on raw tokens, and because a cached response skips the origin's authorisation check, everyone sharing a key must be entitled to it or the gateway must authorise before serving. We give teams a short header standard with no-store as the default and roll out per API from an allow list, starting in observe mode.
What to remember
In this answer, we saw that a shared gateway cache may reuse a response to an authorised request only when the response explicitly allows it, and set the gateway stricter still, caching only explicitly marked responses from allow-listed APIs with no heuristic freshness. Per-tenant data gets the tenant from the verified token into the key, never the raw token, and per-user data stays private. Because a cache hit skips backend authorisation, everyone sharing a key must be entitled to it or the gateway checks scopes first. Teams follow a four-row header standard with no-store as the default, and the rollout goes from observe mode to short TTLs with permanent sampled verification.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The gateway may store and share only responses that explicitly allow it: under HTTP caching rules a shared cache must not reuse a response to a request with Authorization unless the response says public, s-maxage or must-revalidate, and we go further and cache nothing that is not explicitly marked. Per-tenant responses carry the tenant in the cache key or are marked private, never keyed on raw tokens, and because a cached response skips the origin's authorisation check, everyone sharing a key must be entitled to it or the gateway must authorise before serving. We give teams a short header standard with no-store as the default and roll out per API from an allow list, starting in observe mode.
Why this question is asked
A shared cache in an API gateway can remove a large share of backend load in one move, which is why platform teams like it. It can also serve one tenant's data to another, or serve data to a caller who should have been refused, which is the worst incident an API platform can have. With 80 APIs, mostly authenticated with bearer tokens and inconsistent headers, the default must be safe and the rollout must prove itself. Interviewers ask this to see whether we know the HTTP rules for authorised requests and whether we can turn them into a policy that teams can follow. In this answer, we will recap the rules, decide what the gateway may store, handle tenants in the cache key, deal with authorisation, write the header standard for teams, and plan a safe rollout.
1A Quick Recap: Shared Caches and the Authorization Header
A shared cache answers many callers from one stored response, so it must be sure the response is the same for all of them. HTTP has a specific rule for this. A request with an Authorization header, such as a bearer token, probably gets a response meant for that caller. So the HTTP caching specification says a shared cache must not use a stored response to satisfy such a request unless the response explicitly allows it, with public, s-maxage or must-revalidate in its Cache-Control.
Our situation adds two complications. Most of the 80 APIs receive bearer tokens, so this rule applies to almost every request. And many teams set no Cache-Control at all, which leaves caches free to apply heuristic freshness, guessing a lifetime from Last-Modified. A gateway that follows only the minimum rules could therefore still store responses no one meant to share.
2What the Gateway May Store
So we set the gateway's policy stricter than the minimum: it caches nothing that is not explicitly marked as shareable, and it never applies heuristic freshness. A response is stored for sharing only if it carries public or s-maxage, a status we allow (200, 404 for some APIs), no Set-Cookie, and comes from an API on the gateway's allow list. A response marked only must-revalidate is legal to share under the specification, but our gateway does not store it, because a team that wanted sharing should say so with s-maxage.
- API on the cache allow list?YesContinueNoPass through, do not store
- Response has public or s-maxage?YesContinueNoPass through, do not store
- private, no-store or Set-Cookie present?YesPass through, do not storeNoContinue
- Build the keymethod, path, normalised query, tenant if declared
- Store with the s-maxage lifetimeVary values recorded with the entry
5 steps in order. Play them, or step through them one at a time.
3Tenants in the Cache Key, Never Raw Tokens
Some responses differ per tenant: the same GET /plans returns each customer's own plans. If the key is only the path, the first tenant's answer is served to every tenant. There are two safe ways. The tenant can be part of the path, /tenants/acme/plans, so the URL already separates them. Or the gateway extracts the tenant from the validated token, a claim it trusts because it has verified the signature, writes it into a normalised header such as X-Tenant-Id, and adds that header to the key for APIs that declare they vary by tenant. The backend sends Vary: X-Tenant-Id so the dependency is explicit.
What we must never do is key on the raw Authorization header. Tokens are secrets, so storing them in cache keys and logs leaks credentials, and every token is different and short-lived, so each caller gets their own entries and nothing is shared. We would carry all the risk of a shared cache and none of the benefit. Responses that differ per user rather than per tenant, such as GET /me, are simply marked private and never stored at the gateway.
4A Cached Response Skips the Origin's Authorisation
This is the subtle part. When the gateway serves from cache, the backend never sees the request, so its permission checks never run. If a tenant's admin-only report is cached under a key that includes only the tenant, a non-admin user from the same tenant gets it from cache, even though the backend would have refused them.
So for every cached response, either everyone who can produce the same cache key is entitled to the same response, or the gateway itself checks authorisation before serving from cache. In practice we require both a valid token, which the gateway already verifies on every request, cached or not, and a declared rule per API: public data, data every user of a tenant may see, or data that needs a role or scope, in which case the role becomes part of the key or the gateway checks the scope before the cache lookup.
5The Header Standard for API Teams
Teams need a short rule they can apply without reading the HTTP specification, and the default must be safe for the many APIs nobody has reviewed yet.
| Kind of data | Headers to send | Gateway behaviour |
|---|---|---|
| Public, same for every caller | public, s-maxage=N, ETag | Shared; key is path and normalised query |
| Per-tenant | s-maxage=N, Vary: X-Tenant-Id, ETag | Shared within the tenant via the key rule |
| Per-user or permission-dependent | private, no-cache or no-store | Never stored at the gateway |
| Not yet reviewed | no-store (gateway default) | Passed through untouched |
The fourth row matters most. Until a team has classified its API and a rule is on the allow list, the gateway treats it as no-store, so inconsistent or missing headers cannot cause a leak.
6Rolling Out Safely
We roll out per API, from the allow list, in three stages. First, observe mode: the gateway computes what it would have cached and under which key, stores only a hash of each would-be-cached response, and when the next request with the same key reaches the backend, compares the backend's real response with the hash. Some differences are genuine updates, but differences that cluster by tenant, user or language mean the key is missing a dimension, and we fix the rule before anything is served from cache.
Second, we enable caching for that API with short TTLs, for example 30 seconds, while watching hit ratio, backend error rates and latency. Third, we keep a sampled verification running permanently: for a small share of cache hits, the gateway also asks the backend with the real caller's credentials and compares the result, alerting on any cross-tenant or cross-user mismatch. Only after a clean period do we raise TTLs to what each team asked for.
7Follow-up questions
How would you detect a cross-tenant leak if one happened?
Several ways together. Backends add a response header naming the tenant the data belongs to, and the gateway checks it equals the caller's tenant before returning a cached response, logging and blocking any mismatch; that is a cheap invariant on every hit. Sampled verification against the backend catches subtler differences. Synthetic tests with two test tenants request the same cached routes and assert they never see each other's data. And gateway logs record the cache key and the caller's tenant for each hit, so if a report comes in we can find exactly which entries were shared with whom.
Why not simply cache everything for a few seconds?
Because a leak does not get smaller with a short TTL; it only affects fewer requests. A per-user response served to another user for five seconds is still a data exposure. The safe approach is to cache only what is declared shareable, and use short TTLs during rollout to limit staleness, not to excuse a wrong key.
8Conclusion
In this answer, we saw that a shared gateway cache may reuse a response to an authorised request only when the response explicitly allows it, and set the gateway stricter still, caching only explicitly marked responses from allow-listed APIs with no heuristic freshness. Per-tenant data gets the tenant from the verified token into the key, never the raw token, and per-user data stays private. Because a cache hit skips backend authorisation, everyone sharing a key must be entitled to it or the gateway checks scopes first. Teams follow a four-row header standard with no-store as the default, and the rollout goes from observe mode to short TTLs with permanent sampled verification.
Open with
I would start from the HTTP rule: a shared cache must not reuse a response to a request with Authorization unless the response says public, s-maxage or must-revalidate.
Then, if they want more
On top of that, the gateway caches nothing that is not explicitly marked shareable, from an allow list of APIs, with no heuristic caching, so missing headers can never cause a leak.
For per-tenant responses, the tenant goes into the cache key, either through the path or through a normalised header the gateway takes from the verified token, and the backend sends Vary for it. I would never key on the raw token, because that stores secrets and shares nothing. Per-user responses are private and never stored.
The subtle part is that a cache hit skips the backend's authorisation, so everyone who can produce the same key must be entitled to the same response, or the gateway must check the scope or role before the lookup.
For teams I would publish a short standard: public data with s-maxage and an ETag, per-tenant data with an explicit key rule, personal data private or no-store, and no-store as the default until reviewed.
Rollout goes per API: observe mode that compares would-be-cached responses with real ones, then short TTLs while watching hit ratio and errors, with sampled checks for cross-tenant mismatches kept on permanently.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Which responses may the gateway store and serve to other callers, what rules do you give...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/1003f6dd2d/