5 min read

(For more resources related to this topic, see here.)

Ehcache replication using RMI

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.

  1. Stop both the Liferay Portal nodes if they are running.
  2. Add the following properties to the portal-ext.properties file of both the Liferay Portal nodes:

    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

  3. Now restart both the Liferay Portal nodes.

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.

Ehcache configuration using JGroups

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.

  1. Stop both the Liferay Portal nodes if they are running.
  2. Add the following properties to the portal-ext.properties file of both the Liferay Portal nodes:

    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

  3. Now restart both the nodes one by one to activate the preceding configuration.

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.

Ehcache replication using Cluster Links

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.

  1. Stop both the Liferay Portal nodes if they are running.
  2. Now deploy the ehcache-cluster-web enterprise plugin on both the Liferay Portal servers.
  3. Now, edit portal-ext.properties of both the nodes:

    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

  4. Now restart both the Liferay Portal servers to activate this configuration.

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.

Ehcache clustering best practices

We talked about different options to configure Ehcache replication. Let’s learn the best practices related to Ehcache replication.

  • If there are more than two nodes in the cluster, it is recommended to either use Cluster Link- or JGroups-based replication. If we are using the Liferay Enterprise edition, it is recommended to use Cluster Link for Ehcache replication.
  • All three options that we discussed previously use IP multicast for establishing connections with other nodes. The IP multicast technique uses group IP and port to know other nodes in the same group. It is very important to ensure that the same IP and port are used by the nodes of the same cluster.
  • It is advisable to keep the group IP and port different for development, testing, or staging environment to make sure that the nodes of other environments do not pair up with the production environment.
  • Cluster Link provides up to 10 transport channels to transfer cached resources across the cluster. If the application is supposed to have a huge cache and frequent cache changes, it is advisable to configure multiple transport channels using the cluster.link.channel.properties.transport configuration property.

Summary

We learned about various configuration options for implementing clustering.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here