How to Share HTTP Sessions Across Spring Boot Instances with Redis and Nginx
This article explains why session sharing is required in distributed Java back‑ends, demonstrates how Spring Session automatically synchronizes HttpSession data to Redis, and shows step‑by‑step how to configure Spring Boot, Redis, and Nginx to achieve transparent session sharing across multiple service instances.
Why Session Sharing Is Needed
In a single‑service architecture a single server holds the HttpSession, so sharing is not an issue. In distributed or clustered projects each request may be routed to a different server, causing the session data stored on one instance to be invisible to another.
Solution with Redis and Spring Session
The common approach is to store shared data in a central store, typically Redis. Spring Session intercepts all HttpSession operations and automatically writes to or reads from Redis, making the process transparent to developers.
Step‑by‑Step Implementation
1. Create a Spring Boot project
Add the spring-boot-starter-web, spring-session-data-redis and spring-boot-starter-data-redis dependencies (see the pom.xml image).
2. Configure Redis
Only the host and port need to be set because the default port (6379) and database (0) are used.
3. Enable Spring Session
After adding the dependency and the Redis configuration, Spring Session automatically proxies the HttpSession. Developers continue to use the session as usual; the framework handles the Redis synchronization.
4. Verify with two instances
Package the application and start two instances on ports 8080 and 8081. Access localhost:8080/set to store a value; the data is written to Redis. Then call localhost:8081/get and the value saved by the first instance is retrieved, proving that the session is shared.
5. Add Nginx for load balancing
Edit /usr/local/nginx/conf/nginx.conf to define an upstream block with the two Spring Boot services, set weights, and use proxy_pass / to forward all requests. proxy_redirect rewrites redirect URLs from Tomcat to the Nginx address.
Deploy the two Spring Boot jars on a Linux server, start them with nohup java -jar app.jar &, and restart Nginx.
After clearing Redis, a request to 192.168.66.128/set goes through Nginx, is forwarded to one Spring Boot instance, stores the session in Redis, and a subsequent /get request (routed to the other instance) retrieves the same data.
Conclusion
Spring Session dramatically simplifies session sharing in Spring Boot applications by eliminating the manual configuration required in traditional SSM setups. With just a few dependencies and a Redis connection, developers get transparent, cluster‑wide HttpSession handling, and Nginx can be added to provide automatic request routing.
All example code and configuration files are available on GitHub: https://github.com/lenve/javaboy-code-samples
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
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.
