当前位置:网站首页>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
边栏推荐
- [solution] the Microsoft edge browser has the problem of "unable to access this page"
- Summary of 80 domestic database operation documents (including tidb, Damon, opengauss, etc.)
- Idea essential plug-ins
- How to prevent the unburned gas when the city gas safety is alarmed again?
- 精彩记录
- 关于flickr的数据集笔记
- Update 3dcat real time cloud rendering V2.1.2 release
- Function name pointer and function pointer
- Enumeration classes and magic values
- 01. Sum of two numbers
猜你喜欢

Cet

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!

OSPF---开放式最短优先路径协议

The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development

"Digital security" alert NFT's seven Scams

交友活动记录

【解决方案】Microsoft Edge 浏览器 出现“无法访问该页面”问题

11、照相机与透镜
随机推荐
新版selenium4.3在egde浏览器的无头模式
Redis源码与设计剖析 -- 16.AOF持久化机制
走马卒
Is there a method in PostgreSQL that only compiles statements but does not execute them?
WPF 实现用户头像选择器
多项式相加
I'm also drunk. Eureka delayed registration and this pit!
什么是元宇宙Gamefi链游系统开发?Gamefi元宇宙NFT链游系统开发应用案例及分析
02.两数相加
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
[Hardware Engineer] can't select components?
I want to manage money. I don't understand. Is there a principal guaranteed financial product?
PostgreSQL里有只编译语句但不执行的方法吗?
基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
go defer与recover简单笔记
大型仿人机器人的技术难点和应用情况
枚举类和魔术值
哈夫曼树的构建
Pymongo saves data in dataframe format (insert_one, insert_many, multi-threaded saving)
Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant