php – How to handle concurrent order placements in an ecommerce app


The basic implementation of handling order placement is simple: process payment -> update products to reduce the quantity -> create order.

But how to handle concurrent order placements in a production level app? Suppose there are 1000 customers trying to place an order for the same product at the same time

There are two ways that I can think of

1> 1 successful customer: process payment -> update products to reduce the quantity -> create order
999 unsuccessful customers: process payment -> can’t update products since quantity is zero -> start a refund process which will return the money to the customers within 7-9 business days.

pros: 1> product is available to all until one order is successful, so high turnover

cons: 1> have to create refund to all the other customers, since there is a race condition and only one payment matters.
2> Customers money is locked for 7-9 days in the refund process, so can loose customers in long term.

2> 1 successful customer: update products to reduce the quantity -> process payment -> create order
999 unsuccessful customers: can’t update products since quantity is zero -> wait till its again available.

pros: 1> only one payment is issued, so no need to handle a lot of refunds

cons: 1> the product is unavailable to other customers as soon as a customer starts the ordering process
2> if the customers payment fails or the ordering process times out, and the product has to be updated to be made available again, then there could be a loss in product turnover.
edit (additional cons that I taught I need to specify after I read the comment from ben cottrell):
3>people can do malicious stuff like run scripts to make countless orders so that the product isn’t available from a certain seller for like an hour (until the order times out) and thus maybe route customer to another seller. Especially more likely during the launch of new product by a company.

I think both of these ways aren’t ideal. So is there any other way to do this?

ps: loosely related question I found on stackexchange
This question deals with order and payment connection (and ordering) and is not fully answered. Whereas my question deals with payment and product connection and ordering. And how to increase the product availability time whilst lowering the refund of payments and cancellation of orders.