Using Hibernate Second Level Cache with TayzGrid

Hibernate is one of the most used ORM solution for Java. It simplifies your database programming and enables you to develop persistent code following natural Object-oriented idioms. Hibernate is built to support high transaction environments and provides scalability at the application tier but it faces scalability bottlenecks in the database tier.

Knowing the issue, Hibernate provides a sophisticated caching mechanism. This helps applications cache frequently used data and take load off the database. In Hibernate, you get two kinds of caching:

  • First level cache
  • Second level cache

Hibernate First Level Cache

Hibernate has its own implementation of First Level Cache that is enabled by default (you cannot even disable it forcibly). This cache resides in memory and within the application process and the session scope. The primary purpose of this cache is to limit the number of SQL queries which Hibernate needs to execute within a given transaction and it does that very well. However, there are some limiting factors in this First Level Cache as mentioned below:

  • When running multiple applications in a web farm, caches are unable to sync with each other and therefore are unable to maintain data integrity.
  • The cache is within a Session Scope and gets destroyed when this scope goes away. And, since this happens frequently within Hibernate, the cache is created and destroyed over and over again which is costly.
  • Tomcat and Jetty worker processes recycle frequently. This causes the cache to be flushed and all the cached queries are lost. This results in these queries getting executed again against the database.

Hibernate Second Level Cache

In addition to the First Level Cache, Hibernate provides a pluggable Second Level Cache architecture. This allows you to plug in a third party second level cache provider simplify by modifying the configuration files. No coding is required.

Hibernate Second Level Cache is created in the Session Factory scope. This is different from the First Level Cache because it enables the second level cache objects to be available to all of the sessions within an application.

TayzGrid is an Open Source In-Memory Data Grid ready for production environments, which implements a Hibernate Second Level Cache provider. You can use it easily by plugging it in without any code changes. And, since TayzGrid lives outside the application scope, therefore the second level cache outlives the application lifetime. This allows you to keep the cache even when your application process restarts. Additionally, you can have multiple application processes in a multi-server web farm share a common second level cache.

Following configuration example shows you how easy it is to plug in TayzGrid to Hibernate:

     <property name="hibernate.cache.use_second_level_cache">
     <property name="hibernate.cache.region.factory_class">
    <property name="tayzgrid.application_id">
    <property name="hibernate.cache.use_query_cache">

  <application-config application-id="myapp"
      <region name="DefaultRegion"

Take a look at some benefits TayzGrid provides if you use it as Hibernate Second Level Cache,

  • Cache lives outside your application: This helps to keep cache and applications machine separate ensuring consistent access across multiple servers. There are no Data integrity issue as cache data is sharable between all the applications.
  • Scalable cache architecture: You can add new servers on the fly in a production environment. TayzGrid’s robust architecture distributes data at runtime to accommodate more traffic on high loads.
  • High availability & Linear Scalability: Scaling out helps to keep data available at times of server failure. The performance gained from adding server is linear, meaning; the more the servers you add the more the performance guaranteed and reducing your database traffic by 80%. Handle higher transactions without worrying about the database bottleneck.
  • In-process client caches: TayzGrid provides support of client cache that is very close to your application. In fact, you can have keep this cache within your application process (InProc mode) or in a local but separate process. The Client Cache keeps itself synchronized with the TayzGrid clustered to avoid data integrity issues. All of this gives you a super fast cache very close to your application that boosts your application performance greatly.
  • Easy Management: Use simple and easy to learn TayzGrid Manager to manage your cluster and add or remove machines in an active production environment to increase performance.

To make the most out of Hibernate you need to have second level cache enabled and a distributed cache like TayzGrid (Open Source) plugged into it. Do this with zero code change and still run it in a multi-server configuration. You can scale-out your application and handle high load by using TayzGrid as a Hibernate Second Level Cache.

About Basit Anwer
Basit is a Solution Architect at Alachisoft - provider of a leading In-Memory Data Grid for Java called TayzGrid and for .NET called NCache. Alachisoft has been a market leader for 10 years in the .NET market with NCache.
This entry was posted in Hibernate, In-Memory Data Grid. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *