Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of negative inventory caused by large flow and storage by php

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to solve the problem of negative inventory caused by large flow and storage by php". In daily operation, it is believed that many people have doubts about how to solve the problem of negative inventory caused by large flow and storage by php. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the question of "how to solve the problem of negative inventory caused by large flow and storage by php"! Next, please follow the editor to study!

We know that the database processes sql one by one, assuming that the process of buying goods is as follows:

Sql1: query the inventory of goods

If (inventory quantity > 0) {/ / generate order. Sql2: inventory-1}

When there is no concurrency, the above process looks so perfect, assuming that two people place orders at the same time, and the inventory is only 1, and the inventory queried by two people in the sql1 phase is > 0, so the sql2 is finally implemented, and the inventory finally becomes-1, which is oversold, either replenish the inventory or wait for the user to complain.

The popular way to solve this problem is:

1. Use additional single processes to process a queue, place order requests in the queue, process them one by one, and there will be no concurrency problems, but additional background processes and latency issues will not be considered.

two。 Database optimistic lock, roughly means to query the inventory first, and then immediately add 1 to the inventory, and then after the order is generated, query the inventory again before updating the inventory to see if it is consistent with the expected inventory quantity, and roll back if it is inconsistent with the expected inventory quantity. Remind the user that the inventory is insufficient.

3. According to the result of update, we can add a judgment condition update in sql2. Where inventory > 0. If false is returned, the inventory is insufficient and the transaction is rolled back.

4. With the help of file exclusive lock, when processing a request to place an order, lock a file with flock. If the lock fails to indicate that another order is being processed, either wait or directly prompt the user that the server is busy.

What this article wants to talk about is the fourth scheme, and the approximate code is as follows:

Blocking (waiting) mode

Non-blocking mode

At this point, the study on "php how to solve the problem of negative inventory caused by large traffic and storage" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report