Quick revision
The short answer
The most likely cause is that each of the 4 instances has its own local cache: an update through instance A refreshes A's cache, but B, C and D keep serving the old entry until it expires. The second cause is writes Hibernate never sees, from native SQL, other applications or database scripts, which no instance can invalidate. We fix it with a clustered or invalidating cache, or by caching only read-mostly reference data with short TTLs, and we check the hit ratio, because a cache with few hits adds this risk for no benefit.
What to remember
In this answer, we saw two causes of minutes-long stale data after enabling the second-level cache on 4 instances. Local caches are independent, so an update through one instance leaves the others serving old entries until they expire, and writes from native SQL, other applications or scripts are invisible to every cache, while bulk JPQL updates only invalidate regions locally. We fixed it with a clustered invalidating cache, by caching only read-mostly reference data with short TTLs, or by caching at the service boundary, and we used per-region hit ratios to decide whether to keep a cache at all.
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 most likely cause is that each of the 4 instances has its own local cache: an update through instance A refreshes A's cache, but B, C and D keep serving the old entry until it expires. The second cause is writes Hibernate never sees, from native SQL, other applications or database scripts, which no instance can invalidate. We fix it with a clustered or invalidating cache, or by caching only read-mostly reference data with short TTLs, and we check the hit ratio, because a cache with few hits adds this risk for no benefit.
Background to this question
Stale data that lasts for minutes and only appears sometimes is the classic sign of a cache that is correct on one machine and wrong on many. This question is popular because the second-level cache works perfectly in development, where there is one instance and one writer, and the bug only appears once the service is scaled out. In this answer, we will recap where the second-level cache lives, reproduce the problem with two instances, look at the writes Hibernate cannot see, explain how bulk updates behave, and then go through the fixes and the measurement that tells us whether to keep the cache at all.
1A Quick Recap of Where the Cache Lives
The Hibernate second-level cache stores entity state by id so that later lookups can skip the database. Hibernate does not store the data itself; a provider does. The default choice for many teams is Ehcache 3 through JCache, configured as a local cache: a map in the heap of each JVM.
Local means exactly that. With 4 instances behind a load balancer, there are 4 independent caches. Hibernate's concurrency strategies, even READ_WRITE with its soft locks, only coordinate the writers and readers that use the same cache. They know nothing about the other three.
2Reproducing It With Two Instances
Let us follow one product price through two instances. Both have cached product 7 at 10.00, with a time to live of 10 minutes.
6 messages in order. Play them, or step through them one at a time.
Instance B's cache is never told about the change made through instance A.
Notice that nothing went wrong on A. Its database update and cache update were correct. B simply has no way to learn about the change, so whether a user sees the new price depends on which instance the load balancer picks. That is why the symptom is sometimes and for minutes: it lasts until B's entry expires or is evicted.
3Writes Hibernate Never Sees
Even with one instance, the cache only knows about changes that go through Hibernate's entity operations. A native SQL update in the same application, a JdbcTemplate call, a nightly batch job written in another language, a second service sharing the table, or a DBA fixing data by hand all change rows behind the cache's back. The cache keeps serving the old state until the entry expires.
Bulk JPQL statements are a middle case. An update ... set ... where statement through Hibernate does not load the affected entities, so Hibernate cannot update their cache entries individually. Instead it invalidates the whole entity region for that type, which is safe but empties the cache on every bulk update. Native queries run through Hibernate invalidate even more broadly unless we declare which entities they affect (check your version for how). But writes from outside the application cause no invalidation at all.
| Write path | Effect on other instances | Effect on this instance |
|---|---|---|
| Entity update through Hibernate | none with a local cache | entry updated |
| Bulk JPQL update or delete | none with a local cache | whole region invalidated |
| Native SQL through Hibernate | none with a local cache | regions invalidated broadly |
JdbcTemplate, scripts, other services | none | none: stale until expiry |
4Fixing It
There are three main options, and the right one depends on the data. The first is a cache that works across instances: Infinispan in clustered invalidation mode, for example, sends an invalidation message to the other nodes when an entity changes, so their next read goes to the database. Replicated or distributed caches go further but add network cost and their own consistency trade-offs. This works only for writes that go through Hibernate on one of the clustered instances.
The second option is to narrow what we cache. Reference data that changes a few times a year, such as countries, currencies or product categories, can be cached locally with a short time to live, for example 5 minutes, if the business accepts that a change appears within that window. Frequently changing entities such as prices, stock or orders should not be in a local second-level cache at all.
The third option is to stop caching the entity and cache at a higher level where we control invalidation, for example a Spring Cache in Redis at the service boundary, evicted explicitly by every write path, including the batch job.
5Measuring Whether the Cache Pays
Before tuning the cache, we should check whether it earns its place. Hibernate statistics report second-level cache hits, misses and puts per region, and Micrometer can publish them. A region with a hit ratio of 20 percent saves one query in five while creating all the staleness risk above; removing it often costs almost nothing in latency.
So the investigation ends with a small table: for each cached region, the hit ratio, how often the data changes and who writes it. That table usually makes the decision obvious.
6Follow-up questions
Would READ_WRITE instead of NONSTRICT_READ_WRITE fix it?
No. The strategy controls consistency within one cache. With 4 local caches, each one is internally consistent and still unaware of the others' updates. The fix is in the topology or the scope of caching, not the strategy.
Could we evict entries manually after native updates?
Yes, through the Cache API: entityManagerFactory.getCache().evict(Product.class, id) or evict(Product.class) for the whole type. That only clears the local instance, though, so with local caches the eviction must be broadcast to every instance, which is what a clustered cache does for us.
How do you find which write path caused a stale entry?
Compare the row's last update, for example an updated_at column or audit log, with the application's own update logs. If there is no matching Hibernate update, the write came from outside, such as a batch job or another service.
7Conclusion
In this answer, we saw two causes of minutes-long stale data after enabling the second-level cache on 4 instances. Local caches are independent, so an update through one instance leaves the others serving old entries until they expire, and writes from native SQL, other applications or scripts are invisible to every cache, while bulk JPQL updates only invalidate regions locally. We fixed it with a clustered invalidating cache, by caching only read-mostly reference data with short TTLs, or by caching at the service boundary, and we used per-region hit ratios to decide whether to keep a cache at all.
Open with
With four instances, the first thing I would check is the cache topology. If the second-level cache is a local Ehcache in each JVM, there are four independent caches.
Then, if they want more
When an update goes through instance A, Hibernate updates A's entry correctly, but B, C and D keep serving the old state until their entries expire, so users see stale data depending on which instance they hit, for as long as the TTL. The concurrency strategy does not help here; even READ_WRITE only coordinates within one cache.
The second cause is writes Hibernate never sees: native SQL, JdbcTemplate, batch jobs, other services sharing the table, manual fixes. Those invalidate nothing. Bulk JPQL updates invalidate the whole region, but only on the instance that ran them.
To fix it, I would either use a clustered cache with invalidation, such as Infinispan in invalidation mode, or restrict the second-level cache to read-mostly reference data with a short TTL the business accepts, and stop caching entities that change often, or move caching to the service boundary where every write path evicts explicitly.
And I would look at the hit ratio per region from Hibernate statistics, because a region with a low hit ratio adds staleness risk without saving much load, and should simply be removed.
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 Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "After enabling the second-level cache in a service with 4 instances, users sometimes see...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/95afe207e4/