Tomcat Cluster in the Cloud

The platform uses multicast to send and redirect requests to each server with a load balancer, providing session replication between server node pairs. This ensures sessions are shared between nodes over the local network without needing extra software like Memcached. This setup supports hosting large clustered applications.

This guide explains how clustering works in the platform using Tomcat as an example.

Server Clustering

The diagram shows a Tomcat cluster with two servers and one load balancer. The balancer distributes requests to different nodes based on availability and server load.

If one server fails, users are automatically switched to the other server. Thanks to session replication, the other server already has all the sessions from the failed node, so users don’t notice any difference.

To set up Tomcat clustering in the platform, follow these steps:

Step 1: Log into your platform dashboard.

Step 2: Click “New Environment“.

Create New Environment

Step 3: Select Tomcat as your application server, set the cloudlet limits, and enable Auto-clustering (High Availability) as shown below. Name the environment and click “Create“.

Enable Auto-clustering

Note: Horizontal scaling and Auto-Clustering (High Availability) are different features. Horizontal scaling uses multiple servers to evenly distribute the load, while High Availability sets up session replication between server pairs using multicast.

When you enable High Availability, the system generates a special Tomcat configuration file (tomcat-cluster.xml) for each node. Here is an example:

Tomcat-cluster.xml


<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="4">

<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>

<Channel className="org.apache.catalina.tribes.group.GroupChannel">

<Membership className="org.apache.catalina.tribes.membership.McastService"
address="10.100.2.55"
port="${MagicPort}"
frequency="500"
dropTime="3000"/>

<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="${ReceiverIp}"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>

<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>

<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>

<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>

Let’s look at this file in detail:

1. This is the main element where all other cluster elements are configured.


<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="4">

The channelSendOptions flag is attached to every message sent by the SimpleTcpCluster class or any object using the SimpleTcpCluster.send method.

2. The DeltaManager uses the SimpleTcpCluster.send method to send information through the channel.


<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>

3. The group communication framework inside Tomcat is called Tribes. It is used here as the channel element. It handles membership, logic, and communication.


<Channel className="org.apache.catalina.tribes.group.GroupChannel">

4. Membership is done using multicast. Tomcat clusters use a multicast address and port number. Communication between nodes happens over TCP.


<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="${MagicPort}"
frequency="500"
dropTime="3000"/>
{MagicPort} is a unique port number for the cluster, generated automatically.

5. Tribes’ data sending and receiving logic includes two parts: sender and receiver. The Receiver handles data receiving and has a thread pool with maxThreads and minThreads settings. The address attribute is the host address broadcasted to other nodes.


<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="${ReceiverIp}"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>

6. The Sender sends messages to other nodes. It includes the ReplicationTransmitter and Transport sub-component. Messages can be sent concurrently with NIO and a pool of senders.


<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>

7. The elements of the Tribes stack interceptors are:

  • TcpFailureDetector checks for crashed members via TCP.
  • MessageDispatch15Interceptor sends messages asynchronously to a thread pool.

<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

8. The cluster uses valves to track requests to web applications:

  • ReplicationValve starts replication when the request is completed.
  • JvmRouteBinderValve backs up your data.

<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

9. The SimpleTcpCluster is both a sender and receiver of the Channel object, so components are registered as listeners to this cluster.


<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

High Availability configuration is automated, making it easy to set up for any Java app server supported by the platform.

Save $100 in the next
5:00 minutes?

Register Here