Quick revision
The short answer
Ehcache 2 does not work with Spring Boot 3 or Hibernate 6, so every cache must move to Ehcache 3 through JCache (the closest feature set, including disk tiers) or to Caffeine (simpler and fast, heap only), and Hibernate's second-level cache must switch to the jcache region factory with its regions defined in the new configuration. Along the way we should question whether disk persistence is still useful in containers, translate each setting carefully, and compare hit rates and memory before and after with Micrometer metrics.
What to remember
In this answer, we saw that Ehcache 2 is unsupported in Spring Boot 3 and Hibernate 6, so we choose Ehcache 3 through JCache or Caffeine for each cache, switch Hibernate's second-level cache to the jcache region factory with its regions mapped by name, question disk persistence in containers, translate TTL, TTI and size settings deliberately, and compare Micrometer cache metrics and behaviour tests before and after the release.
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.
Ehcache 2 does not work with Spring Boot 3 or Hibernate 6, so every cache must move to Ehcache 3 through JCache (the closest feature set, including disk tiers) or to Caffeine (simpler and fast, heap only), and Hibernate's second-level cache must switch to the jcache region factory with its regions defined in the new configuration. Along the way we should question whether disk persistence is still useful in containers, translate each setting carefully, and compare hit rates and memory before and after with Micrometer metrics.
Why this question is asked
A Spring Boot 3 upgrade looks like a version bump, but for applications built on Ehcache 2 it is really a cache migration hidden inside it. The service caches, the Hibernate entity cache and the disk-persisted caches all depend on a library that the new stack no longer supports. Interviewers give this scenario to see whether we can plan the work, make the decisions explicitly instead of copying old settings, and prove afterwards that nothing got slower or less correct. In this answer, we will see why the migration is forced, choose a replacement, move the Hibernate cache, decide what to do about disk persistence, translate the settings, and verify the result.
1Why the Migration Is Forced
Ehcache 2 is the net.sf.ehcache library. Spring Framework 6 removed its EhCacheCacheManager support, Spring Boot 3 no longer auto-configures it, and Hibernate 6 dropped the hibernate-ehcache module that connected it to the second-level cache. Spring Boot 3 also moved to the jakarta.* namespaces. So even if we kept the old jar on the classpath, nothing in the new stack would talk to it.
That gives us a clear list of things to change: the provider for the Spring service caches (@Cacheable and friends), the provider for Hibernate's second-level cache, which caches entities between sessions, and any code that called the Ehcache 2 API directly, for example to read statistics or clear caches from an admin endpoint.
2Choosing the Replacement
We have two realistic local options. Ehcache 3, used through JCache, has the closest feature set: heap, off-heap and disk tiers, expiry, and XML configuration. Caffeine is smaller and very fast, but holds entries on the heap only. For a distributed need, Redis is a third option.
| Option | Good for | Costs |
|---|---|---|
| Ehcache 3 via JCache | off-heap or disk tiers, one provider for Hibernate too | more configuration, new XML format |
| Caffeine | simple fast heap caches, Spring Boot property config | heap only, no persistence |
| Redis | caches shared across instances, survives restarts | network hop, serialization, ops work |
3Moving Hibernate's Second-Level Cache
Hibernate 6 talks to cache providers through JCache. We add the hibernate-jcache module and a JCache provider such as Ehcache 3, and set the region factory to jcache. Each entity or collection marked with @Cache becomes a region, a named cache, and those regions need entries in the new provider configuration.
Notice the region name. If the old ehcache.xml had a cache called com.example.Product and the new configuration uses product, Hibernate silently creates a default cache or fails to start, depending on its missing-cache setting (check your version for the property name). So we list every region the old configuration had and map each one deliberately.
4Deciding About Disk Persistence
Some caches in the old system were persisted to disk so that they survived restarts. Before translating that, we ask whether it still makes sense. In Kubernetes or most container platforms, a restarted pod usually gets a fresh filesystem, so a disk cache is lost on every deploy anyway, and a persistent volume per pod adds operational work for a cache.
If the warm start really matters, for example because a cold cache overloads the database, a shared cache such as Redis survives pod restarts and serves all instances, which is usually a better fit. If the persistence was just a historical default, we drop it. Only when a single long-lived instance with a local disk really benefits do we keep a disk tier in Ehcache 3.
5Translating the Settings
The old and new formats use different names and sometimes different meanings, so we translate each setting on purpose instead of copying numbers. In Ehcache 2, timeToLiveSeconds meant expire a fixed time after creation, and timeToIdleSeconds meant expire after a period with no access. In Ehcache 3 these become an expiry with ttl or tti; in Caffeine they are expireAfterWrite and expireAfterAccess. maxEntriesLocalHeap becomes a heap resource in entries in Ehcache 3, or maximumSize in Caffeine.
But there is a catch. The old cache combined a TTL and a TTI, while Ehcache 3's XML gives one expiry per cache unless we write a custom expiry policy. So we decide which behaviour the business needs. The eviction algorithm also differs: Caffeine's admission policy may keep a different set of entries than Ehcache 2's LRU did at the same size, which can change hit rates.
6Verifying the Result
We prove the migration with numbers. Before the upgrade, we record hit ratio, size, evictions and heap use per cache, plus database query rates. Micrometer exposes cache metrics for Caffeine and JCache caches when they are registered through Spring's cache manager. After the upgrade, in a load test and then in production, we compare the same numbers. A cache whose hit ratio fell from 90 to 60 percent has probably lost its TTI or got a size that is too small.
We also test behaviour, not just speed: a test that updates an entity and checks the second-level cache returns the new value, a test where a transaction rolls back and the cache does not keep its changes, and a test of admin endpoints that clear caches. Finally, the migration goes out behind a normal canary, with the old release kept ready to roll back.
- List every cache and regionnames, settings, direct API use
- Choose provider per cacheEhcache 3, Caffeine or Redis
- Decide on disk persistencedrop, or move to a shared cache
- Translate settings one by oneTTL, TTI, sizes, region names
- Record baseline metricshit ratio, size, heap, DB load
- Load test and canarycompare, then roll out
6 steps in order. Play them, or step through them one at a time.
7Follow-up questions
What are the risks of Hibernate second-level caching in a cluster?
Each instance has its own local second-level cache, so when one instance updates an entity, the others keep their old copies until they expire, and users may see different data depending on which instance serves them. Updates made outside Hibernate, such as SQL scripts or other services, are never seen at all. So in a cluster we either cache only data that rarely changes, use a clustered or distributed provider, or accept a short expiry as the staleness bound.
Can we keep Ehcache 2 by staying on old Hibernate and Spring versions?
Only by staying on unsupported versions of Spring Boot 2 and Hibernate 5, which means no security fixes. That is a short-term bridge at best, not a plan.
Why not just move every cache to Redis?
Because a network hop and serialization cost far more than a heap lookup, and some caches, such as small lookup tables, gain nothing from being shared. Redis is the right answer for caches that must be shared or survive restarts, not for every cache.
8Conclusion
In this answer, we saw that Ehcache 2 is unsupported in Spring Boot 3 and Hibernate 6, so we choose Ehcache 3 through JCache or Caffeine for each cache, switch Hibernate's second-level cache to the jcache region factory with its regions mapped by name, question disk persistence in containers, translate TTL, TTI and size settings deliberately, and compare Micrometer cache metrics and behaviour tests before and after the release.
Open with
First I would list every cache: the Spring service caches, the Hibernate regions, the ones with disk persistence, and any code that uses the Ehcache 2 API directly.
Then, if they want more
Ehcache 2 is not supported by Spring Boot 3 or Hibernate 6, so all of them need a new provider. For each cache I would choose Ehcache 3 through JCache when I need off-heap or disk tiers or want one provider for Hibernate, Caffeine when it is a simple heap cache, and Redis when instances must share data.
For Hibernate I would add hibernate-jcache, set the region factory to jcache and map every old region name to the new configuration, because a missing region is easy to overlook.
For disk persistence I would ask whether it still helps, since containers usually lose their disk on restart; if a warm start really matters, a shared Redis cache is often better. Then I would translate settings one by one: timeToLive and timeToIdle become expiry policies, maxEntriesLocalHeap becomes a heap size, noting that Ehcache 3 XML takes one expiry per cache.
Finally I would record hit ratio, size, heap and database load before and after with Micrometer, add tests for updates and rollbacks, and release behind a canary.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3, Hibernate 6, Hibernate 5, Spring Boot 2
- 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 "An older Spring application uses Ehcache 2 for its service caches and Hibernate...": https://scrivio-interview-library.netlify.app/databases/caching/995595945c/