session sharing solution

1.session replication

The implementation principle is to rewrite the setAttribute method by rewriting the HttpSession class. When set, values ​​are automatically synced to other servers. It is also necessary to prevent the session from being invalid and out of synchronization, and also to synchronize the getAttrbute method.

There is a problem. If the client accesses server A first, then service A is replicating the seesion to server B. At this time, the network is busy and the replication fails to retry, or the connection times out. Then the client accesses server B again at this time. It will cause session inconsistency. This method is only applicable to multiple servers in the same LAN. In this way, the problem of network delay is very small, almost non-existent.

2. The database stores session data,

To implement the principle, rewrite the setAttrbute method to the getAttrbute method. When set, the data is automatically saved to the database, and when get is read data from the database. How to synchronize if the session is invalid? You can add a last-update-time to record the last access time to determine whether it has expired.

There is a problem, the data of the database is always stored in the hard disk, but the session data is frequently accessed, which will cause the access speed to be quite slow.

3. Use redis to cache session data

The implementation principle is the same as the data method, the difference is that this method is written to redis, and the access speed will be significantly improved.

There is a problem: if redis hangs, all service areas will all hang up.

4. Simple implementation through zookeeper

Root node - "session node - "sessionId node (stores expired information, etc.) - "key node (stores value)

A relatively good implementation, zookeeper is originally a distributed implementation, and there is no single point of failure problem.

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324094319&siteId=291194637