当前位置:网站首页>[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 .
边栏推荐
- MySQL semi sync replication
- Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
- Go crawler framework -colly actual combat (I)
- 2021-04-18
- Android SQLite database
- 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
- Alternative to log4j
- Related operations of ansible and Playbook
- JSON file of China's provincial boundaries
- Custom control - round dot progress bar (imitating one key acceleration in security guard)
猜你喜欢
【Redis实现秒杀业务④】一人一单,不可重复购买
Decoupling pages and components using lifecycle
Fuxin Kunpeng joins in, and dragon lizard community welcomes a new partner in format document technical service
iNFTnews | 国内NFT发展仅限于数字藏品吗?
Usage of ViewModel and livedata in jetpack
不重要的token可以提前停止计算!英伟达提出自适应token的高效视觉Transformer网络A-ViT,提高模型的吞吐量!...
Use of navigation and navigationui
How to reduce the font size of custom controls (optimize the round dot progress bar)
傳輸層 以字節為單比特的滑動窗口技術
Design scheme of authority management of fusion model
随机推荐
vim使用命令
Android SQLite database
Jar package merging using Apache ant
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
Scrollview height cannot fill full screen
[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
Hot fix sophix multi-channel patch solution
Hyperledger Fabric 2. X dynamic update smart contract
通过kubernetes可视化界面(rancher)安装kibana
Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...
2021-09-12
Go crawler framework -colly actual combat (II) -- Douban top250 crawling
Color gradient gradient color collection
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
Infotnews | is the development of domestic NFT limited to digital collections?
Use and click of multitypeadapter in recycleview
Thingsboard - rest API obtains and refreshes tokens
Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
[issue 25] face to face experience of golang Engineer in the rightmost social recruitment
Usage of ViewModel and livedata in jetpack