Redis-Based Tomcat Session Management

Apache Tomcat clustering can seem a bit overwhelming to someone who doesn't fully understand it, but, actually, it's not that complex. Clustering can be defined as using a combination of load balancing, some form of session replication, and multiple server "workers" to process the balanced load.

What Problems Are Solved by Tomcat Clustering?

Some of the problems that Tomcat clustering is used to solve include the following. First, when a server is receiving too many incoming requests such that it cannot handle them efficiently. Second, when a stateful application requires, in case the server fails, a way to preserve session data. Third, a developer wants a way to change the configuration or to update their applications without interrupting service. These are the main reasons that we need to use Tomcat Cluster. But is there a proper way to use Tomcat Cluster that is particularly good?

What Is Redis?

Redis is an in-memory open-source data project. In fact, it is the most popular in-memory database that is currently available. In particular, Redisson can be used as a Redis Java client. Redisson uses Redis to empower Java applications for companies' use. It is intended to make your job easier and develop distributed Java applications more efficiently. Redisson offers distributed Java objects and services backed by Redis.

How Can Redis Be Used for Tomcat?

Redisson's Tomcat Session Manager allows you to store sessions of Apache Tomcat in Redis. It empowers you to distribute requests across a cluster of Tomcat servers. This is all done in non-sticky session management backed by Redis.

Alternative options might serialize the whole session. However, with this particular Redis Tomcat Manager, each session attribute is written into Redis during each invocation. Thanks to this advantage, Redisson Session Manager beats out other Redis-based managers in storage efficiency and optimized writes. Tomcat Session Management, in this way, is used in the most ideal way possible.

Step 1

Add RedissonSessionManager into tomcat/conf/context.xml

   <Manager className="org.redisson.tomcat.RedissonSessionManager" 

readMode - read attributes mode. Two modes are available:

  • MEMORY - stores attributes into local Tomcat Session and Redis. Further Session updates propagated to local Tomcat Session using Redis-based events.
  • REDIS - stores attributes into Redis only. Default mode.

updateMode - read attributes mode. Two modes are available:

  • DEFAULT - session attributes are stored into Redis only through the Session.setAttribute method. Default mode.
    • In readMode=REDIS all changes of session attributes made through the Session.setAttribute method are accumulated in memory and stored into Redis only after the end of the request.
    • In readMode=MEMORY all session attributes are always stored into Redis after the end of the request regardless of the Session.setAttribute method invocation. It is useful in case when some objects stored in session change their own state without Session.setAttribute method execution.

Step 2

Copy two jars into TOMCAT_BASE/lib directory

Base jar: redisson-all-3.22.0.jar

Tomcat Manager implementation:

The reason for using Redis Tomcat Manager to do Tomcat Clusters is if you are expecting a lot of web traffic, i.e. if your site will be expanding, or if you just want a little more help to take on the load in order to increase your availability.

What Else Can Redisson Do?

Redis-based Tomcat Session Management is not the only thing Redisson can provide. Once you are done with your cluster project, check it out for its other benefits. Redisson can provide distributed Java applications. It offers different caching implementations like JCache API, Hibernate 2nd Level Cache, Micronaut Cache, MyBatis Cache and Spring Cache. It supports read/write caching for databases, distributed implementations of ExecutorService and ScheduledExecutorService, and a Java-based MapReduce programming to support "large amounts of data" that is stored in Redis.

Overall, Redisson can do a lot. Tomcat Session Management can seem overwhelming but can be easily resolved with Redisson. Plus, you may find a lot of other features that meet your needs along the way.

Similar articles