How to implement a distributed session

There are several ways of approximately:

  1. Sticky session
  2. session replication
  3. session sharing mechanism
  4. session persistence to the database

1. sticky session

Principle: Viscous Session refers to a user locks onto a server.

Such as the above said example, the first time a user request, load balancer forwards the user's request to the server A, if the load equalizer settings viscous Session, then after each time the user requests are forwarded to the server A, a server and the user corresponds to a stick, which is a viscous Session mechanism.

Advantages: simple, does not require any treatment session.

Disadvantages: lack of fault tolerance, if the current server access fails, the user is transferred to the second server, he's session information will be lost.

Applicable scene: the failure occurred less impact on the customer generated; server failure is a low probability event.

Implementation: In Case Nginx disposed upstream module ip_hash properties can be realized in a viscous Session.

upstream mycluster{
    #这里添加的是上面启动好的两台Tomcat服务器
    ip_hash;#粘性Session
     server 192.168.22.229:8080 weight=1;
     server 192.168.22.230:8080 weight=1;
}

2. Copy the server session

How it works: Any session on a server changes (additions and deletions), all the contents of the node will put this session serialization, then broadcast to all other nodes, regardless of other servers or need session, in order to ensure that the Session synchronization.

Advantages: fault-tolerant, session between the servers can be real-time response.

Cons: network load will cause some pressure, if large, then session may result in network congestion and slow down the server performance.

Method to realize:

① set tomcat, server.xml tomcat clustering feature is turned
Here Insert Picture Description
Address: Fill in the local ip can set the port number, port conflict prevention.

② increase in applications where information: Notification application is currently in a cluster environment, support for distributed
add options in web.xml.

3.session sharing mechanism

Principle: The Session is stored on the distributed cache cluster a machine, when a user visits different nodes existing cache information to get Session

Usage scenarios: the number of machines in the cluster and more complex network environment

Pros: Good reliability

Disadvantages: implementation complexity, stability depends on the stability of the cache, Session information must be reasonable when placed in the cache write strategy

4.session persisted to the database

Principle: Needless to say it, come up with a database, designed to store session information. Ensure session persistence.

Advantages: server problem, session is not lost

Disadvantages: If a lot of traffic to your site, the session is stored in the database, the database will cause a lot of pressure, but also need to add additional overhead to maintain the database.

https://blog.csdn.net/u011213044/article/details/80525997

https://www.jianshu.com/p/d788ecba9514?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Published 67 original articles · won praise 32 · views 60000 +

Guess you like

Origin blog.csdn.net/weixin_43751710/article/details/105278147