How to Use Spring Session with Redis for Distributed Session Sharing

This guide explains how to replace Tomcat's default HttpSession with Spring Session backed by Redis, covering load‑balancing challenges, configuration steps, common pitfalls, and a complete XML‑based example for building a scalable distributed web application.

Java Backend Technology
Java Backend Technology
Java Backend Technology
How to Use Spring Session with Redis for Distributed Session Sharing

Preface

When using Nginx + Tomcat for load balancing, algorithms such as IP_Hash, round‑robin, weighted, or random are common, but session synchronization or loss can occur because each request may be routed to a different Tomcat instance.

Session sharing can be achieved by storing session data in a database (e.g., MySQL) or a cache (e.g., Redis). The open‑source tomcat-redis-session-manager rewrites Tomcat's session manager to point session storage to Redis.

Although tomcat-redis-session-manager works, its configuration is cumbersome and tightly coupled to Tomcat. Spring Session provides a cleaner, container‑agnostic solution that transparently manages distributed sessions.

Spring Session Overview

Official site: http://projects.spring.io/spring-session/

Documentation: http://docs.spring.io/spring-session/docs/1.3.0.RELEASE/reference/html5/

Spring Session offers a set of APIs and implementations for managing HttpSession, including clustered sessions stored in Redis.

Features

API and implementations for session management. HttpSession replacement that is container‑neutral. Clustered Sessions – simplifies cluster session support without binding to a specific container. Multiple Browser Sessions – allows managing several user sessions in one browser. RESTful APIs – enables passing session IDs via headers for RESTful services.

Spring Session XML Configuration Example

Based on an SSM project, the demo code is available at http://git.oschina.net/xuliugen/spring-session-demo .

(1) Environment requirements: a running Redis server.

(2) Add basic Maven dependencies (image omitted).

(3) Add Spring configuration files to create a Servlet filter that replaces the container's HttpSession with Spring Session's implementation (image omitted).

The configuration includes a LettuceConnectionFactory for Redis connections.

Note: Default Redis connection settings are shown in the image; customize them as needed (image omitted).

(5) Resolve the "Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer'" by disabling the CONFIG command in Spring Session (image omitted). Without this, the application throws a BeanCreationException indicating Redis keyspace notifications cannot be configured.

(5) Add DelegatingFilterProxy in web.xml to delegate to springSessionRepositoryFilter (image omitted).

(6) Sample Spring MVC controller code for testing (image omitted).

(7) Test by accessing http://localhost:8080/spring/session/setSession.do?name=xuiliugen&value=123456 and inspecting Redis (image omitted). The stored value includes expiration information.

(8) The tutorial is complete; refer to the demo repository for further details.

Conclusion

In distributed environments, creating sessions in Tomcat is memory‑intensive. When building custom frameworks, it is crucial to generate sessions directly rather than creating them in Tomcat first and then persisting them to Redis.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaredisSpring SessionDistributed Session
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.