What happens if two or more request comes at same time?

Kiran Patel :

I am solo developer and freelancer , I got simple local e-commerce project in my city .

I have developed all the code in PHP MySQL and with other front end technology . But I thought what happens if there is only one item left and two or more person order that same item at the same time . I have used algorithm if item is out of stock than order will be panding or ask customer to cancel project so which customer will have to cancel his project or which customer should get place order .

I know that question seems silly but , think about other project in other scenario when if I make random chatting web app than how state will affect . If a person is online and not chatting with anyone how to match accounts .

I have tried something for this but it did not worked . I have 3 computers 2 PC and one mac I try to make request at the same time but due to speed of internet only one could place order and other get message of out of order after 2 minutes. So what happens if speed was same .

Do I need to add any kind of algorithm that will place order of person and at that time other person hold for that order . I was googling but found nothing . And as I said before I am solo developer so there is no one to ask what to do or MySql will handle it .

Oh , if you are thinking to make this kind of project with WordPress or framework like laravel . I also agree with you but client said to use only plain programming language not even library .

Thank You For Reading .

fromvega :

What you are describing is a race condition type of situation and the way you handle it depends on the business logic. For instance, with the out-of-stock product, first you need to decide if you want to automatically back-order the product or if you want to abort the purchase.

If you want to abort the purchase you need to use some type of database locking mechanism. Locking can block access to a database table or record momentarily. The idea is that you lock the stock information at the beginning of the fulfillment process and unlocks it only after the purchase has been processed.

When a database request encounters a lock, it will wait until the lock is released to continue the operation (or until a timeout happens). So the request that gets the lock first, is the first one to have the chance to finish the purchase.

But locking alone might not be enough, to prevent inconsistencies you want to perform all database operations inside a database transaction. A transaction can ensure that all operations needed to process a purchase happens successfully, otherwise everything is rolled back. For instance, using transactions could prevent you from updating the stock quantity without actually finishing the purchase.

The goal is that when two requests come at almost the same time (since nothing happens at the same time), one of them will get a lock first, then finish and commit the purchase transaction. When the second request finally gets a chance to process is, the stock will be zero and the transaction will fail and a roll-back occurs (given that you did the proper checks).

The specific queries and checks you need vary from application to application, but the same concepts apply. In your case we don't have any specifics to guide you further, so I suggest you check the links provided to gather more information.

Good luck!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=302071&siteId=1