What are the php concurrency solutions?

王林
Release: 2023-03-07 11:28:01
Original
2775 people have browsed it

php concurrency solutions include: 1. File lock; 2. Message queue, such as Memcacheq, Redis, etc. that we often use; 3. Queue server; 4. Memcache lock.

What are the php concurrency solutions?

Solution:

(Recommended video tutorial:java video tutorial)

1. Using file lock

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

2, using message queue

We often use Memcacheq and 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. Therefore, 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.

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

Xiaomi and Taobao's rush buying is slightly different. Xiaomi focuses on the moment of grabbing. The quota is 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.

Related recommendations:php training

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

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!