当前位置:网站首页>Redistemplate solves the problem of oversold inventory in the seckill system with high speed - redis transaction + optimistic lock mechanism
Redistemplate solves the problem of oversold inventory in the seckill system with high speed - redis transaction + optimistic lock mechanism
2022-07-25 17:42:00 【Suddenly】
1、 scene
Seckill system has high concurrency scenario , In the second kill of goods , High concurrency may lead to oversold inventory , So you can go through Redis The transaction mechanism provided is oversold ;Redis A transaction is actually executing all commands in sequence . The transaction will not be interrupted by other commands .
2、 Repeat the oversold scene
2.1 Initialize the inventory interface
@RestController
@RequestMapping("/redis")
@Slf4j
public class RedisController {
@Resource
private RedisTemplate redisTemplate;
// Record the quantity of goods actually sold
private AtomicInteger successNum = new AtomicInteger(0);
@GetMapping(value = "/init")
public String init() {
// Initialize inventory quantity , Simulated inventory as long as 5 A commodity , Write to redis in
redisTemplate.opsForValue().set("stock", 5);
successNum.set(0);
log.info("===>>> Inventory initialization succeeded , The stock is " + 5);
return " Inventory initialization succeeded ";
}
}
2.2 Inventory deduction interface
@RestController
@RequestMapping("/redis")
@Slf4j
public class RedisController {
@Resource
private RedisTemplate redisTemplate;
// Record the quantity of goods actually sold
private AtomicInteger successNum = new AtomicInteger(0);
@GetMapping(value = "/reduce")
public String reduce() {
int stock = (Integer) redisTemplate.opsForValue().get("stock");
log.info("===>>> Current quantity " + stock);
// The simulation reduces only one inventory
stock = stock - 1;
if (stock < 0) {
log.info("===>>> Insufficient inventory ");
return " Insufficient inventory ";
}
// Write the remaining quantity back to redis
redisTemplate.opsForValue().set("stock", stock);
// Record the quantity of goods actually sold ( Thread safe every request will be logged )
log.info("===>>> Reduce inventory successfully , Sell... Together " + successNum.incrementAndGet());
return " Reduce inventory successfully ";
}
}
2.3 test
Using tools JMeter Simulate concurrent requests , Simulate here Per second 200 Time ;JMeter Tool use reference blog :https://blog.csdn.net/tianqingmuyu/article/details/108401543
Be careful : Perform... Before testing Initialize the inventory interface , Ensure inventory is written to Redis in
Use JMeter Request interface , The results are as follows :
3、 Solve oversold and realize
3.1 Initialize the inventory interface
@RestController
@RequestMapping("/redis")
@Slf4j
public class RedisController {
@Resource
private RedisTemplate redisTemplate;
// Record the quantity of goods actually sold
private AtomicInteger successNum = new AtomicInteger(0);
@GetMapping(value = "/init")
public String init() {
// Initialize inventory quantity , Simulated inventory as long as 5 A commodity , Write to redis in
redisTemplate.opsForValue().set("stock", 5);
successNum.set(0);
log.info("===>>> Inventory initialization succeeded , The stock is " + 5);
return " Inventory initialization succeeded ";
}
}
3.2 Inventory deduction interface
@RestController
@RequestMapping("/redis")
@Slf4j
public class RedisController {
@Resource
private RedisTemplate redisTemplate;
// Record the quantity of goods actually sold
private AtomicInteger successNum = new AtomicInteger(0);
@GetMapping(value = "/reduce")
public String reduce() {
// Open transaction
redisTemplate.setEnableTransactionSupport(true);
List<Object> results = (List<Object>) redisTemplate.execute(new SessionCallback<List<Object>>() {
@Override
public List<Object> execute(RedisOperations operations) throws DataAccessException {
// monitor key
operations.watch("stock");
Integer stock = (Integer) operations.opsForValue().get("stock");
operations.multi();
stock = stock - 1;
if (stock < 0) {
log.info("===>>> Insufficient inventory ");
return null;
}
operations.opsForValue().set("stock", stock);
return operations.exec();
}
});
if (results != null && results.size() > 0) {
log.info("===>>> Reduce inventory successfully , Sell... Together " + successNum.incrementAndGet());
return " Reduce inventory successfully ";
}
return " Insufficient inventory ";
}
}
3.3 test
Using tools JMeter Simulate concurrent requests , Simulate here Per second 200 Time ;JMeter Tool use reference blog :https://blog.csdn.net/tianqingmuyu/article/details/108401543
Be careful : Perform... Before testing Initialize the inventory interface , Ensure inventory is written to Redis in
Use JMeter Request interface , The results are as follows , There is no oversold :
Conclusion
adopt Redis Transaction mechanism , It can effectively solve the oversold problem of the second kill system ;
Other implementations :《RedisTemplate Solve the problem of oversold inventory in the second kill system — Redis Implement distributed locking mechanism 》
Statement :JMeter Tool use reference blog :https://blog.csdn.net/tianqingmuyu/article/details/108401543 — @ front end SkyRain
边栏推荐
- [vscode] support argparser/ accept command line parameters
- 走马卒
- [untitled]
- [PHP pseudo protocol] source code reading, file reading and writing, and arbitrary PHP command execution
- What is metauniverse gamefi chain game system development? Development and application case and analysis of gamefi metauniverse NFT chain game system
- Does PgSQL have a useful graphical management tool?
- 8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
- 网上开期货账户安全吗?手续费怎么申请才低?
- 01.两数之和
- Hcip first day experiment
猜你喜欢

超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!

Cet

What is an IP SSL certificate and how to apply for it?

Highlights
Principle and implementation of UDP penetration NAT in P2P

HCIP第一天实验

Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation

Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant

How to fix the first row title when scrolling down in Excel table / WPS table?

【解决方案】Microsoft Edge 浏览器 出现“无法访问该页面”问题
随机推荐
RestTemplate通过泛型实现POST、PUT、DELETE、GET、集合请求以及文件上传(可批量文件、可带参数)的统一封装(可打印日志)
03.无重复字符的最长子串
服务器端架构设计期末复习知识点总结
四六级
11、照相机与透镜
函数名指针和函数指针
How to prevent the unburned gas when the city gas safety is alarmed again?
After consulting about how to deal with DDL in Flink SQL client, how to add fields and jobs to the mapping table in Fink SQL?
Take you to a preliminary understanding of multiparty secure computing (MPC)
OSPF comprehensive experiment
Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
电子产品“使用”和“放置”哪个寿命更长??
咨询下flink sql-client怎么处理DDL后,fink sql里面映射表加字段以及JOb?
[untitled]
简述冒泡排序与快速排序
pgsql有没有好用的图形化管理工具?
PHP解决并发问题的几种实现
Automated test Po design model
01. Sum of two numbers