How to cluster Tomcat servers

Home > Search > How-to
  by

There are two types of clusters:

  • Horizontal
  • Vertical

Usually, a horizontal cluster is preferred. In a horizontal cluster, JVMs that reside on different physical machine are clustered. The obvious advantage to a horizontal cluster is that if one physical server goes down, the other physical server can continue to produce the applications in the JVM, resulting in high-availability. Also, from a systems engineer perspective, a horizontal cluster lets the engineer do work on one cluster member without bringing down the applications being produced by the JVMs in the cluster. For example, the engineer could bring down the server01 JVM, patch the server, restart the JVM, and bring it back up, without causing an outage, as the server02 JVM can still produce the application.

In a vertical cluster, JVMs that reside on the same physical machine are clustered. Similar to a horizontal cluster, a vertical cluster lets the engineer do work on one cluster member without bringing down every JVM. However, if the physical server goes down, the entire cluster also goes down.

 


On both Tomcat servers, add <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" /> to the <Engine> or <Host> container in your $CATALINA_HOME/conf/server.xml file.

<Engine name="Catalina" defaultHost="localhost">
  <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" />
   . . .

 

Restart the Tomcat application server for this change to take effect.

~]# $CATALINA_HOME/bin/shutdown.sh
~]# $CATALINA_HOME/bin/startup.sh

 

After restart, the catalina.log file should contain the following events.

Jan 04, 2018 1:49:31 AM org.apache.catalina.ha.tcp.SimpleTcpCluster startInternal
INFO: Cluster is about to start
INFO: Setting cluster mcast soTimeout to 500
INFO: Sleeping for 1000 milliseconds to establish cluster membership, start level:4
Jan 04, 2018 1:49:31 AM org.apache.catalina.ha.tcp.SimpleTcpCluster memberAdded
Jan 04, 2018 1:49:31 AM org.apache.catalina.ha.tcp.SimpleTcpCluster memberAdded
INFO: Sleeping for 1000 milliseconds to establish cluster membership, start level:8

 


Member Disappeared

The catalina.log file of the JVMs in the cluster that are available will contain the following event if one of the other JVMs in the cluster is not available.

Jan 16, 2018 12:10:44 AM org.apache.catalina.ha.tcp.SimpleTcpCluster memberDisappeared

 


Web App / Web Service

Since web service applications do not have a UI and are not fronted by a web server, there isn’t a need for session and context replication between web services JVMs.  These apps should be stateless, meaning an invocation of the app shouldn’t be dependent on data created/updated in session or context from a previous call. It may be reasonable to not cluster web services JVMs.

If two or more web service JVMs are needed, and it doesn't make sense to create a cluster in Tomcat, there may be some challenges. For example, if a new WAR must be deployed to both web service JVMs, the WAR will need to be deployed to each JVM. Deploying a WAR to a cluster is easier than deploying a WAR to each JVM. There are ways to take care of this without creating a cluster in Tomcat. For example, one service is Hyperic by vFabric, which allows JVMs to be clustered in Hyperic, and then Hyperic can be used to deploy the WAR to every JVM in the cluster.

 

Use the Tomcat manual for more details on clustering.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments