Let's say there is a user table and has field called coins
to represent the coins available for that user in the system. At this stage a certain user has 50 coins
in his account. Same user do the following requests almost instantly, but in the following order.
- Buy something for 20 coins
- Insert a promo code that doubles the coins
If the 1st request fails, in a properly written monolithic application, the user will end up with 100 coins
.
But in a microservices based application where users are maintained in the separate microservice with a separate database and orders are maintained in a separate microservice with a separate database which utilize SAGA pattern to handle transactions, the following scenario can happen.
- At some stage of the SAGA,
20 coins
will be removed from the account. Now user has30 coins
. - Promo code will be applied. Now user has
60 coins
. - At later stage of the SAGA, buying the item fails. So it initiate a rollback.
- User will be given
20 coins
at a stage of the rollback.
At the end, user will own only 80 coins
, which is wrong.
Is there any way to fix this? Any suggestion?