: This article describes how to process sessions in a distributed system. For more information about PHP tutorials, see. Independent server Session
As shown in:
The Independent Session of the server requires that each user's request must be processed on the same application server. This requires the server load balancer server to send the user's request to the server with the same address each time.
The first server 1 that the first user accesses must be directed to server 1 by the server load balancer throughout the user's session. Other servers do not save the Session information of user 1.
Currently, server load balancer servers generally have this function (nginx)
However, if the following conditions occur:
When server 1 goes down, the server load balancer forwards User 1 to server 2 or server 3, however, if the user does not have a secure context on server 2 and server 3, the server will notify the user to log on again. In this way, the user experience will certainly be affected. In addition, it is likely to cause user data loss.
All sessions are retained for each server.
Each server retains the sessions of all users, which is related to Session synchronization between application servers and has high real-time requirements. This method can avoid problems encountered by the independent Session of the above server, as shown in:
Advantages
In this way, even in the first case, the Session information of server 2 and server 3 is saved on server 1, when the faulty server load balancer redirects user 1 to server 2 and server 3, the server will also find the security context of user 1, you do not need to log on again.
Disadvantages
However, this method also has a disadvantage, that is, it requires a high real-time synchronization of sessions on the application server, and brings about additional cross-band overhead, and when the Session is far from changing, synchronization is required. If the amount of information in the Session is large, it will consume a considerable amount of memory.
Server shared Session
Server shared Session information:
Advantages
The Session information of each user is stored on another server (either a database or a KV storage service) outside the application ), in this way, the application server does not need to store the Session information of each user, which saves a lot of memory overhead.
When Session information is required for different application servers, find the shared Session server to obtain information.
In this way, the server load balancer server does not need to allocate users to a server, and does not need to copy Session information between servers. When Session information changes, all application servers can share the server's modification information.
Disadvantages
It depends on the shared server. if a problem occurs on the shared server or shared server cluster, the user will be greatly affected.
Transmit Session data in cookies
Storing user information in cookies can eliminate an unstable factor. However, there are still hidden risks in Cookie security and Cookie length restrictions.
'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
'). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script
The above introduces the processing of sessions in the distributed system, including the content, and hope to be helpful to friends who are interested in PHP tutorials.