Home >Backend Development >PHP Tutorial >Session processing in distributed systems

Session processing in distributed systems

WBOY
WBOYOriginal
2016-07-29 08:58:221435browse

Server Independent Session

As shown below:
Server-independent Session requires that each user request must be operated on the same application server, which requires the load balancing server to send the user's request to the server with the same address every time.
Server No. 1 that the first user accesses for the first time must be directed by the load balancing server to Server No. 1 throughout the user's session. Other servers will not save the session information of user No. 1.

Nowadays load balancing servers generally have this function (nginx)
Session 在分布式系统中的处理

But if the following situation occurs
Session 在分布式系统中的处理

When server No. 1 is down at this time, the load balancing server will divert user No. 1 Go to server No. 2 or No. 3, but the user does not have a secure context on servers No. 2 and No. 3. The server will notify the user to log in again. This user experience will definitely be affected. And it is very likely to cause user data loss.

Each server retains all sessions

Each server retains all user sessions. This is related to the issue of session synchronization between application servers, and the real-time requirements are relatively high. This method can avoid the problems encountered by the independent session of the above server, as shown in the figure below:

Session 在分布式系统中的处理

Advantages

In this method, even if the first situation occurs, the number 1 will be saved on servers 2 and 3. Session information, when the load balancing server fails and diverts user 1 to servers 2 and 3, the server will also find that there is the security context of user 1 and can continue to access without the need to log in again.

Disadvantages

But this method also has disadvantages, that is, the real-time requirements for Session synchronization of the application server are relatively high, and it will bring additional cross-band overhead, and when the Session changes remotely, synchronization is required. If the amount of information in the Session is relatively large, it will consume considerable memory.

Server Shared Session

Server Shared Session Information:

Session 在分布式系统中的处理

Advantages

The Session information of each user will be stored in another server outside the application (maybe a database or a KV storage service) , so that the application server does not need to store the Session information of each user, saving a lot of memory overhead.

When different application servers need to use Session information, go to the shared Session server to obtain the information.

In this way, the load balancing server does not need to assign users to a fixed server, and there is no need to copy session information between servers. When the session information changes, the application server can go to the shared server to modify the information.

Disadvantages

Relying on shared servers. Once there is a problem with the shared server or shared server cluster, users will be greatly affected

Transmitting Session data in Cookies

Storing user information in Cookies can eliminate an instability factors, but Cookies still have hidden dangers in terms of security, and Cookies also have length restrictions.

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the processing of Session in distributed systems, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP正则表达式总结Next article:PHP Swift Mailer库使用