01 / 12
A transfer is two UPDATE statements
Transfer 10,000 yen from A to B. Written in SQL, there are two things to do.
- Subtract 10000 from A balance
- Add 10000 to B balance
Now, what if the server went down just after the first finished? A balance is down and B balance has not gone up. Ten thousand yen has vanished from the world.
Each UPDATE on its own ran correctly. The problem is that the halfway state was left standing.
So you need a way to declare "these two go together, and nothing in between is shown to the outside". That is a transaction.
transfer = two updatestake 10000 from Agive 10000 to Bonly one succeeds -> 10000 gone