In the process of commodity purchase, the inventory deduction process, the general operation is as follows:

1. Select the inventory of the product based on the product ID.

2. According to the number of orders, calculate whether the inventory is enough. If the inventory is insufficient, throw out the exception that the inventory is insufficient.

3. Set Sets the latest remaining inventory value.

The pseudocode for the above procedure is as follows:

Select stock_remaing from stock_table where id=${goodsId}; } else{int new_stock= stock_remaing-quantity; Update stock_table set stock_remaing =${new_stock} id=${goodsId};Copy the code

Concurrent modification of data stock oversold

If the isolation level of a database transaction is not serializable, depending on the nature of the transaction, write overwrite may occur when concurrent modifications are made.

Assume that the remaining stock stock_remaing of the commodity is 100, customer A places 20 orders, and customer B places 30 orders. When the inventory is concurrently deducted, there may be oversold. If both customer A and customer B obtain an inventory surplus of 100, then the value committed after the transaction overwrites the value committed by the previous customer, possibly with 80 or 70 remaining inventory.

The process is as follows:

Lock update repository

To prevent write overwriting in transaction control, you might want to use select for Update to lock the inventory of the item and then do the rest.

The process is as follows:

Above, pessimistic locking mode is used. In distributed service, if the concurrency is relatively high, the operation of inventory reduction is serial operation, which is very low efficiency.

Update using optimistic locks

At update time, update inventory with (CAS+ version update) + retry condition (retries or retry time limit) optimistic lock. At this point, if both customer A and customer B read the inventory surplus 100, one of the operations will fail during the update.

The process is as follows:

This method can greatly improve the concurrency and ensure the consistency of data. Conditional control of retry times and retry times can prevent database stress caused by excessive retries.

Can I use direct decrement?

When deducting inventory, some people propose not to perform the select, calculate, set three-stage operation, direct deduction, and the deduction to less than zero to make a judgment. The pseudocode is as follows:

Update stock_table set remaing_stock=remaing_stock-${quantity} where id = commodity ID and remaing_stock>${quantity};Copy the code

In distributed service invocation, there may be service retry during microservice invocation because of network exception and fetch server exception. For example, the scenario of gateway timeout, service retry mechanism. In this case, this method does not meet the idempotence, but has the situation of multi-buckle. For example, when the same user discharges inventory, the service retries. In extreme cases, the user performs the discharges more than once, and the item is oversold.

Can redis be used for inventory deduction?

Because I have not studied the redis source code, for this way reference daniu’s reply, the answer is that you can use Redis transactional deduction balance, but in THE CAS mechanism has no advantage over mysql, high performance because of its memory storage, the side effect is the risk of data loss.

Original link: blog.csdn.net/new_com/art…

Copyright: Iloveoverfly is an original article published BY CSDN blogger iloveoverfly.

Recent hot articles recommended:

1.1,000+ Java Interview Questions and Answers (2021)

2. Awesome! Java coroutines are coming…

3. The latest! Log4j 2.x releases a new version of Log4j 2.x that officially addresses nuke-level vulnerabilities.

4.Spring Boot 2.6 is out with a lot of new features.

5. “Java Development Manual (Songshan version)” the latest release, quick download!

Feel good, don’t forget to click on + forward oh!