Scenario: web based text game
- Player A sends request to send 10,000 credits to player B (this request takes 20ms to complete).
- 3ms after sending first request (which is still being processed) player A sends another request to battle player C, this request takes 100ms to complete.
What’s happening, in step 1 an app removes credits from player A, gives them to player B, but before this is persisted in the db, second request comes, loads state still showing 10k in an account of player A and overwrites correct state from request 1 (0 credits) with 10k + battle loot from step 2. Result: player A and B both have now 10k in their accounts, the money just doubled.
What can be done to protect against this? This is hypothetical scenario to show the problem, for above scenario account=account+loot sql would do, but I’m looking for solution that works with all data types.
If you set the
transaction isolation levelon your database to serializable, you can treat the above as a single atomic occurrence at the database level.Atomicity is one of the key components of a database system.