Home > Back-end >  Excuse me redis decr why cannot prevent oversold
Excuse me redis decr why cannot prevent oversold

Time:11-22

General logic goes like this:
 
CNT=redis. Decr (product_key);
If (CNT & gt;=0)
Mq. Send (MSG);//actual to reduce inventory
The else
Other logic

Arguably decr is atomic operation, should can prevent oversold, but complicated with a large, there will be oversold phenomenon, thought is mq repeated consumption at the beginning, but found that there is the phenomenon to the following demo
 
Result=redis. Decr (key);
If (result & lt;=10) {
Count + +;
}
Print (result + "|" + count);

The count value will be greater than 10,
Please, comment,

CodePudding user response:

Certainly is not the problem here, it should be other problems, such as repeating the send, or repeated consumption problems, such as

The second paragraph, you can write a complete Demo again,
If so, you can go to a Redis authors find trouble,

CodePudding user response:

reference 1st floor water 2 reply:
certainly is not the problem here, it should be other problems, such as repeating the send, or repeated consumption problems, such as

The second paragraph, you can write a complete Demo again,
If so, you can go to a Redis authors find trouble,



I wrote, do so, the demo is oversold, then see a post Stack Overflow, https://stackoverflow.com/questions/47898901/will-redis-incr-command-can-be-limitation-to-specific-number
Just mentioned the inside use lua, but still don't know specific reasons, I think will because there are some unknown stress redis=operation,=

CodePudding user response:

You wouldn't be according to Redis product_key value, to determine whether oversold???????
This is a mistake,

You want to use the following code in the [product_key + "actual sales number"] to determine whether oversold,
 
CNT=redis. Decr (product_key);
If (CNT & gt;=0) {
Mq. Send (MSG);//actual to reduce inventory
Redis. Incr (product_key + "actual sales number")
} else {
Other logic
}

CodePudding user response:

Decr results might be negative, that's true, as long as decr negative, not the operation of inventory is ok, but don't use the return value to business processing,

Because at the time of high concurrency, decr return values, even beyond the inventory of several times, but your code has been done, less than 0 will not decrease inventory,
  • Related