1

I am working on an shopping website in which we only have unique products. So users can see and add same products to their cart simultaneously but when they will place an order then whosoever will place it first will get the same items added to cart.

But I have problem that how will I ensure that my place order code when called will be locked and all other requests have to wait in queue until its not complete. I read about lock keyword but not much idea. Any help will be appreciated.

Below are few steps for my code:

Step 1: To call a 3rd party service to see that item is available or not Step 2: If Item is available then buy it by calling that 3rd party service. Step 3: If bought successfully then update the database with flags etc.

3
  • would you mind to provide a sample of your code? Commented Aug 2, 2016 at 15:58
  • It sounds like you have some kind of fundamental design flaw. Requests should not be interfering with each other and you shouldn't have to do any kind of locking to make it work. Seeing your code would help us figure out what's wrong. Commented Aug 2, 2016 at 16:00
  • This is not what lock's are for. You need a way to 'remove' an item from inventory temporarily while it is in a persons cart (if I understand your question correctly). The best way I can think is by having the cart in the database. then you can always query against the carts to see what your potential stock is. Commented Aug 2, 2016 at 16:01

1 Answer 1

4

You cant reliably lock the code on the web server since multiple web requests can run in different processes (at least when you upgrade your server in the future).

Instead you should make sure the database changes can only succeed once and handle the failure gracefully for second requests. Database changes are "naturally" atomic so with some smart insert/updates your problem is solved.

Sign up to request clarification or add additional context in comments.

7 Comments

@mattis: Its not only database updates, I am calling few external API's too.
So start a transaction. Do the db update. If it succeeds continue with the external apis. If they succeed, commit the transaction. Otherwise rollback and display error to the user.
Thanks, So you are saying I should update the flags in database if items are available and then trying to buy it by hitting external API, So that in case if there is another request for that it will show that item unavailable unless transaction is rollback by some error. Please correct me If I misunderstood.
I have one doubt here, if transaction is not commit which will be done in last it will show that item available to other requests even if I will save the changes.
@chup sorry, but that sentence makes no sense to me. A transaction can't fail when you commit it. If you choose to rollback you should rollback any other relevant parts too. If you dont ever want to rollback, but just change to a third state, you dont need the surrounding transaction.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.