How to Share Sessions Across Distributed Servers: Nginx, Tomcat, Redis & Cookie Solutions

This article explains why session sharing is needed in micro‑service architectures, outlines common Nginx reverse‑proxy strategies, and presents four practical solutions—Nginx ip_hash load balancing, Tomcat session replication, Redis centralized cache, and cookie‑based sharing—detailing their implementations and trade‑offs.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
How to Share Sessions Across Distributed Servers: Nginx, Tomcat, Redis & Cookie Solutions

When a project is deployed across multiple servers or clusters in a micro‑service environment, a user's session may be stored only on the server that initially handled the request. Subsequent requests routed to other servers can lack the session ID, forcing the user to log in repeatedly.

Common Nginx reverse‑proxy strategies include: Round‑robin Weight‑based ip_hash Custom strategies

Solution 1: Nginx ip_hash Load Balancing

The ip_hash method hashes the client IP to select a specific server from an array, ensuring that the same client consistently reaches the same backend.

Implementation:

upstream backend{
    ip_hash;
    server 192.168.128.1:8080 ;
    server 192.168.128.2:8080 ;
    server 192.168.128.3:8080 down;
    server 192.168.128.4:8080 down;
}
server {
    listen 8081;
    server_name test.csdn.net;
    root /home/system/test.csdn.net/test;
    location ^~ /Upload/upload {
        proxy_pass http://backend;
    }
}

Pros and cons are illustrated below:

Solution 2: Tomcat Session Replication

This approach copies the generated session ID to all servers in the cluster, so any server can retrieve the session without requiring the user to log in again.

How to implement session replication?

Pros and cons of session replication:

Solution 3: Use Redis for Centralized Session Cache

Store each session ID in Redis with an expiration time, allowing any server to retrieve the session while avoiding repeated logins.

Implementation code:

Pros and cons of Redis‑based session sharing:

Solution 4: Combine with Cookie

Store the session ID in a cookie so that each request automatically carries the session information, preventing additional logins in a distributed environment.

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.

Distributed SystemsredisTomcatsession sharing
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

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.