Transactions and ACID: Why Bank Transfers Break
Understand the idea
Why bank transfers fail without atomicity, and how ACID guarantees keep data consistent.
The idea
In real applications, one user action often requires multiple database steps. For example, transferring $100 from Alice to Bob requires two steps: subtracting $100 from Alice's account, and adding $100 to Bob's account. If the server loses power or crashes halfway through, $100 disappears from Alice without ever reaching Bob.
Databases solve this using transactions. A transaction bundles multiple steps into one all-or-nothing package:
- Atomicity: All steps succeed, or the entire transaction is rolled back with zero changes.
- Consistency: Data rules and foreign keys must be valid before and after the transaction.
- Isolation: Other users cannot see half-finished, uncommitted steps.
- Durability: Once committed, changes are written to the so they survive crashes.
In SQL, you start with BEGIN TRANSACTION, run your updates, and finish with COMMIT to save, or ROLLBACK to cancel everything if something goes wrong.
What changes
| Guarantee | Meaning | Failure Prevented Without It |
|---|---|---|
| Atomicity | All operations succeed or all are rolled back | Partial execution: money debited from sender but never credited |
| Consistency | Database state strictly obeys all constraints | Corrupted state: account balances violating minimum balance rules |
| Isolation | Concurrent transactions cannot see intermediate edits | Dirty reads: another user seeing intermediate uncommitted balances |
| Durability | Committed transactions survive power loss and crashes | Lost commits: confirmed purchases vanishing after a sudden reboot |
Transactions · all or nothing
Before BEGIN
Alice
$1,000
Bob
$500
Committed database: Alice $1,000 + Bob $500 = $1,500
Transfer $100 from Alice to Bob. The committed database starts at $1,000 and $500.
01 / 05Transactions · all or nothing
The command
Atomic bank transfer transaction
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
Run the example to see Alice's balance decrease by $100 and Bob's increase by $100 inside an atomic BEGIN/COMMIT transaction block.
Guided challenge
Simulate rollback on error
Update the function so it returns the unchanged senderBal when shouldRollback is true, and the debited balance when false.
Simulate rollback on error
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
1000 900
Hints 0/1
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.