Home » DevOps » Tomcat Clustering Series Part 3 : Session Replication

About Ramki

Rama Krishnan
Ramki is a Application Developer working in the C-DAC, Pune. He has Extensive Design and Development experience in Java, Java Server Faces, Servlets, Java Persistent API (Hibernate), CDI, EJB and experience in applying Design Patterns of JavaEE Architecture.

Tomcat Clustering Series Part 3 : Session Replication

This is the third part of the Tomcat Clustering Series. In this post we are going to discuss the how to setup session replication in tomcat clustering environment. Session replication makes High availability and full fail-over capability to our clustering environment.

[Check the video below for better understanding]

In my previous post we discussed about setup simple load balancer and how to make session affinity concepts.

How to setup Session Replication in tomcat

Before going to session replication we need to understand 2 important concepts

  • Multicast
  • Session Manager in Tomcat


Multicast is to transmit a single message to a select group of recipients. here multicast used by tomcat cluster to identify the instances those part of cluster.

There are 2 types of cluster

  • Static Tomcat Cluster
  • Dynamic Tomcat Cluster

In static cluster there is no need multicast, because each tomcat we statically defined/configured the other instances. But dynamic Cluster we are not defined anything. so each tomcat in that cluster some how to identify the other tomcat instances.

So here multicast concepts is used. Each and every tomcat first joining to single multicast group. and send the heartbeat signals in periodic interval. so other tomcat instances received these signal and add the member to the cluster.

Session Manager in Tomcat

Session Manager is used to create and manage the session behalf the application. In Servlet Specification request.getSession(); line is mention that container (tomcat) is responsible for create the session. here tomcat use the Session Manager for this purpose.

There are 4 types of Session Manager

  • Standard Manager
  • Persistent Manager
  • Delta Manager
  • Backup Manager

Standard Manager

It is the default manager used by tomcat. Even though we are not mention in our web application tomcat use this manager for managing our session. If u want to customize the this standard manager then add <Manager> tag in context.xml file.

<Manager className=“org.apache.catalina.session.StandardManager” />

Here org.apache.catalina.session.StandardManager is fully qualified class name of the Standard Manager.

Persistent Manger

This mnager is to store the session information into persistent place after some interval. here two types of store is available.

  • File Store
  • JDBC Store

File Store helps to store all session information in separate files in underlying file system (local HDD or shared file-system like NFS,..)

JDBC Store helps to store the session information to relational database.

So using the Persistent Manager we can achieve the tomcat cluster. but its not swapped out in real time. its pushes the information after certain interval. so if anything badly happen(crash) before that interval then in-memory session data is gone.

Delta Manger

In this post we are going to use this manager. Its replicate the session to all other instances. so this manager usually used clustered environment. but not good for large cluster.

Backup Manager

this manager usually used clustered environment. Its like delta manger. but it will replicate to exactly one other instance(backup instance). Its acted like one instance is Primary and another instance as backup.

Steps to make Session Replication in Tomcat Clustering

Here i will continue from exactly where i left in last session affinity post. So check that post and make sure jumRoute all are set properly. So steps are

  1. Enable Multicast routing
  2. Add <Cluster> Entries in conf/server.xml file for all instances.
  3. Enable the Web Application as distributable

1. Enable Multicast routing

In Linux Environment most of the system kernel is capable to process the multicast address. but we need to add route entry in kernel routing table.

sudo route add -net netmask dev eth0

Here eth0 is my Ethernet interface. so change according to your interface

In multicast address is belong to Class D address Range ( to so we inform to kernel if any one access these address then it goes through eth0 interface.

2. Add <Cluster> Entries in conf/server.xml file for all instances.

This very important part for tomcat clustering. We need to Add <Cluster> tag in conf/server.xml file in all tomcat instances.

<Cluster className='org.apache.catalina.ha.tcp.SimpleTcpCluster'/>

We can add this <Cluster> tag in either inside the<Engine> tag or <Host> tag.

Here SimpleTcpCluster is Tomcat Cluster implementation

This tag is looks like simple but its has many inner tags. if we omitted then its takes the default values. if we want do any cutomization (like change multicat address, receving address port) we need to use complete <Cluster> tag

This is the complete <Cluster>

<Cluster className='org.apache.catalina.ha.tcp.SimpleTcpCluster'


<Manager className='org.apache.catalina.ha.session.DeltaManager'



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

<Membership className='org.apache.catalina.tribes.membership.McastService'



<Sender className='org.apache.catalina.tribes.transport.ReplicationTransmitter'>

<Transport className='org.apache.catalina.tribes.transport.nio.PooledParallelSender'/>


<Receiver className='org.apache.catalina.tribes.transport.nio.NioReceiver'










<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'/>


Check my sample conf/server.xml file (for reference)

Here most of the code is boiler plate code. just copy and paste. if we need we can customize. for example we can change the multicat address and port number.

<Manager className='org.apache.catalina.ha.session.DeltaManager'/>

here Manager tag define the delta manager. Delta manager means replicate to all instances.

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

Tomcat Clustering use the Apache Tribes communication framework. This group commnication framework is responsible for dynamic membership (using multicast) , send and receive the session delta information using uni-cast (normal TCP connection).

<Membership className='org.apache.catalina.tribes.membership.McastService'





This is Membership definition. here address is multicast address. we can pick any address from Class D address range ( to any port number.

Each and every tomcat send the heart beat signal to multicast address in periodic ( frequency) interval. all other tomcat whose joined the multicast address they can receive these signals and add the membership to the cluster. if heat beat signal is not revive some particular interval ( dropTime) from any one of the tomcat, then we need to consider that tomcat is failed.


All tomcat instances which are part of the clustering, should have same multicast address and port number.

<Sender className='org.apache.catalina.tribes.transport.ReplicationTransmitter'>

<Transport className='org.apache.catalina.tribes.transport.nio.PooledParallelSender'/>


Here sender use the PooledParallelSender have pooled connections to use the send the session information concurrently. so its speedup the session replication process.

<Receiver className='org.apache.catalina.tribes.transport.nio.NioReceiver'






Here we define which port Receiver can bind and used for receiving the session replicate information. here two properties are important. address and port. here address is ur system IP address and port is any unused port. Here address=’auto’ its automatically pick the system IP address.

We have some interceptor.

TcpFailureDetector -Its ensure that instance are dead. In some case multicast messages are delayed, all tomcat instances are think about that tomcat is dead. but this interceptor to make tcp unicast to failed tomcat and ensure that instances is actually failed or not

Another important listener is JvmRouteSessionIDBinderListener, we will talk about it later.

3. Enable the Web Application as distributable

We need to make the our web application distribuable. its simple add <distributable/> tag in web.xml file. In according to servlet specification <distributable/> tag in web.xml mention that any container to consider this application can work in distributed environment.


All session in our web application must me serializable.

Do these steps to all tomcat instances and start the tomcat and httpd server. check my configuration in my github repo or get as ZIP

This is my configuration. all 3 tomcat instances are configured in delta manager and i deployed the distributed web application. all tomcat use the multicast to maintain the membership.

Now client make the request and first tomcat process and create the session, then looks like this

Then tomcat 1 is responsible to replicate the session using Apache tribes group communication framework to replicate the session to all instances.

Now all tomcat instance have exact copy of the session. so if tomcat 1 crashed or shutdown, then any other tomcat still can process the request [see the video below]

We used session affinity like previous post. based on that cookie id contain the tomcat name (worker name). so when first tomcat1 return the session id end with tomcat1. but when tomcat 1 is failed and tomcat 2 take the responsible for all further request. but session id still contain the tomcat1. so its makes the load balancer difficult. because tomcat1 is down. and load balancer pick any other tomcat. but actually tomcat2 takes the responsible. so we need to reflect these changes in session id.

JvmRouteSessionIDBinderListener changes the client session id to tomcat2 when failure is occurred so load balancer redirect to tomcat2 without confusing.

Check the git hub for all configuration files and tomcat clustering setup is available. or u can download as ZIP

Related Links:

Screen Cast:


Reference: Tomcat Clustering Series Part 3 : Session Replication from our JCG partner Rama Krishnan at the Ramki Java Blog blog.

(0 rating, 0 votes)
You need to be a registered member to rate this.
2 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
Arvind Kumar Avinash
Arvind Kumar Avinash
7 years ago

The grammar in this article is too poor to understand the content

Rogério Disciolli
Rogério Disciolli
7 years ago