Step by step
The example above, written out — the same steps the animation plays.
- 1Transfer ₹500: Asha → Ravi. Two updates must happen together: take ₹500 from Asha, give ₹500 to Ravi. The bank's total must stay ₹2,000.
- 2Debit Asha. Asha −₹500 — and with no transaction it is saved straight away.
- 3💥 The server crashes. Power cut before the second update ran.
- 4₹500 has vanished. Asha was debited, Ravi was never credited. Each statement was saved on its own, so the half-finished transfer stays. This is why money moves inside transactions.
- 5With a transaction: BEGIN. Start a transaction: until COMMIT, these changes are provisional — all of them happen, or none do.
- 6With a transaction: Debit Asha. Asha −₹500 — recorded in the transaction log, not yet committed.
- 7With a transaction: 💥 The server crashes. Power cut before the second update ran.
- 8With a transaction: Recovery: roll back. On restart the database finds a transaction that never committed and undoes it. Asha has her ₹1,000 back; nothing was half-done.
What's happening?
- A transfer needs two updates: take money from one account and add it to the other.
- Without a transaction, each update is saved on its own; a crash in between leaves the money half-moved.
- Inside a transaction, nothing is permanent until COMMIT. After a crash, the database rolls back anything not committed.
Where you'll meet it
Payments, bookings, stock levels, sign-ups that create several rows — anything where a half-done change would be wrong.
Common mistake
Calling external services (payment APIs, emails) inside a long transaction: locks are held while you wait, and a rollback cannot undo the email.
FAQ
What does ACID stand for?
Atomic (all or nothing), Consistent (rules still hold), Isolated (others don't see half-done work), Durable (committed means saved).
How does the database undo work after a crash?
It writes every change to a log before applying it. On restart it replays committed transactions and undoes the rest.
Do transactions prevent race conditions?
They help, but two transactions can still read the same old value. Locks (SELECT … FOR UPDATE) or constraints close the gap — see Race Condition.