e commerce - Preventing race conditions for ecommerce application with MySQL -
i'm building ecommerce application mysql, i'm having hard time coming solution prevents following race condition:
two users checkout @ same time same item in cart. store has 1 item available sale. 1 user should able purchase last item, , other user should see error message because item out of stock.
i'm using item counter keep track of number of items in inventory, figure decrement item after processing user's credit card.
i know select...update
query in mysql, i'd stay away locking rows or tables - unless that's best way ecommerce app solve problem.
i'm interested in hearing other solutions other checking/decrementing item counter.
why worried locking. wont hurt till have 100s of simultaneous customers @ time , database engines innodb made handle things. wont able way kind of atomicity , workaround.
you need keep application level transaction management.
checkout reserver_the_items_in_cart if(reservation_succesfull){ get_the_payments_done_via_payment_gatway if(payment_successful) { update_the_reserverd_to sold_status }else{ make_reserved_item_to_available. } }else{ show_error_that_item_not_available. }
other best way handle never allow "show_error_that_item_not_available" happen. replenish inventory on time when started running out.
Comments
Post a Comment