What is the Hibernate second-level cache?

Hibernate is an open-source, lightweight object-relational mapping (ORM) Java framework. ORM tools such as Hibernate are intended to simplify and streamline basic operations for relational databases—for example, CRUD (create, read, update, and delete) operations. 

Part of Hibernate’s efficiency comes from the use of a component called the “second-level cache” or “L2 cache.” So what is the Hibernate second-level cache, and how should you use it?

What is the Hibernate second-level cache?

The Hibernate second-level cache is an L2 cache implemented for the Hibernate framework. In our article on cache memory, we discuss how there are multiple tiers of standard cache storage:

  • The L1 cache is typically smallest and closest to the CPU, which means it’s best for ultra-high-performance use cases.
  • The L2 cache is larger and slightly slower than the L1 cache. If an object is not in the L1 cache, the application next checks the L2 cache.
  • The L3 cache is optional; if it exists, it is slower than the L1 or L2 caches.

Hibernate In the next section, we’ll discuss the details of how the Hibernate L2 cache works.

How does the Hibernate second-level cache work?

When Hibernate checks for cached data, it first consults the L1 cache. The Hibernate first-level cache is associated with a Hibernate Session object, which represents the connection between a Java application and a SQL database. The Session object comes with its own L1 cache that is active for as long as the Session exists; in other words, data does not persist across sessions.

The Hibernate L2 cache is actually optional: it is disabled by default, but users can enable it in Hibernate’s configuration settings. If enabled, the Hibernate second-level cache is associated with a SessionFactory object; this construct enables data in the cache to survive across multiple sessions.

Hibernate also includes a third type of cache: query caching, used to store the results of database queries. Hibernate and other ORM frameworks help address a problem known as the “object-relational impedance mismatch,” in which Java objects don’t neatly correspond to the rows and columns of a relational database. Hibernate helps map between relational databases and Java objects, automatically generating SQL database queries.

If Hibernate users want to query the relational database against an object, the Hibernate framework can save these results using query caching. Queries should be cached if they are expensive, or if the query will be run multiple times within a specific time period.

Hibernate second-level caching with Redis

Redis is an open-source, in-memory data structure store that is used to implement NoSQL key-value databases and caches. While Redis has a lot of features and benefits, it doesn’t support the Java programming language out of the box. For this reason, many Redis Java developers are using third-party Redis Java clients such as Redisson.

The Redisson client offers full support for Hibernate caching, including Hibernate L2 caching. Redisson’s Hibernate L2 caching implementation includes the following strategies:

  • READ_ONLY: Objects do not change once inside the cache.
  • NONSTRICT_READ_WRITE: Objects are (eventually) modified if their corresponding database entry is modified.
  • READ_WRITE: Objects are immediately modified if their corresponding database entry is modified, ensuring strong consistency.
  • TRANSACTIONAL: Objects are modified using distributed XA transactions, guaranteeing data integrity.

Below is an example of how to configure Hibernate to enable the L2 cache and Redisson compatibility:

<!-- 2nd level cache activation -->
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />

<!-- Redisson YAML config (located in filesystem or classpath) -->
<property name="hibernate.cache.redisson.config" value="/redisson.yaml" />
<!-- Redisson JSON config (located in filesystem or classpath) -->
<property name="hibernate.cache.redisson.config" value="/redisson.json" />

To learn more about how to set up Hibernate L2 caching with Redis and Redisson, check out this tutorial.

Similar terms