




















































(For more resources related to this topic, see here.)
The Ehcache framework provides RMI (Remote Method Invocation) based cache replication across the cluster. It is the default implementation for replication. The RMI-based replication works on the TCP protocol. Cached resources are transferred using the serialization and deserialization mechanism of Java. RMI is a point-to-point protocol and hence, it generates a lot of network traffic between clustered nodes. Each node will connect to other nodes in the cluster and send cache replication messages. Liferay provides Ehcache replication configuration files in the bundle. We can re-use them to set up Ehcache replication using RMI. Let's learn how to configure Ehcache replication using RMI for our cluster.
net.sf.ehcache.configurationResourceName=/ehcache/hibernate-
clustered.xml
net.sf.ehcache.configurationResourceName.peerProviderProperti
es=peerDiscovery=automatic,multicastGroupAddress=${multicast.
group.address["hibernate"]},multicastGroupPort=${multicast.group.
port["hibernate"]},timeToLive=1
ehcache.multi.vm.config.location=/ehcache/liferay-multi-vm-
clustered.xml
ehcache.multi.vm.config.location.peerProviderProperties=peer
Discovery=automatic,multicastGroupAddress=${multicast.group.
address["multi-vm"]},multicastGroupPort=${multicast.group.
port["multi-vm"]},timeToLive=1
multicast.group.address["hibernate"]=233.0.0.4
multicast.group.port["hibernate"]=23304
multicast.group.address["multi-vm"]=233.0.0.5
multicast.group.port["multi-vm"]=23305
Liferay Portal uses two separate Ehcache configurations for the hibernate cache and the Liferay service layer cache. Liferay ships with two different sets of configuration files for each hibernate and service layer cache. By default, it uses the non-replicated version of the cache file. Using the portal-ext.properties file, we can tell Liferay to use the replicated cache configuration file. In the preceding steps, we configured the replicated version of cache files for both the hibernate and service layer cache using the net.sf.ehcache.configurationResourceName and ehcache.multi.vm.config.location properties. Replicated Ehcache configuration files internally use IP multicast to establish the RMI connection between each Liferay node. We configured IP multicast and ports for establishing connections.
Another option to replicate Ehcache is using JGroups. JGroups is a powerful framework used for multicast communication. The Ehcache framework also supports replication using JGroups. Similar to the RMI-based Ehcache replication, Liferay also supports JGroup-based replication. Let's learn how to configure the JGroup-based Ehcache replication.
ehcache.multi.vm.config.location=/ehcache/liferay-multi-vm-
clustered.xml
ehcache.multi.vm.config.location.peerProviderProperties=conne
ct=UDP(mcast_addr=multicast.group.address["hibernate"];mcast_
port=multicast.group.port["hibernate"];):PING:
MERGE2:FD_SOCK:VERIFY_SUSPECT:pbcast.NAKACK:UNICAST:pbcast.
STABLE:FRAG:pbcast.GMS
ehcache.bootstrap.cache.loader.factory=com.liferay.portal.cache.
ehcache.JGroupsBootstrapCacheLoaderFactory
ehcache.cache.event.listener.factory=net.sf.ehcache.distribution.
jgroups.JGroupsCacheReplicatorFactory
net.sf.ehcache.configurationResourceName=/ehcache/hibernate-
clustered.xml
net.sf.ehcache.configurationResourceName.peerProviderProperties=pe
erDiscovery=connect=UDP(mcast_addr=multicast.group.address["multi-
vm"];mcast_port=multicast.group.port["multi-vm"];):PING:
MERGE2:FD_SOCK:VERIFY_SUSPECT:pbcast.NAKACK:UNICAST:pbcast.
STABLE:FRAG:pbcast.GMS
multicast.group.address["hibernate"]=233.0.0.4
multicast.group.port["hibernate"]=23304
multicast.group.address["multi-vm"]=233.0.0.5
multicast.group.port["multi-vm"]=23305
The Ehcache replication configuration is very similar to the RMI-based replication. Here, we used the UDP protocol to connect Liferay Portal nodes. With this option both Liferay Portal nodes also connect with each other using IP multicast.
We learned about the JGroups- and RMI-based Ehcache replication. The Liferay Enterprise version includes another powerful feature called Cluster Link, which provides the Ehcache replication mechanism. Internally, this feature uses JGroups to replicate the cache across the network. Let's go through the steps to configure this feature.
cluster.link.enabled=true
ehcache.cluster.link.replication.enabled=true
net.sf.ehcache.configurationResourceName=/ehcache/hibernate-
clustered.xml
ehcache.multi.vm.config.location=/ehcache/liferay-multi-vm-
clustered.xml
Unlike the JGroups- or RMI-based Ehcache replication, this option centralizes all Ehcache changes at one place and then distributes changes to all the nodes of the cluster. This in turn reduces unnecessary network transfers.
This option is only available in the Liferay Enterprise version. Hence, the preceding steps are applicable only if you are using the Liferay Enterprise version.
We talked about different options to configure Ehcache replication. Let's learn the best practices related to Ehcache replication.
We learned about various configuration options for implementing clustering.
Further resources on this subject: