Quick revision
The short answer
A local cache such as Caffeine returns references to the very objects it stores, so when the discount code changes a returned PriceLine, it changes the cached copy that every later request reads. A Redis cache hides this because each read deserializes a new copy. We fix it by never mutating cached values, applying discounts to new objects, by making cached types immutable (records with List.copyOf), by storing unmodifiable copies, and by a test that changes a returned value and checks that a later get is unaffected.
What to remember
In this answer, we saw that a local cache returns references to its stored objects, so changing a returned PriceLine changed the cached copy for every later request, while Redis hides the problem by deserializing a fresh copy each time. The restart cleared the cache, which is why the symptom vanished, and history-dependent behaviour is the hint of shared mutable state. We fix it by never mutating cached values, by making cached types immutable records with List.copyOf, by storing unmodifiable copies where types cannot change, and by a test that a mutation does not affect a later get.
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.
A local cache such as Caffeine returns references to the very objects it stores, so when the discount code changes a returned PriceLine, it changes the cached copy that every later request reads. A Redis cache hides this because each read deserializes a new copy. We fix it by never mutating cached values, applying discounts to new objects, by making cached types immutable (records with List.copyOf), by storing unmodifiable copies, and by a test that changes a returned value and checks that a later get is unaffected.
Why this question is asked
Bugs that depend on what earlier requests did are some of the hardest to reproduce, and they vanish on restart, which tempts teams to blame the environment. Interviewers ask this one because the symptom, discounts leaking to customers who are not eligible and disappearing after a restart, points straight at shared mutable state, and the fix teaches a rule about local caches that every Java developer should know. In this answer, we will see how a local cache returns objects, reproduce the bug, explain why it vanished on restart, and then go through three fixes and a test that guards them.
1What a Local Cache Actually Returns
A local cache keeps Java objects on the heap of our own process. Caffeine stores a reference to the object we put in, and cache.get returns that same reference. It does not copy anything, which is exactly why a local cache is so fast: a hit costs a hash lookup and nothing else.
A remote cache such as Redis works differently. The value is stored as bytes, and every read deserializes those bytes into brand-new objects. Two requests reading the same key get two independent copies. So code that modifies a value it got from Redis only changes its own copy, and the bug we are about to see cannot happen there, which is why it often appears only after a team moves from Redis to a local cache, or adds a local cache in front of Redis.
2Reproducing the Bug
The discount feature loads the price lines for a product from the cache and then applies the customer's discount to them before rendering.
6 messages in order. Play them, or step through them one at a time.
The first eligible customer's discount was written into the cached objects, because lines is the cached list and each PriceLine in it is the cached object. Every later request, eligible or not, starts from the discounted price, and eligible customers get the discount again on top. Nothing was wrong with the cache; the code treated a shared object as if it owned it.
3Why a Restart Made It Disappear
A restart empties the in-process cache, so the next request loads fresh objects from the database, with correct prices. The corruption builds up again only as eligible customers visit each product. So the symptom depends on which customers came before, and it heals on restart. That combination, behaviour depending on request history and resetting with the process, is a strong hint of shared mutable state, and the local cache is the first place to look.
4Fix 1: Never Mutate Cached Values
The direct fix is to treat everything that comes out of a cache as read-only and build new objects for request-specific changes.
This fixes the bug, but it relies on every developer remembering the rule in every place that reads from the cache. The next feature can make the same mistake. So we want the types themselves to make mutation impossible.
5Fix 2: Make Cached Types Immutable
A Java record has final fields and no setters, so its state cannot be changed after construction. If the record holds a collection, the compact constructor should copy it with List.copyOf, which also makes it unmodifiable, so even the list inside cannot be changed through the record.
Now the original bug cannot compile, because there is no setAmount, and a mutation of the list fails loudly at the line that tried it, instead of silently corrupting other customers' prices. BigDecimal and String are immutable themselves, so the whole object graph is safe to share.
6Fix 3: Store Copies and Test the Rule
When the cached type cannot be changed, for example a class from a library, we can store an unmodifiable copy when putting the value, such as Collections.unmodifiableList over copied elements, so at least the structure cannot change. Some cache abstractions offer store-by-value, copying on every read and write, but it costs the speed that made the local cache attractive, so immutable types are usually the better tool.
Finally, a test makes the rule permanent: get a value, try to change it or change a copy derived from it, get the same key again, and assert that the second result still has the original price. That test fails on the original code and passes on every fix above, and it keeps failing for the next developer who introduces a mutable cached type.
7Follow-up questions
Why might the bug never appear in unit tests?
Because it needs two requests sharing one cache, in a particular order, with customers of different eligibility. Unit tests usually check one call with a fresh cache, a mocked cache, or caching disabled, so the second request never sees the first one's changes. Integration tests often run each case in a fresh context. The test that catches it calls the service twice against a real cache: first for an eligible customer, then for one who is not.
Would switching to Redis fix the bug?
It would hide it, because every read deserializes a new copy, but it would leave the code wrong. The moment someone adds a local near cache in front of Redis for speed, the bug returns. The fix belongs in the code and the types, not in the choice of cache.
Do immutable cached objects cost more memory?
Each change creates a new object, but cached values are read far more often than they are changed, and the original stays shared. In practice immutable values often save memory, because many readers can share one instance safely without defensive copies.
8Conclusion
In this answer, we saw that a local cache returns references to its stored objects, so changing a returned PriceLine changed the cached copy for every later request, while Redis hides the problem by deserializing a fresh copy each time. The restart cleared the cache, which is why the symptom vanished, and history-dependent behaviour is the hint of shared mutable state. We fix it by never mutating cached values, by making cached types immutable records with List.copyOf, by storing unmodifiable copies where types cannot change, and by a test that a mutation does not affect a later get.
Open with
The bug is that the discount code changes objects that belong to the cache. Caffeine stores references, and get returns the same PriceLine objects to every caller. So when an eligible customer's request multiplies the amounts by 0.9, it changes the cached objects, and the next customer, eligible or not, sees the discounted price; eligible customers even get it twice.
Then, if they want more
A restart clears the in-process cache, which is why it went away, and behaviour that depends on earlier requests is exactly the sign of shared mutable state. With Redis this would not show up, because each read deserializes a new copy, but that only hides the mistake.
To fix it I would first never mutate cached values: build new PriceLine objects with the discount for that request. Then I would make the cached types immutable, records with final fields and List.copyOf in the constructor, so a setter does not exist and changing the list throws UnsupportedOperationException instead of silently corrupting the cache.
Where a type cannot be changed, I would store unmodifiable copies. And I would add a test that gets a value, applies a discount, gets it again and checks the original price, which catches the next developer who adds a mutable cached type.
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 "The discount code changes the PriceLine objects returned by the cache and then renders...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/fccfb54757/