Home  >  Article  >  Backend Development  >  What are the high concurrency solutions for PHP?

What are the high concurrency solutions for PHP?

王林
王林Original
2020-08-04 11:33:446238browse

php high concurrency solutions include: 1. Use the file lock method to solve the problem; 2. Use the message queue method to solve the problem; 3. If it is a distributed cluster server, one or more queue servers are needed; 4. Use Memcache Lock solution.

What are the high concurrency solutions for PHP?

We usually encounter high concurrency problems in flash sales, grabbing train tickets, etc. The following four solutions are provided:

(Recommended tutorial: php graphic tutorial)

1. Use file lock

$fp = fopen("order.lock", "r");
if(flock($fp,LOCK_EX)){ 
//..处理订单的代码
flock($fp,LOCK_UN);
}
fclose($fp);

2. Use message queue

We commonly use To Memcacheq, Radis.

For example: If there are 100 tickets for users to grab, then these 100 tickets can be put into the cache and do not need to be locked when reading and writing. When the amount of concurrency is large, about 500 people may successfully grab tickets, so that requests after 500 can be directly transferred to the static page at the end of the event. It is impossible for 400 of the 500 people who enter to get the product.

So only the first 100 people can purchase successfully according to the order in which they enter the queue. The next 400 people will go directly to the event end page. Of course, entering 500 people is just an example. You can adjust the number yourself. The activity end page must use a static page, not a database. This reduces the pressure on the database.

(Video tutorial recommendation: php video tutorial)

3. If it is a distributed cluster server, one or more queue servers are required

Xiaomi's and Taobao's rush buying are slightly different. Xiaomi's emphasis is on the moment of rush buying. Once you grab the quota, it's yours, and you can place an order and settle the payment. Taobao, on the other hand, focuses on filtering during payment. It has implemented multiple layers of filtering. For example, if it wants to sell 10 items, it will let more than 10 users grab them. It will then perform concurrent filtering during payment, reducing the number of items layer by layer in an instant. amount of concurrency.

4. Use Memcache lock

product_lock_key is the ticket lock key.

When product_key exists in memcached, all users can enter the order process.

When entering the payment process, first store add(product_lock_key, “1″) in memcached. If the return is successful, enter the payment process. If it fails, it means that someone has already entered the payment process, and the thread waits for N seconds and performs the add operation recursively.

The above is the detailed content of What are the high concurrency solutions for PHP?. For more information, please follow other related articles on the PHP Chinese website!

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