Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Redis-Based Tomcat Session Management

DZone 's Guide to

Redis-Based Tomcat Session Management

Learn what Tomcat clustering is and what problems it can solve by working together with Redis.

· Performance Zone ·
Free Resource

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"
          configPath="${catalina.base}/redisson.conf" readMode="MEMORY" updateMode="DEFAULT"/>


readMode - read attributes mode. Two modes are available:

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

updateMode - attributes update mode. Two modes are available:

  • DEFAULT - session attributes are stored into Redis only through setAttribute method. Default mode.
  • AFTER_REQUEST - all session attributes are stored in Redis after each request.

Step 2

Copy two jars into TOMCAT_BASE/lib directory

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, 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.

Topics:
java ,redis ,tomcat ,session management ,performance

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}