Web Session Management
Tomcat Session¶
Redisson implements a Session Manager for Apache Tomcat that utilizes Valkey or Redis as the storage backend. It stores web session in Valkey or Redis, facilitating the distribution of requests across multiple Tomcat servers in a cluster environment.
The implementation supports non-sticky session management, which means users can be routed to any server in the application cluster without losing their session data, as the session information is stored in Valkey or Redis rather than on individual Tomcat instances.
Supports Apache Tomcat versions 7.x up to 11.x
Usage¶
-
Add two jars into
TOMCAT_BASE/lib
directory:Tomcat 7.x - redisson-tomcat-7-3.50.0.jar
Tomcat 8.x - redisson-tomcat-8-3.50.0.jar
Tomcat 9.x - redisson-tomcat-9-3.50.0.jar
Tomcat 10.x - redisson-tomcat-10-3.50.0.jar
Tomcat 11.x - redisson-tomcat-11-3.50.0.jar
-
Add Session Manager
Add
RedissonSessionManager
in global context -tomcat/conf/context.xml
or per application context -tomcat/conf/server.xml
<Manager className="org.redisson.tomcat.RedissonSessionManager" configPath="${catalina.base}/redisson.conf" readMode="REDIS" updateMode="DEFAULT" broadcastSessionEvents="false" keyPrefix=""/>
keyPrefix
- string prefix applied to all keys. Allows to connect different Tomcat environments to the same Redis or Valkey instance.readMode
- read Session 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 or Valkey only. Default mode.
broadcastSessionEvents
- iftrue
thensessionCreated
andsessionDestroyed
events are broadcasted across all Tomcat instances and cause all registered HttpSessionListeners to be triggered. Default isfalse
.broadcastSessionUpdates
- iftrue
andreadMode=MEMORY
then session updates are broadcasted across all Tomcat instances. Default istrue
.updateMode
- Session attributes update mode. Two modes are available:DEFAULT
- session attributes are stored into Redis or Valkey only through theSession.setAttribute
method. Default mode.AFTER_REQUEST
- In
readMode=REDIS
all changes of session attributes made through theSession.setAttribute
method are accumulated in memory and stored into Redis or Valkey only after the end of the request. - In
readMode=MEMORY
all session attributes are always stored into Redis or Valkey after the end of the request regardless of theSession.setAttribute
method invocation. It is useful in case when some objects stored in session change their own state withoutSession.setAttribute
method execution. Updated attributes are removed from all other Session instances ifbroadcastSessionUpdates=true
and reloaded from Redis or Valkey when these attributes are requested.
- In
configPath
- path to Redisson YAML config. See configuration page for more details. In case session manager is configured programatically, a config object can be passed using thesetConfig()
method
Shared Redisson instance¶
Amount of Redisson instances created by Tomcat for multiple contexts could be reduced through JNDI registry:
-
Add shared redisson instance produced by
JndiRedissonFactory
intotomcat/conf/server.xml
inGlobalNamingResources
tag area: -
Add
JndiRedissonSessionManager
with resource link to redisson instance intotomcat/conf/context.xml
Single Sign-On (SSO)¶
To use Redisson also for SingleSignOn (SSO) within multiple applications on single or multiple Tomcats, add following code into tomcat/conf/server.xml
in Host
tag area
Spring Session¶
For information on using Spring Session implementation, please refer to the Spring Session documentation.
Micronaut Session¶
For information on using Micronaut Session implementation, please refer to theMicronaut Session documentation.