Skip to content
Dev Dump

ACID Properties

Either every statement in the transaction succeeds or the DB rolls back to its previous state.

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

If any statement fails, the DBMS issues a ROLLBACK and none of the updates persist.

The transaction must bring the database from one valid state to another, respecting constraints and business rules.

BEGIN TRANSACTION;
IF (SELECT balance FROM accounts WHERE id = 1) >= 100 THEN
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
ELSE
ROLLBACK;
END IF;

Once committed, the effects survive power loss or crashes. DBMSes journal/redo logs to ensure committed data can be replayed after restarts.

Concurrent transactions should not interfere. Isolation levels balance data freshness vs. anomalies.

Isolation anomalies

LevelGuaranteesAllowsTypical Use
Read UncommittedMinimalDirty readsRare; debugging
Read CommittedNo dirty readsNon-repeatable reads, phantom readsDefault in many RDBMS
Repeatable ReadStable rows within txnPhantom readsFinancial/reporting
SerializableFull serial equivalenceReduced concurrencyStrict correctness
  • Repeatable Read
    SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
    BEGIN;
    SELECT stock FROM products WHERE id = 1; -- locks row
    SELECT stock FROM products WHERE id = 1; -- same result
    COMMIT;
  • Read Committed
    SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
    BEGIN;
    SELECT stock FROM products WHERE id = 1; -- latest committed value
    -- Another transaction commits an update here
    SELECT stock FROM products WHERE id = 1; -- sees new value
    COMMIT;
  • Read Uncommitted
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
    BEGIN;
    SELECT stock FROM products WHERE id = 1; -- may read uncommitted changes
    COMMIT;
  • Serializable
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
    BEGIN;
    SELECT stock FROM products WHERE id = 1; -- blocks concurrent writers
    UPDATE products SET stock = stock - 1 WHERE id = 1;
    COMMIT;
PropertyTaglineEnforced By
AtomicityAll-or-nothingLogging, undo segments
ConsistencyValid states onlyConstraints, application logic
IsolationControlled concurrencyLocks, MVCC, isolation levels
DurabilitySurvives crashesWrite-ahead logging, replication