Sharing HTTP Session Across a Cluster with Redis in Spring Boot

This article demonstrates how to reproduce the session loss problem in a load‑balanced Spring Boot application, explains its cause, and provides a step‑by‑step solution using Redis for shared session storage, including code, Nginx configuration, and deployment instructions.

IT Xianyu
IT Xianyu
IT Xianyu
Sharing HTTP Session Across a Cluster with Redis in Spring Boot

Background: In typical web development, the server stores user state in a Session object, which works fine for a single‑node application but fails in a clustered environment because the request may be routed to a different server that cannot locate the original Session.

Problem Reproduction: A simple Spring Boot 2 web app provides two endpoints ( /set-session and /get-session) to write and read a session attribute. The app runs on ports 9001 and 9002, and Nginx is configured for load balancing. When accessed through Nginx, the session data is lost.

Cause Analysis: The original server stores a cookie (e.g., Cookie_for_Session_A) that identifies the Session. A subsequent request routed to another server cannot retrieve the Session using that cookie, leading to failure.

Solution Overview: Introduce a third‑party store (Redis) to hold session data so that all application instances share the same session information.

Implementation Steps:

Add Redis dependencies to build.gradle:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    compile group: 'org.springframework.session', name: 'spring-session-data-redis', version: '2.1.2.RELEASE'
}

Configure Redis connection in application.yml (host 127.0.0.1, port 6379).

Create a configuration class annotated with @Configuration and @EnableRedisHttpSession (optionally set maxInactiveIntervalInSeconds).

Build the project with Gradle’s bootJar task and run the generated JAR on both ports 9001 and 9002.

Test the endpoints again; the session is now shared across the cluster and the problem is resolved.

Nginx load‑balancing configuration used in the test:

http {
    upstream app_server {
        server 127.0.0.1:9001;
        server 127.0.0.1:9002;
    }
    server {
        listen 9000;
        location / {
            proxy_pass http://app_server;
        }
    }
}

After applying the Redis‑backed session configuration, the test passes, confirming that session data is correctly shared among the clustered instances.

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.

BackendJavaSpring BootClusterSession
IT Xianyu
Written by

IT Xianyu

We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.

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.