Analysis of Several Implementation Methods to Solve Concurrent Problems in PHP Development

  • 2021-08-12 02:21:52
  • OfStack

In this paper, several implementation methods to solve concurrency problems in PHP development are described with examples. Share it for your reference, as follows:

For concurrent scenarios such as commodity snapping, oversold may occur, so it is necessary to solve these problems caused by concurrency

There is no native solution to provide concurrency in PHP, so other methods are needed to implement concurrency control.

Scenario 1: Use file lock exclusive lock

The flock function is used to obtain the lock of a file. This lock can only be obtained by one thread at the same time. Other threads that have not obtained the lock either block or fail to obtain it

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

Scenario 2: Use pessimistic locks provided by Mysql database

The Innodb storage engine supports row-level locking. When a row of data is locked, other processes cannot operate on the row of data

Query and lock the row first:


select stock_num from table where id=1 for update
if(stock_num > 0){
// Place an order 
update table set stock_num=stock-1 where id=1
}

Scenario 3: Using queues

The order-placing requests of users are stored in a queue in turn, and a single process is used in the background to process the order-placing requests in the queue

Scenario 4: Using Redis

The operation of redis is atomic. You can store the inventory of goods in redis. You can perform decr operation on the inventory before placing an 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


if(redis->get('stock_num') > 0){
 stock_num = redis->decr('stock_num')
 if(stock_num >= 0){
 // Place an order 
 }else{
 // Insufficient stock 
 }
}else{
// Insufficient stock 
}

Other concurrency issues:

In practical applications, data will be stored in the cache in many cases. When the cache fails, it will go to the database to fetch data and reset the cache. If the concurrency is very large, many processes will go to the database to fetch data at the same time, resulting in many requests

Penetrate to the database, and make the database collapse, which can be solved by file lock


$data = $cache->get('key');
if(!$data){
  $fp = fopen('lockfile');
  if(flock($fp, LOCK_EX)){
    $data = $cache->get('key');// Check the cache again after getting the lock, and you may already have it 
    if(!$data){
      $data = mysql->query();
      $cache->set('key', $data);
    }
    flock($fp, LOCK_UN);
  }
  fclose($fp);
}

To put it bluntly, locking is necessary to solve the concurrency problem, and the essence of various schemes is locking

For more readers interested in PHP related content, please check the topics on this site: "Summary of php+redis Database Programming Skills", "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: