web server cluster (multiple web servers) session synchronization sharing three solutions

  • 2020-05-07 20:50:31
  • OfStack

After doing web clustering, you will definitely consider session synchronization first, because after load balancing, the same IP access to the same 1 page will be assigned to a different server, if session is not synchronized, 1 logon user, 1 will be logon status, 1 will not be logon status. Therefore, this paper gives three different methods to solve this problem according to this situation:
1. Use database to synchronize session
I did not use this method when doing multi-server session synchronization. If I have to use this method, I have thought of two methods:
1. Use a low-end computer to set up a database to store session of web server, or set up this special database on a file server. When users visit web server, they will go to check1 and session to achieve the purpose of session synchronization.
2. This method is to put the session table and other database tables in one place. If mysql is also clustered, each mysql node should have this table, and the data table of session table should be synchronized in real time.
Note: using the database to synchronize session will increase the burden on the database. The database is already a bottleneck place. If session is still put into the database, it will make things worse. Of the above two methods, the first method is better. It separates the session tables and reduces the burden on the real database
2. Synchronize session with cookie
session is the situation of the file in the server side, cookie is the situation of the file in the client side, how to achieve synchronization? The simple solution is to put the session generated by the user visiting the page into cookie, which is to use cookie as the relay station. You visit web server A, session put it on the cookie inside, you visit assigned to B web server, this time, web B judgment first server to have the session, if not, to see if client cookie inside is this session, if no, explain session really don't save, if cookie there, Just synchronize sessoin in cookie to B on web server, and then you can synchronize session.
Note: this method is simple to implement, convenient, also won't increase the burden of the database, but if the client banned cookie, then session can not synchronize, this will bring losses to the site; The security of cookie is not high, although it has been encrypted, it can still be forged.
3. Synchronize session with memcache
memcache can be distributed, without which it cannot be used to synchronize session. He can combine the memory in web server into a "memory pool". No matter which server produces sessoin, it can be put into this "memory pool" and the rest can be used.
Advantages: synchronizing session in this way does not add to the burden on the database, and is much more secure than using cookie, which is much faster to put in memory than reading from a file.
Disadvantages: memcache divides the memory into many kinds of storage blocks. If there is a block, there is a size. This method also determines that memcache cannot fully utilize the memory.
4. Summary
All three of the above methods are feasible
The first method, which affects the speed of the system the most, is not recommended.
The second method, the effect is good, but the existence of a safety hazard;
The third method, I think the third method is the best, recommended to use

Related articles: