Let's say you have a table that keeps track of your inventory, with columns product ID, and stock available. Your client-side application is responsible for reducing the stock count as soon as there is a purchase.
Now, imagine if 2 client applications are connected to your server, load a particular product info, and try to update the stock of the same product at the same time.
Given, that the original stock is 15.
Client #1, product #1, stock available 10 -> because client #1 sold 5 items of the product.
Client #2, product #1, stock available 14 -> because client #2 sold 1 item of the product.
Now, depending on whose request went first, the updation will happen. If client #1 hits the update button first, the sequence would be.
update stock from 15 to 10 -> via client #1
update stock from 10 to 14 -> via client #2
But what should be the actual stock remaining? It has to be 15 - (5 +1), which is 9. But the value on your database is 14!
Such challenges would come up when you work with systems at scale, and the operations happen concurrently. How would you solve this?
I will post an article tomorrow, where we will go through the same scenario and solve it using optimistic locking in Amazon DynamoDB and .NET. This is a very interesting topic, and I have come across this same use case, at work as well.
Stay Tuned!
You can follow me on Linkedin: https://www.linkedin.com/in/iammukeshm/