Cache replication

Exclusive offer: get 50% off this eBook here
Liferay Portal Performance Best Practices

Liferay Portal Performance Best Practices — Save 50%

A practical tutorial to learn the best practices for building high performing Liferay-based solutions with this book and ebook

$16.99    $8.50
by Samir Bhatt | August 2013 | e-Commerce Open Source

Caching is a very important technique to boost the performance of the system. Liferay Portal, by default, caches resources of the persistence layer and the service layer. By default, Liferay Portal uses the Ehcache framework for caching, and it caches resources in memory and the filesystem. In the clustered environment, each Liferay Portal node will have its own copy of the cache. It is very important to invalidate or replicate the cache on all the Liferay Portal nodes if the cache is invalidated or updated on any of the nodes. To implement this we need to replicate the cache.

In this article by Samir Bhatt, author of Liferay Portal Performance Best Practices, we will learn multiple options to replicate Ehcache across the cluster.

(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:


Liferay Portal Performance Best Practices A practical tutorial to learn the best practices for building high performing Liferay-based solutions with this book and ebook
Published: June 2013
eBook Price: $16.99
Book Price: $34.99
See more
Select your format and quantity:

About the Author :


Samir Bhatt

Samir Bhatt is an Enterprise Architect with over 12 years of IT experience. He has been working on Liferay-Portal-based solutions for the last four years. He has co-authored a book, Liferay Beginner's Guide by Packt Publishing. He is also a Liferay certified trainer and has delivered public and private training across the world. Samir leads an architectural group at CIGNEX Datamatics. He extensively worked on performance tuning of Liferay-Portal-based solutions. Apart from Liferay Portal, Samir has also worked on many other technologies and frameworks including Hadoop, MongoDB, Pentaho BI, Oracle, Java Swing, ICEfaces, ZK, Spring, Hibernate, and Visual Basic.

Samir is also a very good speaker and has delivered various webinars on Liferay, Pentaho BI, and MongoDB. He blogs at www.connect-sam.com.

Books From Packt


Liferay Beginner’s Guide
Liferay Beginner’s Guide

Liferay Portal 6 Enterprise Intranets
Liferay Portal 6 Enterprise Intranets

Liferay Portal Enterprise Intranets
Liferay Portal Enterprise Intranets

Liferay Portal Systems Development
Liferay Portal Systems Development

Liferay User Interface Development
Liferay User Interface Development

Liferay Portal 5.2 Systems Development
Liferay Portal 5.2 Systems Development

Instant Liferay Portal 6 Starter [Instant]
Instant Liferay Portal 6 Starter [Instant]

Learning Vaadin
Learning Vaadin


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
j
G
Y
q
A
3
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software