Quick revision
The short answer
First we contain it: disable or clear the cache at once and work with the security and privacy teams to establish which tenants and records were exposed. Then we fix the design: the cache key includes the tenant, and the cache moves out of a static singleton into a container-managed bean, with tests that run two tenants with overlapping product ids and assert isolation. The lasting standard is no mutable static state in services, enforced with static analysis or ArchUnit rules, a review of other services for the same pattern, and a blameless review that focuses on the design gap rather than the person.
What to remember
In this answer, we led a cross-tenant leak caused by a static cache keyed only by product id. We contained it first by disabling and clearing the cache and scoped the exposure with the security and privacy teams. Then we keyed the cache by tenant and product, moved it into a container-managed bean, and added tests with two tenants sharing an id. The standard of no mutable static state is enforced with ArchUnit rules, other services are reviewed for the same pattern, and a blameless review addresses the design gap.
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.
First we contain it: disable or clear the cache at once and work with the security and privacy teams to establish which tenants and records were exposed. Then we fix the design: the cache key includes the tenant, and the cache moves out of a static singleton into a container-managed bean, with tests that run two tenants with overlapping product ids and assert isolation. The lasting standard is no mutable static state in services, enforced with static analysis or ArchUnit rules, a review of other services for the same pattern, and a blameless review that focuses on the design gap rather than the person.
Background to this question
A cross-tenant data leak is one of the most serious incidents a multi-tenant service can have: customers trust that their prices, orders or records are invisible to others, and in many jurisdictions such a leak must be reported. The technical bug here is small, a missing part of a cache key, but leading the response well means acting in the right order, fixing the design rather than the symptom, and making sure the same mistake cannot quietly return. In this answer, we will look at the broken code, walk through containment, apply the fix, write tests that prove isolation, set the standard with automated rules, and close with the wider review and the postmortem.
1What Went Wrong in the Code
A multi-tenant service serves several customers, called tenants, from the same running application, keeping their data apart by a tenant id. In this service, somebody added a cache to speed up product lookups, as a static singleton.
Product ids are only unique within a tenant, so tenant A and tenant B can both have a product 42. The first request fills the cache with A's data, and every later request for 42, from any tenant, gets it. Notice also why the bug was easy to miss: the cache lives in a static field outside the Spring container, so the request's tenant context, held in the container's request handling, was never part of the design.
2First Hour: Contain and Scope the Exposure
The order of actions matters. The first priority is to stop the leak, even at the cost of performance: turn the cache off with a feature flag or configuration if one exists, or deploy a build that bypasses it, and restart or clear every instance so no stale entries remain. A slower service is acceptable; a service that keeps leaking is not.
The second priority is to establish scope, together with the security and privacy teams, because they decide on notification. We need to know since when the cache existed, which endpoints used it, which tenants made requests that could have been served from another tenant's entry, and what fields were exposed. Access logs with tenant ids, product ids and timestamps usually allow a good estimate. We record every step in the incident channel, because the timeline will be needed later.
- Disable or bypass the cacheflag, config or hotfix build
- Clear every instancerestart or evict all entries
- Confirm the leak has stoppedtest with two tenants in production-like data
- Bring in security and privacythey own notification decisions
- Scope the exposure from logstenants, endpoints, fields, time window
- Only then: permanent fixdesign change and tests
6 steps in order. Play them, or step through them one at a time.
3The Fix: Tenant in the Key, Cache in the Container
The permanent fix has two parts. The cache key must contain everything that makes data unique, here the tenant id and the product id. And the cache should be a Spring bean, injected where it is used, so it is visible, testable and replaceable, instead of a static singleton nobody can see.
Notice that the tenant id is a required parameter, not something the cache reads from a thread-local behind our back. That makes it impossible to call the cache without thinking about the tenant. The bounded size and expiry are a bonus: the old map grew forever. With Spring's cache abstraction, the same idea applies to @Cacheable: the key expression must include the tenant, or a custom KeyGenerator must add it.
4Tests That Prove Isolation
A fix without a test can be undone by the next refactoring. The test that matters uses two tenants with overlapping ids, exactly the situation that broke.
Notice that the order is deliberate: tenant A fills the cache first, so the test would have failed against the old code.
5The Standard: No Mutable Static State in Services
The root cause is not one missing key; it is that mutable shared state was possible outside the container, where nobody reviewed it as part of the request flow. The standard we write is short: services hold no mutable static state; caches and registries are beans; and every cache key in a multi-tenant service includes the tenant. A written rule is only as good as its enforcement, so we add it to the build.
These ArchUnit rules fail the build when someone adds a static non-final field or a static map or collection, which catches the common shapes of this mistake. Real constants, such as an immutable lookup table, get an explicit, commented exception, which turns an accident into a reviewed decision.
6Looking Further and Writing the Review
If one service had this pattern, others may too, especially if it was copied from a shared example. We search the codebase for static caches and maps, run the new ArchUnit rules across all services and track the findings, starting with multi-tenant services that handle sensitive data.
The incident review is blameless: it describes what happened, the timeline, the impact and the contributing factors, and focuses on why the system allowed the mistake, such as no rule against static state, no multi-tenant test pattern and no review checklist for caches, rather than on who wrote the line. People who expect blame hide information. The action items are the ones above, each with an owner and a date.
7Follow-up questions
Why not keep the static cache and just add the tenant to the key?
That fixes this bug but keeps the design that allowed it: an invisible, unbounded, global map that tests cannot reset and reviewers do not see in constructors. Moving it into a bean makes the dependency visible, lets tests replace or clear it, and lets us configure size and expiry.
What if the tenant id comes from a thread-local context?
It is safer to pass it explicitly into the cache, as in the fix, so the key cannot be built without it. If a framework puts it in a context, the cache layer should read it once and fail loudly when it is missing, rather than defaulting to no tenant.
Who decides whether customers must be notified?
The security, privacy and legal teams, based on the scope we establish. Engineering's job is to give them an accurate, timely picture of what was exposed, to whom and when, and to stop further exposure.
8Conclusion
In this answer, we led a cross-tenant leak caused by a static cache keyed only by product id. We contained it first by disabling and clearing the cache and scoped the exposure with the security and privacy teams. Then we keyed the cache by tenant and product, moved it into a container-managed bean, and added tests with two tenants sharing an id. The standard of no mutable static state is enforced with ArchUnit rules, other services are reviewed for the same pattern, and a blameless review addresses the design gap.
Open with
My first step is containment: disable or bypass the cache immediately, even if the service gets slower, and clear it on every instance so no wrong entries remain.
Then, if they want more
At the same time I would bring in security and privacy, because they own the notification decision, and our job is to scope the exposure from access logs: since when, which tenants, which endpoints and which fields. Then the fix.
The root bug is that the key is only the product id, and product ids repeat across tenants, so the key must be tenant plus product.
But I would also move the cache out of the static singleton into a Spring bean, with the tenant as an explicit parameter, a bounded size and an expiry, so it is visible in constructors and testable.
I would add a test where two tenants both have product 42, tenant A fills the cache first and tenant B must still get its own data, which would have failed before.
For the standard, no mutable static state in services, enforced in the build with ArchUnit rules such as static fields must be final and no static maps or collections, with explicit exceptions for real constants.
Then I would scan the other services for the same pattern and run a blameless review that focuses on why our system allowed this, not on who wrote it.
About this answer
- Last updated
- 12 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 "A production incident shows one tenant's data in another tenant's response. The cause is...": https://scrivio-interview-library.netlify.app/java/creational-patterns/2a2b06f881/