Let us assume the state (the account balance) is stored in a relational database. The state machine examines each command one by one in FIFO order. For each command, it checks whether the account has a sufficient balance. If yes, the state machine generates an event for each account. For example, if the command is “A->
1−C”,thestatemachinegeneratestwoevents:“A:−1” and “C:+$1”.
Figure 17 shows how the state machine works in 5 steps.
Read commands from the command queue.
Read balance state from the database.
Validate the command. If it is valid, generate two events for each of the accounts.
Read the next Event.
Apply the Event by updating the balance in the database.
https://bytebytego.com/courses/system-design-interview/digital-wallet