In digital wallet systems like PhonePe or GPay, it is important that transactions are handled correctly. When money is transferred from one user to another, both operations — deducting from the sender and adding to the receiver — must happen together. If one step fails, the entire transaction should fail. This is known as atomicity.
To understand this, I used the accounts table with two users, Alice and Bob. Initially, their balances were 800 and 700.
First, I performed a successful transaction where 200 was transferred from Alice to Bob. Inside a transaction block, I deducted 200 from Alice and added 200 to Bob, and then committed the transaction. After this, the balances were updated correctly to 600 for Alice and 900 for Bob.
Next, I tested what happens when an error occurs during the transaction. I again started a transaction and deducted 200 from Alice. However, in the next step, I intentionally introduced an error in the update query for Bob. This caused the transaction to fail.
Even though the deduction step was executed first, the database did not apply any changes permanently. After the failure, I checked the balances again, and they remained unchanged at 800 and 700.
This shows that PostgreSQL does not allow partial updates in a transaction. Either all operations are completed successfully, or none of them are applied.
From this experiment, it is clear that atomicity ensures reliability in financial systems. It prevents situations where money is deducted from one account but not added to another, maintaining correct and consistent data at all times.
Top comments (0)