Today I worked on a wallet system like GPay or PhonePe, where users can send money to each other. The main focus was Atomicity from ACID properties.
Atomicity means “all or nothing”.
If a transaction fails in between, everything should go back to the original state.
Transaction
I wrote a transaction like this
BEGIN;
UPDATE accounts
SET balance = balance - 200
WHERE name = 'Alice';
UPDATE accounts
SET balance = balance + 200
WHERE name = 'Bob';
COMMIT;
Now Testing Failure
I intentionally broke the second query (credit to Bob).
BEGIN;
UPDATE accounts
SET balance = balance - 200
WHERE name = 'Alice';
UPDATE accounts
SET balancee = balance + 200
WHERE name = 'Bob';
COMMIT;
BEGIN - start transaction
Do all operations
If success - COMMIT
If error - ROLLBACK
Atomicity ensures no partial transactions, which is very important in payment systems.
Top comments (0)