当前位置:网站首页>[redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly
[redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly
2022-06-25 00:45:00 【Bulst】
List of articles
For seckill fruit , A user can only kill one order of one kind of fruit .
The business process

Concurrency issues
normal 
Concurrency issues 
Lock to ensure thread safety
Determine if an order exists , To ensure thread safety , We can lock ,synchronized.

If you add it to the method , It's right this Lock , in other words , All users have to lock in , This is not in line with our expectation of "one person, one order" .
We are based on users ID Lock , Reduce the range of locks , Lifting performance .
userId.toString().intern() Ensure that the only 【 Returns the canonical representation of a string object 】
@Transactional
public Result createVoucherOrder(Long voucherId) {
// One person, one single 【 Among the filters configured above , If the logged in user , Will be in ThreadLocal Add user information 】
Long userId = UserHolder.getUser().getId();
// Lock the execution code block ,intern() Methods to avoid waste of resources
synchronized (userId.toString().intern()) {
// Query order
int count = query().eq("user_id", userId).eq("voucher_id", voucherId).count();
// Judge whether it exists
if (count > 0) {
// The user has already purchased
return Result.fail(" The user has purchased once !");
}
// Deducting the inventory
boolean success = seckillVoucherService.update()
.setSql("stock = stock - 1") // set stock = stock - 1
.eq("voucher_id", voucherId).gt("stock", 0) // where id = ? and stock > 0
.update();
if (!success) {
// Deduction failed
return Result.fail(" Insufficient inventory !");
}
Cluster problem
If you are in a cluster environment , There will be multiple JVM There is , There will be multiple lock monitors , In this way, the lock will be completely invalid , There will still be thread safety problems , therefore , In the next article we will use redis Implement distributed locks to ensure thread safety .

边栏推荐
- JS dynamically generates variable names and assigns values
- Apk slimming compression experience
- 通过kubernetes可视化界面(rancher)安装kibana
- Basic summary of MySQL database knowledge
- VNC viewer remote connection raspberry pie without display
- 5-minute NLP: summary of 3 pre training libraries for rapid realization of NER
- [leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
- 融合模型权限管理设计方案
- @mysql
- Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source
猜你喜欢

JMeter socket connection sends data

Do280openshift access control -- encryption and configmap

Working principle analysis of kubernetes architecture core components

Use of navigation and navigationui
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it

A small program written this week
5-minute NLP: summary of 3 pre training libraries for rapid realization of NER

Source code analysis the problem that fragments cannot be displayed in the custom ViewGroup
Hyperledger Fabric 2. X dynamic update smart contract
随机推荐
ros(25):rqt_ image_ View reports an error unable to load plugin for transport 'compressed', error string
Working principle analysis of kubernetes architecture core components
Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source
Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
2021-11-07
Use of JMeter
Decoupling pages and components using lifecycle
I suddenly find that the request dependent package in NPM has been discarded. What should I do?
[Solved] Public key for mysql-community-xxx. rpm is not installed
Adding, deleting, modifying and checking in low build code
Tiktok wallpaper applet source code
[figure database performance and scenario test sharp tool ldbc SNB] series I: introduction to data generator & Application to ges service
The drawableleft of the custom textview in kotlin is displayed in the center together with the text
无人驾驶: 对多传感器融合的一些思考
Only positive integers can be entered in the text box
How to delete the entire row with duplicate items in a column of WPS table
C WinForm maximizes occlusion of the taskbar and full screen display
Databinding quick start (still using findviewbyid?)
Unimportant tokens can be stopped in advance! NVIDIA proposes an efficient visual transformer network a-vit with adaptive token to improve the throughput of the model
Tiktok wallpaper applet v1.0.2 function, new arrival function