Analysis of several implementation methods to solve concurrency problems in PHP development

Source: Internet
Author: User
Tags flock
This article describes several implementation methods to solve concurrency problems in PHP development. For your reference, the details are as follows:

This article describes several implementation methods to solve concurrency problems in PHP development. We will share this with you for your reference. The details are as follows:

For concurrent scenarios such as commodity flash sales, the phenomenon of overselling may occur. in this case, we need to solve these problems caused by concurrency.

The PHP language does not provide a native concurrency solution. Therefore, you need to use other methods to implement concurrency control.

Solution 1: Use the filelock exclusive lock

The flock function is used to obtain the file lock. this lock can only be obtained by one thread at the same time. other threads that have not obtained the lock are either blocked or failed to obtain the lock.

When the lock is obtained, first query the inventory. If the inventory is greater than 0, place the order, reduce the inventory, and then release the lock.

Solution 2: Use the pessimistic lock provided by the Mysql database

The Innodb storage engine supports row-level locks. when a row of data is locked, other processes cannot operate on this row of data.

First query and lock the row:

12345 Select stock_num from table where id = 1 for updateif (stock_num> 0) {// place an order update table set stock_num = stock1 where id = 1}

Solution 3: Queue

The user's order requests are stored in one queue in sequence, and a separate process is used in the background to process the order requests in the queue.


Solution 4: use Redis

Redis operations are Atomic. you can store the inventory of commodities in redis and decr the inventory before placing the order. if the returned value is greater than or equal to 0, you can place an order. Otherwise, you cannot place an order, this method is more efficient

12345678910 If (redis> get ('stock _ num')> 0) {stock_num = redis> decr ('stock _ num') if (stock_num> = 0) {// place an order} else {// stock shortage}


Other concurrency problems:

In real-world applications, data is stored in the cache in many cases. when the cache fails, data is retrieved from the database and cached again. if the concurrency is large, many processes fetch data from the database at the same time, resulting in many requests.

Penetrate into the database and make the database crash. The File lock can be used here to solve the problem.

1234567890123 $ Data = $ cache> get ('key'); if (! $ Data) {$ fp = fopen ('lockfile'); if (flock ($ fp, LOCK_EX) {$ data = $ cache> get ('key '); // Check the cache again after the lock is obtained. if (! $ Data) {$ data = mysql> query (); $ cache> set ('key', $ data);} flock ($ fp, LOCK_UN );} fclose ($ fp );}


To put it bluntly, to solve the concurrency problem, you must lock it. the essence of various solutions is locking.

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.