How amazon will handle concurrency write issues like when multiple users try to buy the same product whose available quantity is only one.
Considering the "PRODUCT" table has below fields :

I am looking for little detailed explanation in terms of database transaction and locking or is any other way to handle this situation in more detailed technical terms.
Lets say when 2 users have added the same product in their cart and one user proceed to PAY :
a)
BEGIN TRANSACTION
SELECT * FROM PRODUCT WHERE ID = 1 AND STATUS = "AVAILABLE" FOR UPDATE;
update the quantity field to "quantity - 1" and (if quantity = 0 then status = "SOLD OUT") in product table
Call the payment gateway
If PAYMENT TIMEOUT ROLLBACK;
COMMIT
END TRANSACTION
===========================================================================
1) While still above transaction is going on, other users can still see the PRODUCT available as it is still not committed and they may also proceed to PAY.
2) Also, if the sufficient quantity of product is available, taking a lock on the "product" table row may stop other users trying to buy the same product at same time.
===========================================================================
HOW to handle this issues ??
I have read the below blog of using optimistic locking on the database but finding it difficult to understand in depth of how exactly to achieve.
http://blog.gainlo.co/index.php/2016/08/28/design-ecommerce-website-part-ii/