What is Tomcat web session replication?
Apache Tomcat is a lightweight, highly flexible open-source project developed by the Apache Software Foundation that offers a "pure Java" HTTP web server environment for running Java code. Major companies such Walmart, E-Trade, and The Weather Channel have all used Tomcat to build powerful, robust websites.
If you're building a website with Apache Tomcat, you might run into an issue called "Tomcat web session replication." So what is Tomcat web session replication, and how does Tomcat web session replication work?
What is Tomcat web session replication?
A web session refers to the length of time that a user spends browsing a website, as well as the activities they perform on the website and the data they generate during the session. Applications save this data in order to remember and better serve the users who visit a website: for example, by personalizing their experiences, or by storing the items they want to purchase in a digital "shopping cart."
Web sessions are stored in databases, and retrieved when the web application needs to remember or update the session. Very large web applications will make use of distributed databases, in which the data is distributed across multiple servers or machines. So what happens when the web session changes on one node in the cluster? This change needs to be shared with the other nodes promptly and efficiently.
The term "web session replication" refers to how the data stored in a session is replicated across different instances in the same cluster, in order to improve the availability and scalability of this data. If an application server crashes, replicating web session data prevents you from losing the web sessions that were stored on that server. Apache Tomcat includes built-in functionality for web session replication, helping to preserve data in the event of an unexpected failure.
According to the Apache Tomcat documentation, Tomcat web session replication can be achieved in one of two ways:
- Using session persistence and saving the session to a shared file system (e.g. FileStore) or shared database (e.g. JDBCStore).
- Using in-memory replication via the SimpleTcpCluster implementation included with Tomcat.
Session replication is just one part of web session clustering, in which user sessions are balanced among the different nodes in a cluster. If a single node fails or needs to be shut down for maintenance, web session replication and web session clustering ensure that the entire system recovers seamlessly. In particular, the users whose data is stored on the node should not experience any downtime or disruption.
Tomcat web session replication and Redis
Redis is an open-source, in-memory data structure store used to implement NoSQL key-value databases, caches, and message brokers. However, Redis isn't automatically compatible with programming languages such as Java out of the box. Since Tomcat is designed specifically for Java, this fact can pose a challenge if you want to perform Tomcat web session replication in Redis.
When using Redis, many Java developers choose to install a third-party Redis Java client such as Redisson. The Redisson client includes built-in implementations of many familiar Java objects, collections, and constructs, so that Java developers can lower the learning curve when using Redis.
The good news is that Redisson makes it easy to perform Tomcat web session replication in Redis, as well as including other useful features such as different caching implementations and MapReduce programming. The RedissonSessionManager class has been specifically designed for use with Apache Tomcat, so that you can store Tomcat sessions in a Redis database.
First, add the RedissonSessionManager into your context.xml file:
<Manager className="org.redisson.tomcat.RedissonSessionManager" configPath="${catalina.base}/redisson.conf" readMode="MEMORY" updateMode="DEFAULT"/>
Please note the following issues when adding this code:
- The configPath attribute should specify the path to Redisson's JSON or YAML configuration file. For more information, see the Redisson configuration page on GitHub.
- The readMode attribute specifies how to store session attributes. The MEMORY mode, which is the default, stores attributes in both Redis and the local Tomcat session. The REDIS mode stores attributes only in Redis.
- The updateMode attribute specifies how to update session attributes. The DEFAULT mode stores session attributes in Redis only with the setAttribute() method. The AFTER_REQUEST mode stores session attributes in Redis after each request.
Next, copy the files redisson-all-3.x.x.jar and redisson-tomcat-9-3.x.x.jar into the TOMCAT_BASE/lib directory. The redisson-all-3.x.x.jar file should contain the version of Redisson that you have installed. You can find the relevant Redisson/Tomcat JAR file in this Maven repository.