Quick revision
The short answer
The rule I would set is that application code uses Spring's cache abstraction or JCache for ordinary read-through caching, so business code never names a provider. Provider APIs are allowed only where a feature the abstraction lacks (atomic counters, sorted sets, locks, pub/sub) justifies the coupling, and then only behind a small interface the team owns. All cache configuration lives in a shared starter. The limit is that an abstraction hides the API, not the semantics: serialization, TTL support, null handling and consistency still differ, so every provider change still needs testing, and the rule itself is reviewed at each major framework or provider upgrade.
What to remember
In this answer, we set a long-term rule for caching code: use Spring's cache abstraction or JCache by default, allow provider APIs only for features the abstraction lacks and only behind a small owned interface, and keep all provider configuration in a shared starter. We also stated the limit honestly: the abstraction hides the API but not semantics such as serialization, TTL support, null handling and consistency, so every migration still needs testing. The rule itself is reviewed at major framework, provider and licence changes so it keeps reducing the cost it was made to reduce.
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 rule I would set is that application code uses Spring's cache abstraction or JCache for ordinary read-through caching, so business code never names a provider. Provider APIs are allowed only where a feature the abstraction lacks (atomic counters, sorted sets, locks, pub/sub) justifies the coupling, and then only behind a small interface the team owns. All cache configuration lives in a shared starter. The limit is that an abstraction hides the API, not the semantics: serialization, TTL support, null handling and consistency still differ, so every provider change still needs testing, and the rule itself is reviewed at each major framework or provider upgrade.
Why this question is asked
Every long-lived organisation collects caching code from several eras. Ten years ago someone used Ehcache 2 directly, then a team adopted Hazelcast for a distributed map, and later Redis clients appeared everywhere. Each provider change has meant hunting through hundreds of call sites. A leader at this level is expected to stop that cycle with a rule that is simple enough to follow and honest about where it stops working. Interviewers ask this to see whether we can separate what an abstraction really buys from what it only appears to buy. In this answer, we will see why provider coupling was expensive, set the default rule, define the exception and how to isolate it, look at the semantic differences no abstraction hides, and decide how the rule stays current.
1Why Provider Changes Kept Being Expensive
When code calls a provider API directly, the provider's types spread through the codebase: Ehcache Element objects, Hazelcast IMap instances, Redis template calls with hand-built keys. Each call site also encodes provider decisions, such as how keys are formatted, how values are serialized, and what happens when the cache is down. Migrating then means finding and rewriting every one of those call sites, and each rewrite is a chance to change behaviour by accident.
Real drivers for migration come regularly. Spring Boot 3, for example, dropped direct support for Ehcache 2 because it does not support the Jakarta namespace, which pushed teams toward Ehcache 3 through JCache. Licence changes, cost and operational effort have pushed others between Redis-compatible products. Over ten years we should expect several such moves, so the cost of each one is what the rule must reduce.
2The Default Rule: Code Against the Abstraction
For simple read-through caching, which is the large majority of cases, application code uses @Cacheable, @CachePut and @CacheEvict, or the Cache and CacheManager interfaces when it needs programmatic access. JCache (JSR-107) annotations and API are an acceptable alternative where a team prefers a standard. Business code then names caches, never providers.
With the first style, moving from Hazelcast to Redis is a change of dependency and configuration in one place, plus testing. With the second, it is a search through the codebase.
3The Exception: Provider Features Behind a Small Interface
Some needs are not caching in the read-through sense. A rate limiter needs an atomic increment with expiry, a leaderboard needs a sorted set, a scheduler needs a distributed lock. Spring's abstraction has none of these, and pretending otherwise leads to slow and incorrect code. So the rule allows the provider API, with one condition: the coupling is isolated behind an interface named after the business need, with exactly one implementation that knows the provider.
- Business codeannotations, Cache,
CacheManager, or owned interfaces likeRequestQuota - Owned adaptersone class per provider-specific feature
- Shared caching starter
CacheManager, serializers, prefixes, TTLs, error handling - ProviderRedis, Caffeine, Hazelcast, a JCache implementation
4 layers, from the top down. Step through them one at a time.
Only the bottom two layers change when the provider changes.
A migration then touches one class per feature, and the interface gives us a seam for tests. The exception is approved in design review, so a provider API does not reappear in business code just because it was convenient.
4What the Abstraction Does Not Hide
The honest limit of the rule is that the abstraction hides the API, not the behaviour. A @Cacheable method that worked on one provider can behave differently on another. Serialization is the biggest one: an in-process cache stores the object itself, so mutating a returned object changes the cached copy, while Redis stores bytes, so the type must be serializable and every read creates a new object. TTL support differs too: ConcurrentMapCache has none, Caffeine expires locally, Redis expires on the server. Null handling, the support for sync = true, the timing of eviction (Caffeine evicts asynchronously) and consistency across instances all vary.
So the rule comes with a requirement: every provider migration is treated as a behaviour change and tested as one, with tests for serialization round trips, TTL expiry, nulls and failure handling, and a staged rollout that compares hit ratios and error rates before and after.
| Behaviour | In-process (Caffeine) | Remote (Redis) |
|---|---|---|
| What is stored | the object reference | serialized bytes |
| Mutating a returned value | changes the cached copy | has no effect on the cache |
| TTL | per JVM, local clock | on the server, shared |
| Visibility of an eviction | this instance only | all instances |
| Failure mode | heap pressure | network errors and timeouts |
5Keeping Configuration Central and the Rule Current
The rule only pays off if provider choice is made in one place. A shared starter owns the CacheManager setup, serializers, key prefixes, default TTLs, the CacheErrorHandler and metrics, so a provider change is a starter release followed by service upgrades, not thirty independent projects.
Finally, the rule is reviewed rather than frozen. A major Spring Framework or Spring Boot release, a provider's major version, or a licence change is a natural review point. The review asks whether the abstraction still covers our common cases, whether the list of approved exceptions has grown, and whether the cost of a migration has actually dropped. If it has not, the rule is not working and we need to know why.
6Follow-up questions
Is JCache better than Spring's abstraction as the standard?
It is a standard API with several implementations, which is attractive, and Spring supports its annotations. But Spring's own annotations are what most Spring teams already use, they integrate with SpEL keys and conditions, and Spring Boot can back them with JCache anyway. I would allow both and require one per service, so the choice is about team habit, not about provider coupling.
How do you stop the exception from becoming the norm?
By making it visible and slightly costly. Provider APIs are only allowed in adapter packages, an architecture test (for example with ArchUnit) fails the build when business packages import provider types, and each adapter is listed with its owner. If the list grows quickly, that is a sign the abstraction is missing something common, and we add it to the starter.
Does this rule make caching slower?
Not in any way that matters for typical read-through caching. The proxy and interceptor add microseconds, while a Redis round trip costs around a millisecond. Where nanoseconds matter, for example a hot in-process lookup in a tight loop, the team can use a Caffeine instance behind an owned interface, which is exactly what the exception is for.
7Conclusion
In this answer, we set a long-term rule for caching code: use Spring's cache abstraction or JCache by default, allow provider APIs only for features the abstraction lacks and only behind a small owned interface, and keep all provider configuration in a shared starter. We also stated the limit honestly: the abstraction hides the API but not semantics such as serialization, TTL support, null handling and consistency, so every migration still needs testing. The rule itself is reviewed at major framework, provider and licence changes so it keeps reducing the cost it was made to reduce.
Open with
The pattern I see is that each provider change was expensive because provider types and decisions had spread into every call site. So my rule would be: for ordinary read-through caching, application code uses Spring's cache abstraction, the annotations or the Cache and CacheManager interfaces, or JCache, and never names a provider.
Then, if they want more
Where a team needs something the abstraction does not offer, such as atomic counters, sorted sets or locks, they may use the provider API, but only behind a small interface named after the business need, with one implementation class, approved in design review and enforced with an architecture test.
All cache configuration, serializers, prefixes, TTLs, error handling and metrics, lives in a shared starter, so a provider change is one release plus upgrades. I would be clear about the limits: the abstraction hides the API, not the semantics.
An in-process cache stores references while Redis stores bytes, TTL support and null handling differ, Caffeine evicts asynchronously, and eviction is local or global depending on the provider. So migrations are still behaviour changes and need serialization, expiry and failure tests and a staged rollout.
And I would review the rule whenever a major Spring Boot, provider or licence change comes, checking whether migration cost actually dropped.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3
- 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 "Over ten years your teams have written caching code against provider APIs directly...": https://scrivio-interview-library.netlify.app/databases/caching/f371a4bc33/