当前位置:网站首页>[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 .

边栏推荐
- ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
- How to reduce the font size of custom controls (optimize the round dot progress bar)
- Network request -volley
- Thingsboard - rest API obtains and refreshes tokens
- [interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
- Scrollview height cannot fill full screen
- 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
- 百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑
- Hot fix sophix multi-channel patch solution
- Svg line animation background JS effect
猜你喜欢
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it

Easy to wear - drop down radio

Creative SVG ring clock JS effect

无人驾驶: 对多传感器融合的一些思考
![[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL](/img/95/02a58c9dc97bd8347b43247e38357d.png)
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL

Svg+js keyboard control path

Go crawler framework -colly actual combat (II) -- Douban top250 crawling

Use of navigation and navigationui

MySQL log management

2019 summary and 2020 outlook
随机推荐
More pictures | explain the Nacos parameters in detail!
Helm chart warehouse operation
Encryption and encoding resolution
Decoupling pages and components using lifecycle
Technologie des fenêtres coulissantes en octets dans la couche de transmission
[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
JS dynamically generates variable names and assigns values
[figure database performance and scenario test sharp tool ldbc SNB] series I: introduction to data generator & Application to ges service
Practical operation notes - notebook plus memory and ash cleaning
Zed acquisition
Apk slimming compression experience
Do280openshift access control -- encryption and configmap
打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?
The picture of wechat official account can not be displayed normally
Using tcp/udp tools to debug the yeelight ribbon
【Redis实现秒杀业务②】超卖问题的解决方案
Virtual machine - network configuration
Kibana installation via kubernetes visual interface (rancher)
Use coordinatorlayout+appbarlayout+collapsingtoolbarlayout to create a collapsed status bar
MySQL log management