Hibernate Second-Level Cache Explained

A Hibernate second-level cache is one of the data caching components available in the Hibernate object-relational mapping (ORM) library. Hibernate is a popular ORM library for the Java language, and it lets you store your Java object data in a relational database management system (RDBMS).

Since Java objects do not naturally align with the tables/rows/columns of an RDBMS (a phenomenon known as “impedance mismatch”), Hibernate acts as the translation layer. With Hibernate, you can easily and quickly access relational data by providing mapping mechanisms from relational database tables, rows, columns, and foreign keys to your annotated Java classes. Hibernate generates SQL queries with an easy-to-use syntax to query data based on the Java object-oriented domain model.

Hibernate Second Level Cache Diagram
Hibernate Second Level Cache Diagram

The second-level cache is enabled by plugging in a third-party caching technology to expand the built-in caching capabilities in Hibernate. It is used to overcome the limitations of the first-level cache, described below.

What Are the Hibernate Caching Levels?

Caching is one of the strengths of the Hibernate framework, and it is available at multiple levels.

The first-level cache is the first place that Hibernate checks for cached data. It is built in and active by default to reduce the number of SQL queries directly to the database. If the requested query results are not in the first-level cache, then the query is run against the underlying database (that is, if there is no second-level cache enabled). This cache only works at a session level, meaning each session object caches data independently, so there is no sharing of cached data across sessions, and the cached data is deleted when the session closes. This makes the cache only useful for repeated queries in the same session. For repeated queries across multiple sessions, that’s where the second-level cache comes into play.

With the Hibernate second-level cache, you can plug in a caching technology to complement the first-level cache. If the requested query results are not in the first-level cache, then the second-level cache is checked. The second-level cache shares cached data across sessions, so all sessions/users can benefit from the cached data, even for data that was inserted by another session, and even if the session that inserted the data into the second-level cache closes.

Why Is a Second-Level Cache Important for Hibernate?

A second-level cache improves application performance with regard to persistence for all sessions created with the same session factory. With a second-level cache, a request for an object can be served by the cache, even if the request is executed from multiple sessions, with much lower latency than if the request went to the database. Considering that ORM queries can be complex, and therefore relatively slow, a system can greatly benefit from a second-level cache to reduce the repetition of the same complex/slow queries to the underlying database.

This is especially useful for high-volume web applications that have high commonality across users in terms of SQL queries to the database. And with highly scalable caches, you can cache more data and realize more performance acceleration.

What Can Be Used as a Hibernate Second-Level Cache?

Any technology that supports out-of-the-box integration with Hibernate can be plugged in to act as a second-level cache. In-memory data grids are commonly used as a Hibernate second-level cache. For example, Hazelcast IMDG has built-in integration with Hibernate to easily plug in for second-level caching. With its distributed architecture, you can add more nodes to the cluster to grow the cache as much as you want to handle heavier workloads.

Keep Reading