Quick revision
The short answer
The second-level cache stores entity state by id in a cache shared by all sessions of the application, provided by a cache such as Ehcache 3 or Infinispan through JCache, so a later em.find() can skip the database. We enable it with hibernate.cache.use_second_level_cache and a region factory, and mark entities with @Cacheable and @Cache(usage = ...), where the strategy READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE or TRANSACTIONAL decides how consistent the cache stays under updates. The query cache is separate: it stores the ids returned by a query, is invalidated whenever any table in that query changes, and needs the entities themselves cached too, just like collection caches.
What to remember
In this answer, we saw that the second-level cache stores disassembled entity state by id in regions shared by all sessions, through a provider such as Ehcache 3 or Infinispan via JCache. We enable it with use_second_level_cache, a region factory and selective caching, and mark entities with @Cacheable and @Cache. The four strategies range from READ_ONLY to TRANSACTIONAL in how they keep the cache consistent on updates. The query cache stores only the ids a query returned and is invalidated whenever any table in the query changes, and collection caches store ids too, so both depend on the entities being cached.
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 second-level cache stores entity state by id in a cache shared by all sessions of the application, provided by a cache such as Ehcache 3 or Infinispan through JCache, so a later em.find() can skip the database. We enable it with hibernate.cache.use_second_level_cache and a region factory, and mark entities with @Cacheable and @Cache(usage = ...), where the strategy READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE or TRANSACTIONAL decides how consistent the cache stays under updates. The query cache is separate: it stores the ids returned by a query, is invalidated whenever any table in that query changes, and needs the entities themselves cached too, just like collection caches.
Why this question is asked
Caching inside the ORM sounds like free performance, and that is exactly why it gets misused. Teams switch it on, see fewer queries in one benchmark, and later chase stale data they cannot explain. Interviewers ask about it to see whether we know what is actually cached, at what granularity, and what each concurrency strategy promises. In this answer, we will recap the first-level cache, see how the second-level cache sits next to it, enable it in Spring Boot, compare the four strategies, and then look at the query cache and collection caches and how they depend on the entity cache.
1A Quick Recap of the First-Level Cache
Every EntityManager has a persistence context, which acts as a first-level cache. If we call em.find(Customer.class, 1) twice in the same transaction, the second call returns the same object without a query. But this cache lives only as long as the EntityManager, usually one transaction. The next request starts empty and queries again.
The second-level cache, often written L2 cache, is shared across all EntityManagers of the application, in other words across sessions and requests. Hibernate looks there after the first-level cache and before the database.
- In the persistence context?Yesreturn the same objectNocheck the second-level cache
- In the Customer region of the L2 cache?Yesbuild an entity from the cached stateNoselect from the database
- Put state into the L2 cacheon a database load, for cacheable entities
3 steps in order. Play them, or step through them one at a time.
2What Is Actually Stored
The L2 cache does not store our Java objects. It stores the entity's state in a disassembled form, roughly an array of column values keyed by entity name and id, in a region named after the entity. On a hit, Hibernate assembles a new entity instance from that state and puts it into the current persistence context. So each session still gets its own object, which avoids threads sharing a mutable entity.
Hibernate does not implement the cache storage itself. It talks to a provider through a region factory. With Hibernate 6 and later, the usual route is JCache, the standard Java caching API, with Ehcache 3 for a local cache in each JVM, or Infinispan for local or clustered caches.
3Enabling It in Spring Boot
We add the provider dependencies (hibernate-jcache and, for example, Ehcache 3), switch the cache on, and opt entities in one by one.
ENABLE_SELECTIVE means only entities marked @Cacheable are cached, which is what we want: caching should be a decision per entity. The provider property name still uses javax in current Hibernate versions; check your version, and configure sizes and expiry in the provider's own configuration file.
4The Four Concurrency Strategies
The strategy in @Cache(usage = ...) decides what happens when the cached data changes. READ_ONLY is for data that never changes after insert: it is the fastest and simplest, and Hibernate refuses updates to such entities. NONSTRICT_READ_WRITE updates or evicts the cache entry after the transaction commits, without locking, so for a short window another session can read the old value; it suits data that changes rarely where a brief staleness is acceptable.
READ_WRITE uses soft locks: when a transaction updates an entity, Hibernate marks the cache entry as locked, so concurrent readers go to the database instead of reading a value that is about to change, and the entry is replaced after commit. This gives read-committed behaviour for the cache, but only if every writer goes through Hibernate and the cache provider sees every lock. TRANSACTIONAL makes the cache take part in the transaction itself, which needs JTA and a transactional provider such as Infinispan; it is the strongest and the most complex.
| Strategy | On update | Use for |
|---|---|---|
READ_ONLY | updates rejected | data that never changes, such as countries |
NONSTRICT_READ_WRITE | entry evicted after commit, brief stale window | rarely changed data, staleness tolerated |
READ_WRITE | soft lock during the update, replaced after commit | read-mostly data that does change |
| TRANSACTIONAL | cache updated inside the JTA transaction | JTA setups with a transactional provider |
5How the Query Cache Differs
The query cache is a separate feature, switched on with hibernate.cache.use_query_cache=true and then enabled per query with a hint. It does not store entities. For a query and its parameter values, it stores the result as a list of ids (and scalar values, if the query returns columns). On a hit, Hibernate takes those ids and loads each entity, from the L2 entity cache if it is there, from the database if not.
That is why a query cache without the entity cache is often slower than no cache: a hit turns one query into one query per id. Hibernate 6.5 added a setting to store full entity data in the query cache instead (check your version), but the id layout remains common.
Invalidation is also coarse. Hibernate keeps an update timestamps region with the last change time of each table. Any insert, update or delete through Hibernate on a table makes every cached query result that uses that table stale. On a table with frequent writes, the query cache is invalidated almost constantly and only adds overhead.
6Collection Caches Store Ids Too
Associations have their own cache. If we put @Cache on a collection, for example the lines of an order, Hibernate caches the ids of the elements, not the elements themselves. So the element entity must be cacheable too; otherwise each cached collection hit loads every element from the database one by one, which is an N+1 problem created by the cache.
7Follow-up questions
Does the second-level cache help with JPQL queries like findByName?
Not by itself. The entity cache is keyed by id, so it serves find() and association loads by id. A JPQL query always goes to the database unless the query cache is enabled for it, and then only the ids are cached.
What happens with bulk updates or native SQL?
A bulk JPQL update or delete makes Hibernate invalidate the affected entity regions, often the whole region, because it cannot know which rows changed. A native query invalidates more broadly unless we tell Hibernate which entities it touches. Changes made outside the application are invisible to Hibernate and leave stale entries until they expire.
Is the L2 cache shared between application instances?
Only if the provider is clustered or distributed. A local Ehcache in each instance is independent, so an update on one instance does not invalidate the others, which is a common source of stale data.
8Conclusion
In this answer, we saw that the second-level cache stores disassembled entity state by id in regions shared by all sessions, through a provider such as Ehcache 3 or Infinispan via JCache. We enable it with use_second_level_cache, a region factory and selective caching, and mark entities with @Cacheable and @Cache. The four strategies range from READ_ONLY to TRANSACTIONAL in how they keep the cache consistent on updates. The query cache stores only the ids a query returned and is invalidated whenever any table in the query changes, and collection caches store ids too, so both depend on the entities being cached.
Open with
The second-level cache is a cache shared by all sessions of the application, sitting between the persistence context and the database. It stores the disassembled state of entities by id, per entity region, and Hibernate uses a provider for the storage, usually Ehcache 3 or Infinispan through JCache.
Then, if they want more
In Spring Boot I set hibernate.cache.use_second_level_cache to true, the region factory to jcache, the shared cache mode to ENABLE_SELECTIVE, and mark each entity with @Cacheable and Hibernate's @Cache with a concurrency strategy. READ_ONLY is for immutable data and rejects updates. NONSTRICT_READ_WRITE evicts after commit without locking, so there is a short window of stale reads.
READ_WRITE uses soft locks so readers bypass an entry while it is being updated, which gives read-committed behaviour as long as all writes go through Hibernate. TRANSACTIONAL needs JTA and a transactional provider.
The query cache is different: it caches the result of a specific query with its parameters as a list of ids, and it is invalidated whenever any table the query uses changes, so it only pays off for rarely changing tables, and the entities must be cached too or each hit loads them one by one. Collection caches work the same way, storing only element ids.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6
- 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 "How does the Hibernate second-level cache work, what are its concurrency strategies, and...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/d3087d0977/