Quick revision
The short answer
Both methods use the cache named lookup, and the default key is only the argument, so byCode("USD") and byIso("USD") read and write the same entry. Whichever method runs first stores its object, and the other method then receives that object from the cache and fails with a ClassCastException when it is cast to its own return type. We fix it with a separate cache name per method, or a key that includes the method, add a service prefix when the cache is a shared Redis, and prevent it with a convention enforced by a starter or a test.
What to remember
In this answer, we saw that a cache entry is addressed only by cache name and key, and that the default key is just the argument, so byCode("USD") and byIso("USD") share an entry in the lookup cache. The method that runs first stores its object, and the other method fails with a ClassCastException when the proxy casts it, which only happens for overlapping values and depends on traffic order. We fixed it with a cache name per kind of data or a method-aware key, added service prefixes for shared Redis, and prevented it with naming conventions enforced by a starter check or a test.
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.
Both methods use the cache named lookup, and the default key is only the argument, so byCode("USD") and byIso("USD") read and write the same entry. Whichever method runs first stores its object, and the other method then receives that object from the cache and fails with a ClassCastException when it is cast to its own return type. We fix it with a separate cache name per method, or a key that includes the method, add a service prefix when the cache is a shared Redis, and prevent it with a convention enforced by a starter or a test.
Why this question is asked
A ClassCastException in code that never casts anything is a strong hint that something outside our code handed us the wrong object. Here, that something is the cache. The bug appears only "sometimes" because it depends on which method happens to cache a given code first, and only for values the two methods have in common, so it can pass every test and still fail in production. Interviewers ask this to see whether we know how the default cache key is built and what it leaves out. In this answer, we will recap how a cache entry is addressed, reproduce the collision, explain why it appears only sometimes, fix it in three ways, and finish with how to prevent it across teams.
1A Quick Recap: How a Cache Entry Is Addressed
A cached entry is found by two things only: the cache name and the key. The cache name selects one named store from the CacheManager, and the key selects the entry inside it. When we do not write a key attribute, Spring uses SimpleKeyGenerator: with a single argument, the key is the argument itself; with several, a SimpleKey of all of them; with none, the constant SimpleKey.EMPTY.
Notice what is not part of the address: the method name, the class name and the return type. Spring assumes that if two methods use the same cache, they mean the same data.
2Reproducing the Collision
The two methods live in different beans but share the CacheManager, so the name lookup returns the same Cache object to both.
5 messages in order. Play them, or step through them one at a time.
The cache did its job; the address was not unique.
The interceptor finds an entry under "USD" and returns it, exactly as designed. The proxy's generated method then has to return it as a Currency, and the cast fails at the call site.
3Why It Happens Only Sometimes
The collision needs two conditions at once. The same string must reach both methods, for example a caller that passes "USD" to byCode as well as to byIso, or code lists that happen to share some values, and the collision happens for those keys only. And the other method must have cached that key first, which depends on traffic order and on when entries expire. After a deployment the cache starts empty, so the first callers decide which type sits under each shared key, and the failures look random.
With a local cache such as Caffeine, each instance fills in its own order, so one instance may fail for a key while another does not. That is why the bug often survives tests: a test that calls only one of the methods never sees it.
4Fixing the Collision
The simplest and best fix is a separate cache name per kind of data. Countries and currencies are different data and deserve different caches, which also lets each have its own TTL and size.
Notice that target here is the real bean, not the proxy, so its class name is stable. If several subclasses inherit one cached method, we should decide whether they may share entries and choose between target.getClass() and method.getDeclaringClass() on purpose. A third option is a SpEL key that adds a literal prefix, such as key = "'currency:' + #iso", which is explicit but easy to forget on the next method.
When the cache is a Redis instance shared by several services, there is one more layer to the address. RedisCacheManager prefixes keys with the cache name followed by two colons by default, so two services that both use a cache called lookup write to the same Redis keys. Each service needs its own prefix, for example with RedisCacheConfiguration.prefixCacheNameWith("currency-service:") or a computePrefixWith function.
5Preventing It Across Teams
A convention prevents most cases: cache names are named after the data they hold, such as currencies, never generic names like lookup or cache. A shared caching starter can enforce it. At startup it can walk the cache operations Spring discovered and fail, or warn, when one cache name is used by methods with different return types. A cheaper option is a test in each service that scans its own beans for @Cacheable and asserts that each cache name maps to one return type.
For shared Redis, the starter sets the key prefix from spring.application.name, so a service cannot forget it. Together these turn a production ClassCastException into a startup error or a failed build.
6Follow-up questions
What does SimpleKey.EMPTY mean for zero-argument methods sharing a cache?
Every call to a method with no arguments uses the same constant key, SimpleKey.EMPTY. So if two zero-argument methods share a cache name, for example @Cacheable("config") Settings settings() and @Cacheable("config") Flags flags(), they do not collide sometimes, they collide always: whichever runs first owns the only entry, and the other method fails on every call afterwards. Give each its own cache name or an explicit key such as key = "'settings'".
Why does the exception appear at the caller and not in the cache?
The cache stores and returns plain objects and does not know about return types. The interceptor hands back whatever it found as an Object, and the proxy method then returns it as the declared type, which inserts a cast. The cast is where the mismatch is finally noticed, which is far from the place where the wrong entry was written.
Would generics or a common interface avoid the cast?
They would only hide it. If both types implemented a common interface and callers used it, the code might not throw, but byIso("USD") would quietly return a Country. A wrong value without an exception is worse than the ClassCastException, so the fix is a unique address, not a looser type.
7Conclusion
In this answer, we saw that a cache entry is addressed only by cache name and key, and that the default key is just the argument, so byCode("USD") and byIso("USD") share an entry in the lookup cache. The method that runs first stores its object, and the other method fails with a ClassCastException when the proxy casts it, which only happens for overlapping values and depends on traffic order. We fixed it with a cache name per kind of data or a method-aware key, added service prefixes for shared Redis, and prevented it with naming conventions enforced by a starter check or a test.
Open with
The cause is that both methods use the cache called lookup, and with the default SimpleKeyGenerator the key is just the single argument. The method name, the class and the return type are not part of the key.
Then, if they want more
So byCode("USD") and byIso("USD") address the same entry. If byCode runs first, a Country is stored under "USD", and when byIso is called with "USD" the interceptor finds an entry, returns it, and the proxy's cast to Currency throws a ClassCastException.
It only happens sometimes because it needs a value valid for both methods and depends on which one cached that key first after a deployment or expiry, and with local caches each instance fills in its own order. The fix I prefer is separate cache names per kind of data, countries and currencies, which also allows different TTLs.
If the cache really must be shared, a custom KeyGenerator that includes the declaring class and method name, or an explicit prefix in the key expression, makes the key unique. If it is a shared Redis across services, the cache-name prefix also needs the service name.
To prevent it, I would name caches after their data and have a starter or a test check at startup that each cache name maps to one return type, and that the Redis prefix includes the application name.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Cache abstraction
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 "Two methods in different services share one cache name: @Cacheable("lookup") Country...": https://scrivio-interview-library.netlify.app/databases/caching/8db7db225a/