Common SESSION synchronization mechanisms in WEB clusters

Source: Internet
Author: User
WEB clusters are common in projects. However, after server load balancer is used, session non-synchronization may occur. the most intuitive phenomenon is that after a user logs on, the status of logged-on and unlogged-on will change continuously. This is because access to the same page from the same IP address is randomly allocated to different servers. The following is the most common solution for the Tianya PHP blog [http://blog.phpha.com/##online data

WEB clusters are common in projects. However, after server load balancer is used, session non-synchronization may occur. the most intuitive phenomenon is that after a user logs on, the status of logged-on and unlogged-on will change continuously. This is because access to the same page from the same IP address is randomly allocated to different servers. Next, the Tianya PHP blog [http://blog.phpha.com/] organizes some of the most common solutions.

I. synchronize sessions with databases
I didn't use this method when synchronizing sessions on multiple servers. if I had to use this method, I thought of two methods:
1) use a low-end computer to create a database to store the session of the web server, or create the dedicated database on the file server. when a user accesses the web server, check the session in this dedicated database to synchronize sessions.
2) This method is to put the table that stores the session together with other database tables. if mysql also has a cluster, each mysql node must have this table, in addition, the data tables of this session table must be synchronized in real time.
Note: Using a database to synchronize sessions will increase the burden on the database. the database is prone to bottlenecks. if you put the session in the database, it will be worse. In the preceding two methods, the first method is better. the table for storing sessions is independent, which reduces the burden on the real database.

II. use cookies to synchronize sessions
Session is a situation where files are stored on the server, while cookie is a situation where files exist on the client. how can we achieve synchronization? The method is simple, that is, to put the session generated by the user to the cookie, that is, to use the cookie as the transfer station. When you access web server A, A session is generated to put it in the cookie, and your access is allocated to web server B. At this time, web server B first checks whether the server has this session, if no, check whether the session exists in the cookie of the client. If no, it means that the session is not saved. if there is a session in the cookie, synchronize sessoin in the cookie to web server B, so that the session can be synchronized.
Note: This method is easy to implement and will not increase the burden on the database. However, if the client bans cookies, the session will not be synchronized, which will cause losses to the website; the cookie is not highly secure. although it has been encrypted, it can still be forged.

III. use memcache to synchronize sessions
Memcache can be distributed. without this function, it cannot be used for session synchronization. It can combine the memory in the web server to form a "memory pool". no matter which server generates sessoin, it can be placed in this "memory pool", and others can be used.
Advantage: using this method to synchronize sessions will not increase the burden on the database, and the security is much higher than using cookies. putting sessions in the memory is much faster than reading from files.
Disadvantages: memcache divides the memory into multiple types of storage blocks, and the size of each block determines that memcache cannot fully utilize the memory and will generate memory fragments, if the storage block is insufficient, memory overflow occurs.

IV. Summary
The above three methods are feasible,
The first method is the one that most affects the system speed and is not recommended;
The second method has a good effect, but the security risks are the same;
The third method is the best. we recommend that you use it.

5. use memcache to synchronize sessions in web clusters
1) simulate a web Cluster
I started two memcached processes, simulating two servers respectively.

 
 
  1. /usr/local/bin/memcached -d -m 1024 -c 3000 -u uenucom -p 12000 -P ./memcached.pid
  2. /usr/local/bin/memcached -d -m 1024 -c 3000 -u uenucom -p 13000 -P ./mem.pid

2) modify the php configuration

 
 
  1. vi /usr/local/php/lib/php.ini
  2. session.save_handler = "memcache"
  3. memcache.hash_strategy = "consistent"
  4. session.save_path = "tcp://127.0.0.1:13000?weight=10,tcp://127.0.0.1:12000"

Note: In the first line, the session storage method is memcache; in the second line, the hash algorithm of memcache is consistent; in the third line, the session storage status;
3) restart apache
View phpinfo

 
 
  1. session
  2. Session Supportenabled
  3. Registered save handlersfiles user sqlite memcache
  4. Registered serializer handlersphp php_binary

Next is

 
 
  1. session.save_pathtcp://127.0.0.1:13000,tcp://127.0.0.1:12000

4) perform a simple test.
A) prepare the file session. php.

 
 
  1. session_start();
  2. $_SESSION['username'] = "abcabc";
  3. echo session_id();
  4. ?>

B) display the session content file

 
 
  1. $mem = new Memcache;
  2. $mem->addServer("127.0.0.1″,12000) or die ("Could not add server 12000″);
  3. $mem->addServer("127.0.0.1″,13000) or die ("Could not add server 13000″);
  4. $val = $mem->get('qp0mrob2ovcqle3u4lbr4obsa5');
  5. echo $val;
  6. ?>

C) the result is

 
 
  1. username|s:6:"abcabc";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.