I'm trying to implement a little shopping system. I have a single product which has a limited stock.

For example, my product can be bought 5 times.

My problem is, if a user adds this product to his cart, fill the form and pay.

Decrement the stock when he adds the product to his cart (problem: if he quits the site before the payment? The product stock is decremented, but not bought)

Decrement the stock when the payment is confirmed (problem, if 2 or more user pay at the same time)

Have you got any advice on the best way to solve this?


  • 4
    There was a similar question here: programmers.stackexchange.com/questions/133925/… In short there is no 'best way'. You can implement 'reservations' but run the risk to block items (for some time at least) or have to make a final check at sell out and run the risk that you have to tell the customer that the item is gone. – thorsten müller Nov 12 '15 at 11:07
  • 1
    Following on from what @thorstenmüller said, some sites (e.g. Amazon) give an indicator of stock levels while you're going thru the purchase process. I suppose the rationale being that you're going to be more forgiving if an item is out of stock on checkout if you'd been warned that there were say, only 1 of that item in stock beforehand. – Robbie Dee Nov 12 '15 at 11:25
  • Problem is not so for warn the user, that to have a sure system. The stock must to be minimum at 0, and not less. If I control the stock just before payment, it is possible that 2 people at the same time clic on the payment button (It is rarely the case, but still possible) – anubis Nov 12 '15 at 11:47

I would look at it as if it was a physical shop. Customer A goes to a shelve, takes the products needed from the shop into his or her cart and may or may not proceed to buy the products in the cart. Customer B can go to the shelve, however he or she can not pick up the same stock as Customer A as that stock has moved into Customers A cart.

The point is to not handle stock as a number, but as actual objects that move through your system, until they are checked out.

You should then have a time out mechanism, so that if someone leaves a cart with stock in your shop, stock is put back on the shelve at some point. Think of it as a shop clerk going through the store and cleaning up after the customers after the shop has closed for the night.

| improve this answer | |
  • 2
    A big part of this is what is called the "conversion rate", the ratio of people who put things in their cart to people who actually check out. When I used to do e-commerce work, the conversion rate was shockingly low -- on the order of 10%. So you're conceivably tying up 90% of your inventory in carts that will never go through checkout. Our solution was to do an inventory check every time an item was put in the cart, and inform users that out-of-stock items were being removed from their cart. Possibly not relevant to the problem stated, but in general. – TMN Nov 12 '15 at 14:23
  • That is a useful addition. However, wouldn't this be solved by a correct time out on the carts contents? I'd also be interested in the number of concurrent users and when you would actually decide that a user was not going to check out their cart. – Jonathan van de Veen Nov 12 '15 at 14:55
  • The problem is how do you determine what a "correct" time out would be? I worked on a site for a large international toy store, so we had tens of thousands of concurrent users (hundreds of thousands over the holidays). I don't think anyone "decided" when a user wasn't going to check out, it was when the number of items in all carts reached a certain limit, they would run a database purge and remove all items that had been carted longer than (say) six months ago. – TMN Nov 12 '15 at 15:10
  • Six months sounds like a way to long period to me. Do users actually expect to come back to a site after six months and find their shopping cart in tact? – Jonathan van de Veen Nov 13 '15 at 13:57
  • I don't know what the actual horizon was (I was in R&D, not operations), but I do know that it was common for people to start putting things in their cart in mid-November and not actually check out until mid-December, using it like a virtual shopping list. We did support multiple wish lists, but apparently this was still common. – TMN Nov 16 '15 at 14:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.