Transactions & ACID

Group several operations into one all-or-nothing unit, so a failure never leaves data half-changed.

What is it?

A money transfer is two operations: subtract $100 from Account A, then add $100 to Account B. If the database crashes or the connection drops between them, $100 just vanishes.

A transaction groups operations into one all-or-nothing unit. Either every operation succeeds and is saved, or if anything fails, everything is rolled back as if it never happened. There's no half-applied state.

Transaction guarantees are summed up as ACID:

- Atomicity: all or nothing. - Consistency: data always follows the rules you've defined, like foreign keys. - Isolation: transactions running at the same time don't see each other's half-finished work. - Durability: once confirmed, a transaction survives even a crash right afterward.

Explain like I'm 10

A transaction is like sealing several steps in one envelope: either the whole envelope is delivered as sealed, or it's never sent. Nobody gets half an envelope.

Examples

A money transfer as a transaction

BEGIN;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

COMMIT;

Both updates sit between BEGIN and COMMIT. If anything fails before COMMIT, the database rolls it all back and Account 1's balance is restored.

Rolling back on purpose

BEGIN;

UPDATE inventory SET quantity = quantity - 1 WHERE product_id = 42;
-- Suppose application code checks the new quantity and finds it went negative:
ROLLBACK;

ROLLBACK undoes every change since BEGIN, as if the transaction never happened. Useful when app logic spots a problem partway through.

How it works

Inside a transaction, the database tracks every change but doesn't make it visible or permanent to anyone else yet. COMMIT makes all the changes permanent at once. ROLLBACK discards them all, restoring the data to how it was before BEGIN.

For isolation, the database controls how much (and when) one transaction's in-progress changes are visible to others running at the same time, so two simultaneous transfers don't corrupt each other's math.

Why does it exist?

Many real operations are several steps that only make sense together. Without transactions, a crash, network drop, or bug partway through could leave data in a state no correct sequence would produce: money debited but never credited, or an order marked paid with no payment recorded. Transactions make "partly done" impossible.

When to use it

Use a transaction whenever several statements must succeed or fail together to keep data correct: money transfers, placing an order that also reduces inventory, creating a user account along with its settings row.

When not to use it

A single statement doesn't need an explicit transaction; most databases already treat each statement as its own transaction. Also avoid keeping a transaction open for long (e.g. across a slow external API call), since it can block other transactions.

Common mistakes

  • Forgetting to COMMIT, so the changes are eventually rolled back or lost.

  • Doing slow, unrelated work (like calling an external API) inside an open transaction, blocking other operations waiting on the same rows.

  • Assuming a crash mid-transaction leaves partial changes. Properly used transactions guarantee it doesn't.

Practice exercises

  1. Easy:

    In your own words, explain what would go wrong with a money transfer that isn't in a transaction if the app crashed halfway.

  2. Medium:

    Write a transaction that inserts an order row and reduces a product's stock quantity, so both happen or neither does.

  3. Hard:

    Explain each of the four ACID properties in your own words, using the money transfer example for each.

Interview questions

What does a database transaction guarantee?

A group of operations either all succeed and become permanent, or, if any part fails, none take effect. There's no partly applied state.

What does the 'A' in ACID stand for, and what does it mean?

Atomicity: the transaction's operations form one indivisible unit. Either all apply or none do.

What is the difference between COMMIT and ROLLBACK?

COMMIT makes every change in the transaction permanent. ROLLBACK discards every change since the transaction began, as if it never happened.