A bank transfer that can't be half-done
Day 48 of 149
๐ Full deep-dive with code examples
The Bank Transfer
You transfer some money from savings to checking.
What if it crashed halfway?
- Savings: -$100 โ
- Checking: +$0 โ
- The transfer is now inconsistent ๐ฑ
Transactions prevent this!
All or Nothing
A transaction groups operations:
START TRANSACTION
1. Take money from savings
2. Add money to checking
COMMIT (make permanent)
If anything fails โ ROLLBACK (undo everything!)
Either BOTH happen or NEITHER happens.
The ACID Rules
| Letter | Meaning | Example |
|---|---|---|
| A | Atomic | All or nothing |
| C | Consistent | Constraints/invariants stay true |
| I | Isolated | Others don't see half-done state |
| D | Durable | Once committed, it survives crashes |
Real Example
BEGIN TRANSACTION;
UPDATE savings SET balance = balance - 100;
UPDATE checking SET balance = balance + 100;
COMMIT; -- Make it permanent
-- or ROLLBACK; if something went wrong
In One Sentence
Transactions ensure multiple database operations either all succeed together or all fail together, avoiding partially-applied state.
๐ Enjoying these? Follow for daily ELI5 explanations!
Making complex tech concepts simple, one day at a time.
Top comments (0)