Transactions & ACID
A transaction is a unit of work that either happens completely or not at all. ACID is the promise that keeps a database trustworthy.
In plain English
A bank transfer is all-or-nothing: the money leaves one account and arrives in the other, even after a crash.
The one idea
A transaction is an all-or-nothing unit that moves the database from one consistent state to another.
Money, inventory, and bookings are not allowed to be half-updated. ACID is the promise that a crash or a concurrent reader cannot leave a partial fact behind.
By the end
- 01Define a transaction and its boundaries.
- 02Explain atomicity, consistency, isolation, and durability.
- 03Contrast commit and rollback.
- 04See ACID in a bank-transfer example.
What a transaction is
A transaction groups several operations into one logical unit. It begins with a start marker and ends with COMMIT or ROLLBACK. The DBMS must make the whole group behave as if it ran alone and either fully succeeded or never happened.
BEGIN;
UPDATE Account SET balance = balance - 100 WHERE id = 1;
UPDATE Account SET balance = balance + 100 WHERE id = 2;
COMMIT;ACID
Atomicity: all operations succeed or none do. Consistency: the transaction moves the database from one valid state to another. Isolation: concurrent transactions do not see each other's uncommitted work. Durability: once committed, the change survives a crash. Each property needs a different mechanism: logging, constraints, concurrency control, and stable storage.
Commit and rollback
COMMIT makes the transaction durable and visible. ROLLBACK undoes its changes. A transaction may also abort automatically on a constraint violation, deadlock, or crash. The recovery system uses the log to undo uncommitted work and redo committed work.
Move 100 from A to B
Account A has 500 and account B has 300. A transfer debits A and credits B.
Debit A by 100.
A becomes 400 inside the transaction, not yet durable.
Crash before crediting B.
Recovery rolls back the debit; A returns to 500.
Retry and commit both writes.
A is 400, B is 400, and the total is unchanged.
ConclusionAtomicity protects the pair of writes. Consistency protects the balance rule. Isolation hides the intermediate state. Durability survives the crash.
The trade-off
No free correctness.
Gain
A correctness boundary that applications can rely on.
Cost
Locks, logging, fsync, and coordination all cost throughput.
Verdict
Use transactions for any multi-step invariant; relax isolation only with a measured anomaly budget.
Recap
What to keep.
Takeaways
- +A transaction is an all-or-nothing unit of work.
- +Atomicity is all or nothing; consistency preserves invariants.
- +Isolation hides uncommitted changes; durability survives crashes.
- +COMMIT makes changes permanent; ROLLBACK undoes them.
- +Recovery uses logs to redo and undo.
Traps
- !Leaving a transaction open and holding locks.
- !Assuming COMMIT is free; it forces durability work.
- !Confusing consistency with isolation.
Focus
- →Explain each ACID property with a bank transfer.
- →Know what COMMIT and ROLLBACK do.
- →Name the mechanism behind durability.
Check yourself
Before you turn the page.
Which ACID property ensures a committed change survives a crash?
What does ROLLBACK do?