How to synchronize data when using multiple nodes for the Hibernate second-level cache


I am planning to use Hibernate L2 cache that is distributed across several processes. So, essentially I would be having several different processes each with local L2 cache and they are replicated using jgroups. I am planning to use synchronous replication between the caches. However, I would like to touch upon a scenario and would like to know the behavior of Hibernate in this scenario:

Client does some update to data that is cached in local cache. Hibernate updates the database and does update to the local cache. Since the replication is synchronous, the same transaction tries to replicate date to other caches. Due to some network issue, other caches are not reachable.

  1. In above scenario, what will happen… will Hibernate update the local cache of the process that updated the database and other caches will NOT be updated?

  2. Will Hibernate Not update local cache of the process as well, so eventually the database has the update however none of the local caches has the updated data?

  3. What will be the behavior in above scenario if I configure replication to be asynchronous?

The Hibernate 2nd-level cache consists of a set of interfaces that are implemented by cache providers. Hibernate just executes those callbacks, and its the cache provider responsibility to define the cache logic.

To answer your question, using synchronous replication in a distributed system is not a good idea. You are better off using a consensus protocol, like reading and writing to a majority of nodes.

Anyway, you don’t need to implement that yourself. The cache provider should give you the possibility of using distributed nodes. Check out the Infinispan user gude about how you can configure a node cluster.

For replication, EHcache supports Jgroups, JMS and RMI. I am using jgroups which uses a consensus protocol underneath, however it also supports either synchronous or asynchronous replication options.

Then, you need to ask the question on the Ehcache to see what happens for this scenarios.

All those scenarios happen inside their code, not in Hibernate. Hibernate just delegates the calls to the 2nd-level cache provider.