Im having an issue with an API and I am lost how to correct it.
I have two models "users" and "charity".
Users
- id
- balance
Charity
- id
- balance
In simple form, the API takes a request and deducts from users->balance and increases charity->balance
Now, everything works fine except when a request is spammed:
Working Order:
- User Balance | Charity Balance
- 5 1
- 4 2
- 3 3
- 2 4
Spammed Order
- User Balance | Charity Balance
- 5 1
- 5 2
- 4 3
- 4 4
As you can see, the data integrity is corrupted because the database cannot catch up before the next request is processed.
Im running out of luck trying to solve this issue. I've tried transactions, separate statements etc.
Im not sure what to try.