CSE Roadmap

Core tier · about 2 hours · 5 parts

Part 2 of 5 · 1 min read

Atomicity

A transaction's effects happen entirely, or not at all; there is no partial result.

Think of a bank transfer that must both subtract money from one account and add it to another. If only the subtraction happened before a failure, money would simply vanish.

In plain words: atomicity guarantees that a transaction's statements either all take effect together, or none of them take effect at all. If a transaction is interrupted partway through, by a crash or an explicit rollback, any changes it already made are undone, leaving the database as if that transaction had never started. This is distinct from isolation, covered below, which concerns what other transactions can see while a transaction is still running, not whether it finishes completely or not at all.

A worked example. A transaction that debits $50 from one account and credits it to another either completes both the debit and the credit together, or, if interrupted after only the debit, has that debit undone as well, leaving both balances unchanged.

The key lines.

BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 50 WHERE account_id = 2;
COMMIT;
-- atomicity: both updates happen, or neither does

Atomicity

Atomicity guarantees that a transaction's statements either all take effect together or none take effect at all, with any partial changes undone if the transaction does not complete.

Example: a bank transfer where an interrupted debit is undone rather than left standing alone is atomicity in action.