4
votes

in my Spring MVC + Hibernate based web application, I used Ehcache for Hibernate 2nd level cache. It works great on a single server. However I need to migrate my application to a clustered environment. Just wondering how can I make the Ehcache as a 2nd level cache for Hibernate works in the clustered environment? Thanks in advance.

2

2 Answers

3
votes

You need to use Teracotta for clustering Ehcache and change your ehcache.xml configuration to male Ehcache aware of the cluster environment:

<cache name="cacheRegion"
    maxEntriesLocalHeap="1000"
    eternal="false"
    timeToIdleSeconds="3600"
    timeToLiveSeconds="1800"">
  <persistence strategy="distributed"/>
  <terracotta clustered="true" consistency="eventual" />
</cache>

If you don't want to buy a Teracotta license, you can set up a replicated cache instead. You can use RMI replication to coordinate Cache nodes:

<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
multicastGroupPort=4446, timeToLive=32"/>
0
votes

From my perspictive I prefer to use hazelcast cache as 2nd level cache for my application specially when you need to build clustered software application. Also you can integrate with spring easily. For more info in how to use it and integrate with spring refer to the following link https://docs.hazelcast.com/imdg/4.2/integrated-clustering/spring.html I wish you can find what you need in this topic