Backend Development 5 min read

Session Sharing Solutions in Distributed Environments: Nginx ip_hash, Tomcat Replication, Redis Cache, and Cookie

To prevent repeated logins in distributed microservice systems, this article explains four session‑sharing solutions—Nginx ip_hash load balancing, Tomcat session replication, Redis‑based session caching, and cookie‑based storage—detailing their principles, configuration examples, and advantages and disadvantages.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Session Sharing Solutions in Distributed Environments: Nginx ip_hash, Tomcat Replication, Redis Cache, and Cookie

In microservice and distributed deployments, a project may run on multiple server clusters, causing a problem: a user's session is stored on the server that initially handled the request, typically in a JVM ConcurrentHashMap keyed by session ID. When subsequent requests are routed to a different server that lacks this session information, the user is forced to log in again, leading to a poor experience.

Therefore, implementing session‑sharing mechanisms is crucial for maintaining seamless user interactions across distributed environments.

Solution 1: Nginx ip_hash Load Balancing

This method hashes the client IP address against the list of available backend servers and forwards the request to the same server each time, ensuring the session remains on a single node.

Configuration example (add to nginx.conf ):

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;
    }
}

The advantages and disadvantages of this approach are illustrated in the original article (image).

Solution 2: Tomcat Session Replication

This approach copies the generated session ID to all Tomcat instances in the cluster, so any server handling a subsequent request can locate the same session, eliminating repeated logins.

Implementation details are shown in the article’s screenshots (image).

Solution 3: Redis‑Based Session Cache

Each request’s session ID is stored in a Redis server with an expiration time. All application nodes retrieve the session from Redis, ensuring consistency without requiring the user to re‑authenticate within the TTL.

Configuration and code examples are provided in the article’s images (image).

Solution 4: Cookie‑Based Session Storage

By placing the session identifier in a cookie, every client request carries the session information, allowing any server in the distributed system to recognize the user without additional login steps.

The article concludes with a link to the original source for further reading.

Distributed SystemsBackend DevelopmentRedisnginxTomcatsession sharing
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login 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.