NOTE

1.4 How to Implement Distributed Sessions

Distributed sessions share user-session state across machines so users do not need to log in again when reaching a different node or system.

Distributed SystemsCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What Is a Distributed Session?

A session is server-side memory.

A distributed session is a session shared across multiple machines.

2. Why Are Distributed Sessions Needed?

In a distributed environment, if the traditional Tomcat session mechanism is still used, the following can happen: after a user logs in to system A, the user needs to access system B to perform some operations. The problem is that the user’s login information is stored in system A, while system B has no related session. At this point, system B will ask the user to log in again.

3. How to Implement Distributed Sessions

3.1. Session Replication

The disadvantage of this approach is that when there are too many machines in the cluster, session data is frequently transmitted over the network, placing a heavy load on the network.

3.2. Single Sign-On System

Send all requests related to user information, including registration and login, to a dedicated single sign-on (SSO) subsystem. SSO then generates a random number as a token and returns it to the client. The token identifies the user making the request.

3.2.1. Specific Implementation

The SSO system uses Redis to implement sessions.

Generate a random number as the key, serialize the user information as JSON and store it in Redis. When user information is needed, retrieve it from Redis using this key.

Set the session expiration time to 30 minutes.

4. References

Spring Data Redis

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub