I have a /cancel_reservation
endpoint which takes a reservationId and needs authentication. Inside this method the reservation is cancelled and a refund is issued. Now if the same users calls this method twice concurrently, race conditions might occur and the user could be refunded twice.
For now I solved this issue by having a static Set
which contains locked reservation ids and the method first checks that the reservation id is not locked. But I reckon there are better ways to solve this?
edit: Reservations are saved on Couchbase but I'd rather not use db optimistic/pessimistic locking in order to become db independent. Also I doubt optimistic locking will ever help in this scenario.
Also the flow of this method goes like this:
Fetch reservation ⮞ Regular checks ⮞ Cancel reservation with a 3rd party service ⮞ Refund the user ⮞ Update reservation status+other data ⮞ Persist reservation