ACID Transactions Explained
The guarantees that keep a multi-step write from leaving a database in a half-finished, contradictory state.
Beginner
| Letter | Property | What it guarantees |
|---|---|---|
| A | Atomicity | All the writes in a transaction succeed, or none of them do — there's no partial state where the charge happened but the order didn't get created. |
| C | Consistency | A transaction only ever moves the database from one valid state to another — it can't leave data violating a defined rule, like an order referencing a restaurant ID that doesn't exist. |
| I | Isolation | Concurrent transactions don't see each other's half-finished work. How much more than that you get depends on the isolation level: "as if they ran one at a time" is specifically the SERIALIZABLE guarantee, not what every level provides. |
| D | Durability | Once a transaction is confirmed committed, it survives a crash immediately after — it's on stable storage, not just in memory. |
BEGIN TRANSACTION;
UPDATE customers SET balance = balance - 24.50 WHERE id = 910;
UPDATE restaurant_inventory SET qty = qty - 1 WHERE item_id = 'pz_104';
INSERT INTO orders (customer_id, item_id, total) VALUES (910, 'pz_104', 24.50);
COMMIT;
qty = 1) at nearly the same instant. Without isolation, both transactions could read qty = 1, both decide the purchase is valid, and both commit — leaving qty = -1 and two customers who paid for food that doesn't exist.SERIALIZABLE, the two transactions are forced to behave as if one ran completely before the other, and one of the orders is correctly rejected. Under the more common default of READ COMMITTED, the read-then-decide-then-write sequence above is not prevented: both transactions can read qty = 1 before either writes.UPDATE inventory SET qty = qty - 1 WHERE id = 42 AND qty > 0 — makes the second transaction block on the row lock and then re-evaluate against the updated value, so it matches zero rows and the order is rejected. Explicitly locking the row on read (SELECT ... FOR UPDATE) achieves the same by serializing the two transactions on that row. Underneath both sits locking or multi-version concurrency control (MVCC).