当前位置:网站首页>Several implementations of PHP to solve concurrency problems
Several implementations of PHP to solve concurrency problems
2022-07-25 17:39:00 【kankan231】
For concurrent scenarios such as rush purchase of goods , There may be oversold , At this time, we need to solve these problems caused by concurrency
stay PHP There is no native solution to concurrency in the language , Therefore, we need to use other methods to achieve concurrency control .
Scheme 1 : Use file lock exclusive lock
flock The function is used to obtain the lock of the file , This lock can only be obtained by one thread at the same time , Other threads that do not acquire locks are either blocked , Or the acquisition fails
When you get the lock , Query inventory first , If the inventory is greater than 0, Then place the order , Reduce inventory , Then release the lock
Option two : Use Mysql Pessimistic lock provided by database
Innodb The storage engine supports row level locking , When a row of data is locked , Other processes cannot operate on this row of data
First query and lock the row :select stock_num from table where id=1 for update
if(stock_num > 0){
// Place the order
update table set stock_num=stock-1 where id=1
}
Option three : Using the queue
Put the user's order request into a queue in turn , In the background, a separate process is used to process the order placing request in the queue
Option four : Use Redis
redis All operations are atomic , The inventory of goods can be stored in redis in , Check the inventory before placing an order decr operation , If the returned value is greater than or equal to 0 You can place an order later , Otherwise, you cannot place an order , This way is more efficient
if(redis->get('stock_num') > 0){
stock_num = redis->decr('stock_num')
if(stock_num >= 0){
// Place the order
}else{
// Insufficient inventory
}
}else{
// Insufficient inventory
}
Other concurrent problems :
In practical applications , In many cases, data will be stored in the cache , When the cache fails , Fetch data from the database and reset the cache , If the concurrency is large , There will be many processes going to the database to get data at the same time , Lead to many requests
Penetrating into the database , And make the database crash , File lock can be used here
$data = $cache->get('key');
if(!$data){
$fp = fopen('lockfile');
if(flock($fp, LOCK_EX)){
$data = $cache->get('key');// After getting the lock, check the cache again , There may already be
if(!$data){
$data = mysql->query();
$cache->set('key', $data);
}
flock($fp, LOCK_UN);
}
fclose($fp);
}To put it bluntly , To solve the concurrency problem, we must lock , The essence of various schemes is locking
边栏推荐
- 实时黄金交易平台哪个可靠安全?
- Page table cache of Linux kernel source code analysis
- 生成扩散模型漫谈:DDPM = 贝叶斯 + 去噪
- How to rectify the unqualified EMC of electronic products?
- Redis源码与设计剖析 -- 17.Redis事件处理
- Automated test Po design model
- "Digital security" alert NFT's seven Scams
- Is there a principal guaranteed product for financial management?
- Cross validation (CV) learning notes
- 理财有保本产品吗?
猜你喜欢

四六级

11. Camera and lens

Beyond convnext, replknet | look 51 × 51 convolution kernel how to break ten thousand volumes!

Redis源码与设计剖析 -- 16.AOF持久化机制

window10系统下nvm的安装步骤以及使用方法

大型仿人机器人的技术难点和应用情况

POWERBOARD coco! Dino: let target detection embrace transformer

Idea 必备插件

Highlights

Tkinter module advanced operations (I) -- transparent buttons, transparent text boxes, custom buttons and custom text boxes
随机推荐
04. Find the median of two positive arrays
mysql case when
我也是醉了,Eureka 延迟注册还有这个坑!
HCIP第一天实验
约瑟夫环问题
[Hardware Engineer] can't select components?
What financial products can you buy to make money with only 1000 yuan?
PHP解决并发问题的几种实现
Is it safe to open a futures account online? How to apply for a low handling fee?
Interviewer: talk about log The difference between fatal and panic
Product life cycle to be considered in making intelligent hardware
Excel表格 / WPS表格中怎么在下拉滚动时让第一行标题固定住?
03.无重复字符的最长子串
咨询下flink sql-client怎么处理DDL后,fink sql里面映射表加字段以及JOb?
Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
Technical difficulties and applications of large humanoid robots
EDI docking commercehub orderstream
什么是 IP SSL 证书,如何申请?
如何看一本书
Update 3dcat real time cloud rendering V2.1.2 release