Caching using Terracotta

In Chapter 1, Architectural Best Practices, we talked about various caching options for our Liferay-based solution. We discussed using Terracotta as a centralized cache server. If the portal is designed to handle huge amounts of traffic and transactions, it will need a good amount of cache to provide the best performance. In such situations, it is recommended to go with a high-end, centralized cache server. Terracotta is one of the most popular products in this space. We can configure Liferay Portal to cache resources in Terracotta instead of in embedded Ehcache. Let's learn how to configure Liferay Portal to cache resources in a Terracotta server. We will configure Terracotta-based caching for our clustered setup using the following steps:

  1. Download and install Terracotta in a directory on a separate server. This directory is referred to as TERRACOTTA_HOME.

    Note

    The Terracotta community edition can be downloaded from http://terracotta.org/downloads/open-source/catalog. We need to download terracotta-x.x.x-installer.jar. Here, x.x.x is the version of the Terracotta community edition. We need to follow the installation steps mentioned at the site to install the Terracotta server.

  2. Now stop the two Liferay Portal servers if they are running.
  3. Locate the node-01liferay-portal-6.1.1-ce-ga2 tomcat-7.0.27webappsROOTWEB-INFlib directory on liferay-node-01 and remove the following files:
    ehcache*.jar
    slf4j*.jar
  4. Locate the TERRACOTTA_HOMEehcachelib directory on the Terracotta server and copy the following JAR files to the node-01liferay-portal-6.1.1-ce-ga2 tomcat-7.0.27webappsROOTWEB-INFlib directory on liferay-node-01:
    ehcache*.jar
    slf4j*.jar
  5. Locate the $TERRACOTTA_HOMEcommon directory and copy the following JAR file to the node-01liferay-portal-6.1.1-ce-ga2 tomcat-7.0.27webappsROOTWEB-INFlib directory:
    terracotta-toolkit*.jar
    
  6. Create the terracotta-cache directory in the node-01liferay-portal-6.1.1-ce-ga2 tomcat-7.0.27webappsROOTWEB-INFclassesdirectory and create the hibernate-terracotta.xml file with following content:
    <ehcache
      dynamicConfig="false"
      name="hibernate-terracotta"
      updateCheck="false"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="ehcache.xsd"
    >
      <defaultCache
        eternal="false"
        maxElementsInMemory="100000"
        overflowToDisk="false"
        timeToIdleSeconds="600"
      >
        <terracotta />
      </defaultCache>
      <cache
        eternal="false"
        maxElementsInMemory="100000"
        name="com.liferay.portal.model.impl.UserImpl"
        overflowToDisk="false"
        timeToIdleSeconds="600"
      >
        <terracotta />
      </cache>
    <terracottaConfig url="<IP/host name of Terracotta Server>:<terracotta server port>" />
    </ehcache>

    Note

    Change the host name and port in the terracottaConfig tag accordingly.

  7. Create the liferay-multi-vm-terracotta.xml file in the node-01liferay-portal-6.1.1-ce-ga2 tomcat-7.0.27webappsROOTWEB-INFclasses erracotta-cache directory with the following content:
    <ehcache
      dynamicConfig="false"
      name="liferay-multi-vm-terracotta"
      updateCheck="false"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="ehcache.xsd">
      <defaultCache
        eternal="false"
        maxElementsInMemory="10000"
        overflowToDisk="false"
        timeToIdleSeconds="600">
        <terracotta />
      </defaultCache>
      <cache
        eternal="false"
        maxElementsInMemory="10000"
    name="com.liferay.portlet.journalcontent.util.JournalContent"
        overflowToDisk="false"
        timeToIdleSeconds="600">
        <terracotta />
      </cache>
      <terracottaConfig url="<IP/host name of Terracotta Server>:<terracotta server port>" />
    </ehcache>
  8. Add the following properties in portal-ext.properties of the liferay-node-01 file to enable Terracotta-based caching:
    net.sf.ehcache.configurationResourceName=/terracotta-cache/hibernate-terracotta.xml
    ehcache.multi.vm.config.location=/terracotta-cache/liferay-multi-vm-terracotta.xml
    hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
  9. Repeat steps 3 to 8 on liferay-node-02 and restart the Terracotta server and both Liferay Portal nodes.

Terracotta internally uses the Ehcache framework for caching. Hence, we replaced the Ehcache-related JAR files from the Terracotta installation. We then created the cache manager configuration file for the Hibernate cache manager and multi-VM cache manager. These XML files look similar to the Ehcache configuration files. In addition to the cache bucket configuration, we also provided Terracotta server details in the file. Within every cache bucket entry, we added the terracotta tag to inform the cache manager to store cached objects in the Terracotta server.

In the Terracotta configuration files, we configured only one cache bucket. For all other buckets, it will use cache control attributes of the defaultCache element. It is recommended to configure all the important cache buckets by adding cache entry in these configuration files. As it uses the same Ehcache XML schema, we can optimize each cache bucket by providing the same cache control attributes that we learned about in the previous section. Unlike Ehcache-based cache implementation, Terracotta provides a GUI-based tool called Developer Console for monitoring and diagnosis. This tool is useful to monitor cache buckets during the load test.

We used Terracotta only for caching resources but it also provides a way to store HTTP sessions and Quartz jobs in it. It is recommended to configure Liferay Portal to store HTTP sessions and Quartz jobs on Terracotta if we are using Terracotta for caching. This will reduce the overhead of replication.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.160.131