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

边栏推荐
- Go crawler framework -colly actual combat (IV) -- Zhihu answer crawl (I)
- 热力图展示相关矩阵
- The acceleration of 100 km is only 5.92 seconds, and the willanda high-performance version leads with the strength of high-energy products
- Custom animation (simulated win10 loading animation)
- Hyperledger Fabric 2. X dynamic update smart contract
- Qiniu cloud uploads video to get the first frame of video
- Use of JMeter
- How to use promise Race() and promise any() ?
- ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
- 传输层 以字节为单位的滑动窗口技术
猜你喜欢

Encryption and encoding resolution

Ott marketing is booming. How should businesses invest?

Use and click of multitypeadapter in recycleview

热力图展示相关矩阵

JMeter socket connection sends data

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
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it

Interesting checkbox counters

Xcode预览(Preview)显示List视图内容的一个Bug及解决

Source code analysis the problem that fragments cannot be displayed in the custom ViewGroup
随机推荐
Encryption and encoding resolution
D omit parameter name
Previous basic review
Fuxin Kunpeng joins in, and dragon lizard community welcomes a new partner in format document technical service
JMeter socket connection sends data
adb shell sendevent
2021-04-18
Custom animation (simulated win10 loading animation)
Several ways for wechat applet to jump to the page are worth collecting
Technologie des fenêtres coulissantes en octets dans la couche de transmission
Usage of assert
ros(25):rqt_image_view报错Unable to load plugin for transport ‘compressed‘, error string
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
不重要的token可以提前停止计算!英伟达提出自适应token的高效视觉Transformer网络A-ViT,提高模型的吞吐量!...
Collection of software testing and game testing articles
A small crawler program written by beginners
无人驾驶: 对多传感器融合的一些思考
JS dynamically generates variable names and assigns values
MySQL semi sync replication
Jar package merging using Apache ant