当前位置:网站首页>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
边栏推荐
猜你喜欢

Tkinter module advanced operations (I) -- transparent buttons, transparent text boxes, custom buttons and custom text boxes

哈夫曼树的构建

Redis源码与设计剖析 -- 16.AOF持久化机制

Wu Enda logistic regression 2

带你初步了解多方安全计算(MPC)

自动化测试 PO设计模型

HCIP第一天实验

I'm also drunk. Eureka delayed registration and this pit!

精彩记录

Calculation date or date formatting
随机推荐
[vscode] support argparser/ accept command line parameters
Calculation date or date formatting
Tkinter module advanced operations (I) -- transparent buttons, transparent text boxes, custom buttons and custom text boxes
世界各地的标志性建筑物
03.无重复字符的最长子串
Idea essential plug-ins
Redis源码与设计剖析 -- 18.Redis网络连接库分析
Go language context control function execution timeout return
约瑟夫环问题
Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
[cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
精彩记录
Three dimensional function display of gray image
【硬件工程师】关于信号电平驱动能力
电子产品EMC不合格,如何整改?
Boring post roast about work and life
Installation steps and usage of NVM under windows10 system
Interviewer: talk about log The difference between fatal and panic
Excel表格 / WPS表格中怎么在下拉滚动时让第一行标题固定住?
Interface automation test postman+newman+jenkins