How to share sessions across multiple PHP servers

Key points redis ABCD session sharing party ABCD, this party is also a blogger recommendation party ABCD.

With the gradual development of website business scale and traffic, the original mini website architecture of a single server and a single domain name can no longer meet the development needs.

At this time, we may purchase more servers from Mai, and enable multiple secondary sub domain names to channel the websites to independent servers according to business functions; Or through load balancing technology (such as DNS polling, Radware, F5, LVS, etc.), multiple channels can share a group of servers.

OK, In our mind, we have conceived such a solution, but after in-depth development, new technical problems will follow:

We distributed the website programs to multiple servers and independently divided them into several secondary domain names. Due to the limitation of the implementation principle (in PHP, the session is saved in the hard disk of the local server by default in the form of a file), our website users had to frequently enter user names and passwords back and forth between several channels to log in, leading to a greatly reduced user experience; In addition, the original program can directly read data (such as nicknames, credits, login time, etc.) from the user's session variable, because the session variable cannot be updated synchronously across servers, forcing developers to read and write the database in real time, thus increasing the burden on the database.

As a result, the need to solve the session sharing scheme between websites across servers has become urgent, and eventually led to a variety of solutions. Here are four more feasible solutions for comparison and discussion:

1. Session sharing based on Redis

Redis is a memory based database, which has become very popular in recent years, with more and more users. Based on its various types of operation processing, compared with Memcache, Redis has greater advantages.

PHP can be used normally by simply configuring php.ini and adding the Redis library. Change the save_handler parameter to Redis, and configure the Redis address.

2. NFS based session sharing

NFS is short for Net FileSystem. It was first developed by Sun Corporation to solve directory sharing between Unix network hosts.

This solution is the simplest to implement. It does not need to do too much secondary development. It just needs to mount the shared directory server to the local session directory of each channel server. The disadvantage is that NFS relies on complex security mechanisms and file systems, so the concurrency efficiency is not high. Especially for small files with high concurrent read and write, such as sessions, Because the io wait of the shared directory server is too high, the execution efficiency of front-end WEB applications will be ultimately dragged down.

3. Session sharing based on database

The famous MySQL database is the first choice, and it is recommended to use the memory table Heap to improve the reading and writing efficiency of the session operation. This scheme is very practical. I believe that it is widely used. Its disadvantage is that the concurrent read and write ability of sessions depends on the performance of MySQL database. At the same time, you need to implement session elimination logic to update and delete session records from the data table regularly. When the concurrency is too high, table locks are likely to occur. Although we can choose the table engine of row level locks, However, we have to deny that the use of database to store sessions is still a bit like killing a chicken with a knife.

4. Session sharing based on cookies

This scheme may be unfamiliar to us, but it is still widely used in large websites. The principle is to encrypt and serialize the session information of all site users, and then uniformly plant it under the root domain name (such as. host. com). When accessing all secondary domain name sites under the root domain name with a browser, the characteristics of all cookie content corresponding to the domain name will be transferred, so as to realize the shared access of users' cookie sessions among multiple services.

The advantages of this scheme do not require additional server resources; The disadvantage is that limited by the confidence length of the http protocol header, only a small part of the user information can be stored. At the same time, the cookie based session content needs to be encrypted and decrypted securely (for example, using DES, RSA, etc. for plaintext encryption and decryption; Then MD5, SHA-1 and other algorithms are used for anti-counterfeiting authentication). In addition, it will also occupy certain bandwidth resources, because the browser will attach the local cookie to the http header and pass it to the server when requesting any resources under the current domain name.

5. Session sharing based on Memcache

Memcache is a memory sharing system based on Libevent multi-channel asynchronous I/O technology. The simple Key+Value data storage mode makes the code logic compact and efficient, so it has an absolute advantage in concurrent processing capacity. At present, the projects I have experienced have reached 2000/second average queries, and the server CPU consumption is still less than 10%.

In addition, it is worth mentioning that the Expires data expiration elimination mechanism unique to Memcache's memory hash table coincides with the session expiration mechanism, which reduces the code complexity of deleting expired session data. Compared with the "database based storage scheme", this logic alone creates huge query pressure on the data table.

Redis based storage is recommended among these solutions!

Other schemes still have their use occasions. The specific set to be selected needs comprehensive evaluation by developers according to the current server resources, website concurrency pressure, etc.

 Watson Blog
  • This article is written by Published on May 16, 2019 15:17:24
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/2462.html

Comment