Quick revision
The short answer
Tenants see each other's changes because each tenant's copy is a shallow copy: the top-level object is new, but the nested rule objects and maps are shared, so one tenant's override changes them for everyone. I would make the loaded templates immutable and share one instance across all 2,000 tenants, store only each tenant's small overrides, and resolve a value by checking the overrides first and the shared template second. That removes the cross-tenant bug and most of the memory at the same time.
What to remember
In this answer, we saw that tenants seeing each other's changes is the signature of shallow copies sharing mutable nested objects. The fix is to make the template immutable and share one instance, store only per-tenant overrides, and resolve values by checking overrides first and the shared template second. Where a full independent copy is needed, an explicit deep copy method with an independence test does the job. Sharing an immutable base cuts memory from gigabytes to a few hundred megabytes and removes the cross-tenant bug at the same time.
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.
Tenants see each other's changes because each tenant's copy is a shallow copy: the top-level object is new, but the nested rule objects and maps are shared, so one tenant's override changes them for everyone. I would make the loaded templates immutable and share one instance across all 2,000 tenants, store only each tenant's small overrides, and resolve a value by checking the overrides first and the shared template second. That removes the cross-tenant bug and most of the memory at the same time.
Why this question is asked
Multi-tenant systems often start by copying a default for each customer and then editing the copy. It works for ten tenants and fails at a few thousand, in two ways at once: memory grows with every tenant, and a copy that is not as independent as it looks leaks one customer's prices into another's. Interviewers like this scenario because both symptoms have the same root cause, and seeing that is the point. In this answer, we will diagnose why tenants see each other's changes, redesign the model as a shared immutable template plus per-tenant overrides, show how values are resolved, discuss when a full deep copy is still needed, and estimate the memory saved.
1Diagnosing the Cross-Tenant Bug
The current code loads the rule templates at startup and, for each tenant, creates a modified copy. The usual way to copy, clone() or a copy constructor that copies only the outer object, makes a shallow copy: the new object has its own fields, but fields that refer to other objects, such as a Map of rule parameters or a List of discount tiers, still point to the same nested objects as the template.
Notice that acme and globex are different RuleSet objects, but both share the template's HashMap and the Rule objects inside it. So setPercent() on Acme's copy changes the one Rule that every tenant uses. The same mistake, done carefully with a deep copy instead, would fix the bug but make memory even worse, because each tenant would then hold a full copy of every rule.
2Why Memory Is High
Let us put numbers on it. Suppose the templates hold 5,000 rules, and each rule with its parameters takes about 2 KB on the heap. One full set is about 10 MB. If each of the 2,000 tenants holds even a partial copy of the outer structures, say a new HashMap with 5,000 entries at roughly 40 bytes per entry plus the map's table, that is around 250 KB per tenant, or 500 MB just for maps that mostly point at identical rules. A true deep copy would be 2,000 times 10 MB, or 20 GB.
But most tenants override only a handful of values, perhaps 10 to 50 parameters each. So almost all of that memory stores the same data many times. This is the clue to the redesign: share what is the same, and store only what is different.
3The Redesign: Shared Immutable Template Plus Overrides
We make the template immutable, meaning nothing can change it after loading. In Java that means records or final classes with final fields, and unmodifiable collections created with Map.copyOf() and List.copyOf(). Then one instance can be shared by every tenant with no risk, because nobody can write to it. Each tenant gets a small object that holds only its overrides.
- Request rule "bulk-discount" for Acme
- Does Acme have an override?a lookup in a map of 10 to 50 entriesYesReturn the overrideNoLook in the shared template
- Read the shared immutable templateone instance for all 2,000 tenants
- Return the template rule
4 steps in order. Play them, or step through them one at a time.
Overrides first, then the shared template. Nothing on this path can change shared data.
Notice that an override never touches the template; it is a separate Rule object in the tenant's own map. Changing a tenant's override means creating a new TenantOverrides, which is cheap because it is small. If we need overrides at the parameter level rather than whole rules, the same idea works with a map keyed by rule id and parameter name.
4When a Full Copy Is Really Needed
Sometimes a tenant needs a genuinely independent copy, for example to start a draft of a new price list that it edits for weeks before publishing. Then we write an explicit deep copy method, such as Template.deepCopy(), that creates new collections and new rule objects all the way down. Because the rule objects are records with immutable values, a deep copy can stop at the collections: immutable leaves can be shared safely even in a deep copy.
Explicit copying needs a test for independence: copy the template, change the copy, and assert that the original did not change. That test would have caught the original bug in a few lines. Adding such a test for every type that offers a copy method is cheap insurance.
5What the Redesign Buys Us
The memory now is one template of about 10 MB plus 2,000 tenants times perhaps 50 overrides of 2 KB, which is about 200 MB in the worst case and usually much less, compared with half a gigabyte or more before. And the cross-tenant bug is gone by construction, not by care: since the shared template cannot be changed, there is no code path through which one tenant can affect another.
There are smaller wins too. Reloading templates becomes an atomic swap of one reference to a new Template, and every tenant sees the new version on its next lookup. An audit of a tenant's pricing is simply its list of overrides, which is far easier to read than a full copy of 5,000 rules.
6Follow-up questions
Is the extra lookup in two maps slower?
Slightly, but it is two hash lookups, which take nanoseconds. Compared with the memory saved and the reduced garbage collection pressure from not holding millions of duplicate objects, it is usually faster overall. If profiling shows a hot spot, we can cache the resolved rules for the busiest tenants.
What if the template is reloaded while tenants are using it?
Because the template is immutable, we build the new one completely and then replace a single volatile reference or an AtomicReference. A lookup in progress keeps using the old template, the next lookup uses the new one, and no lookup ever sees a half-loaded state.
Why not make the copies deep and keep the old design?
That would fix the bug but multiply memory by the number of tenants, potentially to many gigabytes, and every reload would need 2,000 deep copies. The problem is not only how we copy but that we copy at all when most data is identical.
7Conclusion
In this answer, we saw that tenants seeing each other's changes is the signature of shallow copies sharing mutable nested objects. The fix is to make the template immutable and share one instance, store only per-tenant overrides, and resolve values by checking overrides first and the shared template second. Where a full independent copy is needed, an explicit deep copy method with an independence test does the job. Sharing an immutable base cuts memory from gigabytes to a few hundred megabytes and removes the cross-tenant bug at the same time.
Open with
The fact that tenants see each other's changes tells me the per-tenant copies are shallow: the outer object is new, but the nested maps and rule objects are shared with the template, so an override on one tenant mutates the rule everyone uses.
Then, if they want more
Making the copies deep would fix the bug but make memory far worse, so I would redesign instead. I would load the rule templates once into immutable objects, records and unmodifiable maps created with Map.copyOf, and share that single instance across all 2,000 tenants. Each tenant then holds only its overrides, typically a few dozen values.
A lookup checks the tenant's overrides first and falls back to the shared template. Because nothing can modify the template, no code path lets one tenant affect another, and memory drops from roughly one copy per tenant to one template plus small override maps. Reloading becomes an atomic swap of one reference.
If a tenant ever needs a fully independent copy, say for a draft price list, I would add an explicit deep copy method and a test that changes the copy and asserts the original is unchanged.
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 pricing engine loads expensive rule templates from files at startup and creates a...": https://scrivio-interview-library.netlify.app/java/creational-patterns/1be779bf3c/