Implementing Session Sharing with Spring Session and Redis in a Spring Boot Cluster
This article explains how to use Spring Session together with Redis to achieve transparent HTTP session sharing across multiple Spring Boot instances in a clustered environment, including project setup, Redis configuration, sample controller code, Nginx load‑balancing, and deployment steps.
1 Practical Example
In a traditional single‑service architecture session sharing is trivial, but in a distributed or clustered setup each server must be able to access the same session data. The common solution is to store session information in a shared store such as Redis.
1.1 Create the Project
Start a Spring Boot project and add the spring-boot-starter-web , spring-boot-starter-data-redis and spring-session-data-redis dependencies. The pom.xml snippet looks like this:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
</dependencies>1.2 Configure Redis
Add the following properties to application.properties (only host and port are required because the default port is 6379 and the default database is 0):
spring.redis.host=192.168.128.66
spring.redis.port=6379
spring.redis.password=123
spring.redis.database=01.3 Use Spring Session
After the configuration, Spring Session automatically intercepts all HttpSession operations and synchronises them with Redis. A simple controller demonstrates the usage:
@RestController
public class HelloController {
@Value("${server.port}")
Integer port;
@GetMapping("/set")
public String set(HttpSession session) {
session.setAttribute("user", "javaboy");
return String.valueOf(port);
}
@GetMapping("/get")
public String get(HttpSession session) {
return session.getAttribute("user") + ":" + port;
}
}The controller stores a value in the session on /set and reads it on /get . Because the session is backed by Redis, the data is shared across all instances.
1.4 Run Multiple Instances
Package the application and start two instances on different ports:
java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=8080 &
java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=8081 &Calling http://localhost:8080/set stores the session attribute in Redis. A subsequent call to http://localhost:8081/get retrieves the same value, proving that the session is shared.
1.5 Add Nginx for Load Balancing
Configure Nginx to balance requests between the two Spring Boot instances. The relevant part of nginx.conf looks like:
upstream javaboy.org {
server 192.168.128.66:8080 weight=1;
server 192.168.128.66:8081 weight=1;
}
server {
listen 80;
location / {
proxy_pass http://javaboy.org;
proxy_redirect off;
}
}After reloading Nginx, requests are distributed to the two backend services while the session remains consistent because it is stored in Redis.
1.6 Deploy on Linux
Upload the JAR files to a Linux server and start them with nohup so they keep running after the terminal is closed:
nohup java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=8080 &
nohup java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=8081 &Reload Nginx with /usr/local/nginx/sbin/nginx -s reload . Now you can test the session sharing by invoking the /set endpoint on one instance and the /get endpoint on the other through Nginx.
2 Summary
The article demonstrates how Spring Session abstracts away the boiler‑plate code required for session sharing, allowing developers to work with the standard HttpSession API while the framework automatically synchronises data to Redis. Adding Nginx provides transparent load‑balancing, making the solution suitable for real‑world clustered Spring Boot deployments.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.