After making a Web cluster, you will certainly first consider the session synchronization problem, because after load balancing, the same IP access to the same page will be assigned to different servers, if the session is not synchronized, a login, one will be logged in state, and not login status. So this paper gives three different ways to solve this problem according to this situation:
One, using Database synchronization session
I didn't use this approach when doing long server session synchronization, and if I had to use this approach I thought about two ways:
1, with a low-end computer to build a database dedicated to the session of the Web server, or, the dedicated database built on the file server, users visit the Web server, will go to this special database check the session of the situation, in order to achieve the purpose of the session synchronization.
2, this method is to store the session table and other database tables together, if MySQL also made the cluster, each MySQL node must have this table, and this session table data table to synchronize in real time.
Description: Use the database to synchronize session, will increase the burden of the database, the database is easy to create bottlenecks in the place, if the session also put into the database inside, is undoubtedly worse. The above two methods, the 1th method is better, put the table of the session to separate, reduce the real database burden
Second, synchronization session with cookies
Session is the file situation stored on the server side, cookie is the file situation exists in the client, how to achieve synchronization? The method is very simple, that is, the user access page generated by the session into the cookie, is a cookie as a transit point. You visit Web Server A, generate a session and put it in the cookie, you access is assigned to the Web Server B, this time, Web Server B First judge the server has no this session, if not, In the cookie inside to see if there is no this session, if not, that the session really does not exist, if the cookie inside, the cookie inside the Sessoin synchronization to the Web Server B, so you can achieve synchronization of the session.
Description: This approach to achieve simple, convenient, and will not increase the burden of the database, but if the client to ban the cookie, then the session will not be synchronized, which will bring loss to the site; cookies are not secure enough, but they can be forged even though they have been added to the secret.
Third, using memcache synchronous session
Memcache can do distributed, if not this function, he can not be used to do session synchronization. He can combine the memory in the Web server to become a "memory pool", no matter which server produces the sessoin can put in this "memory pool", the other can be used.
Advantages: In this way to sync session, will not increase the burden of the database, and security than with cookies greatly improved, the session into memory, than from the file to read a lot faster.
Disadvantage: Memcache the memory into many kinds of storage blocks, there is a block size, this way is decided, memcache can not fully utilize memory, will produce memory fragments, if the storage block is insufficient, also can produce memory overflow.
Four, summary
All three of these methods are feasible.
The first method, the one that most affects the speed of the system, is not recommended for use;
The second method, the effect is good, but the same security risks exist;
The third method, personally feel that the third method is the best, recommended that everyone use
V. Using Memcache to synchronize sessions during Web clustering
I think it's best to sync the session with Memcache, and here's my setup process.
1, Analog Web cluster
I started two memcached processes to simulate two servers, respectively.
/usr/local/bin/memcached-d-M 1024-c 3000-u uenucom-p 12000-p./memcached.pid
/usr/local/bin/memcached-d-M 1024-c 3000-u uenucom-p 13000-p./mem.pid
2, modify the configuration of PHP
Vi/usr/local/php/lib/php.ini
Session.save_handler = "Memcache"
Memcache.hash_strategy = "consistent"
Session.save_path = "Tcp://127.0.0.1:13000?weight=10,tcp://127.0.0.1:12000″
Description: The first line, session storage mode is Memcache, the second line, Memcache hash algorithm is consistent, the third line, session storage status;
3, back up Apache.
View Phpinfo
Session
Session supportenabled
Registered Save Handlersfiles User SQLite Memcache
Registered Serializer handlersphp Php_binary
followed by the following is
session.save_pathtcp://127.0.0.1:13000,tcp://127.0.0.1:12000
4, a simple test.
A), preparing documents session.php
The code is as follows |
Copy Code |
<?php Session_Start (); $_session[' username ' = "ABCABC"; Echo session_id (); ?> |
b), displaying session content Files
The code is as follows |
Copy Code |
<?php $mem = new Memcache; $mem->addserver ("127.0.0.1″,12000) or Die (" could not add server 12000″); $mem->addserver ("127.0.0.1″,13000) or Die (" could not add server 13000″); $val = $mem->get (' qp0mrob2ovcqle3u4lbr4obsa5′); Echo session_id (); Echo $val; ?> |
c), the result is username|s:6: "ABCABC";