Database ACID: The Four Promises
What you will learn
- What Atomicity, Consistency, Isolation and Durability mean
- How to see each promise at work
- Which promise protects against which problem
1. The four promises
ACID is the name for what a reliable database promises about transactions. Each letter is one promise:
| Letter | Promise | In one line |
|---|---|---|
| Atomicity | all or nothing | a transaction never leaves half its steps behind |
| Consistency | rules always hold | after every transaction, all the rules (keys, CHECK, NOT NULL...) are true |
| Isolation | transactions do not trip over each other | one transaction's unfinished work is not seen or damaged by others |
| Durability | committed means saved | once COMMIT succeeds, the data survives a crash or restart |
Atomicity and consistency are shown with the sample database you chose in lesson 4.1 (use the Example data picker at the top; changes stay in your browser's copy, and Reset data starts again). Isolation and durability need two separate connections acting as two users, so they use a small file database in Python (a tiny accounts table of its own, created here):
import os, tempfile, sqlite3
path = os.path.join(tempfile.mkdtemp(), "bank.db")
a = sqlite3.connect(path, isolation_level=None)
a.execute("CREATE TABLE accounts (name TEXT PRIMARY KEY, balance INTEGER NOT NULL CHECK (balance >= 0))")
a.execute("INSERT INTO accounts VALUES ('Rina', 500), ('Sam', 100)")
2. Atomicity
A transaction that is rolled back leaves nothing behind, and one that is committed keeps all its steps. Atomicity is what makes ROLLBACK possible.
BEGIN;
UPDATE accounts SET balance_cents = balance_cents - 20000 WHERE account_id = 5;
UPDATE accounts SET balance_cents = balance_cents + 20000 WHERE account_id = 6;
ROLLBACK;
SELECT account_id, balance_cents FROM accounts WHERE account_id IN (5, 6) ORDER BY account_id;
BEGIN;
UPDATE accounts SET balance_cents = balance_cents - 20000 WHERE account_id = 5;
UPDATE accounts SET balance_cents = balance_cents + 20000 WHERE account_id = 6;
COMMIT;
SELECT account_id, balance_cents FROM accounts WHERE account_id IN (5, 6) ORDER BY account_id;
account_id balance_cents
---------- -------------
5 153100
6 218400
(2 rows)
account_id balance_cents
---------- -------------
5 133100
6 238400
(2 rows)
The rolled-back transfer changed nothing; the committed one changed both accounts.
BEGIN;
INSERT INTO orders (order_id, customer_id, order_date, status, ship_city) VALUES (2601, 1, '2026-10-01', 'pending', 'Leeds');
INSERT INTO order_items (order_id, line_no, product_id, quantity, unit_price) VALUES (2601, 1, 2, 2, 44.1);
UPDATE products SET stock = stock - 2 WHERE product_id = 2;
ROLLBACK;
SELECT COUNT(*) AS lines FROM order_items WHERE order_id = 2601;
SELECT stock FROM products WHERE product_id = 2;
BEGIN;
INSERT INTO orders (order_id, customer_id, order_date, status, ship_city) VALUES (2601, 1, '2026-10-01', 'pending', 'Leeds');
INSERT INTO order_items (order_id, line_no, product_id, quantity, unit_price) VALUES (2601, 1, 2, 2, 44.1);
UPDATE products SET stock = stock - 2 WHERE product_id = 2;
COMMIT;
SELECT COUNT(*) AS lines FROM order_items WHERE order_id = 2601;
SELECT stock FROM products WHERE product_id = 2;
lines
-----
0
(1 row)
stock
-----
150
(1 row)
lines
-----
1
(1 row)
stock
-----
148
(1 row)
The rolled-back order changed nothing; the committed one has its line and the reduced stock.
BEGIN;
INSERT INTO enrollments (student_id, section_id, enrolled_on) VALUES (1, 31, '2026-01-20');
DELETE FROM enrollments WHERE student_id = 1 AND section_id = 30;
ROLLBACK;
SELECT student_id, section_id, grade FROM enrollments WHERE student_id = 1 ORDER BY section_id;
BEGIN;
INSERT INTO enrollments (student_id, section_id, enrolled_on) VALUES (1, 31, '2026-01-20');
DELETE FROM enrollments WHERE student_id = 1 AND section_id = 30;
COMMIT;
SELECT student_id, section_id, grade FROM enrollments WHERE student_id = 1 ORDER BY section_id;
student_id section_id grade
---------- ---------- -----
1 19 NULL
1 30 49.2
1 41 60.8
(3 rows)
student_id section_id grade
---------- ---------- -----
1 19 NULL
1 31 NULL
1 41 60.8
(3 rows)
The rolled-back move changed nothing; the committed one has both steps.
3. Consistency
The database refuses changes that break its rules, and the refused statement does not leave half a change behind. Each box below is meant to fail; read the message:
UPDATE accounts SET balance_cents = balance_cents - 300000 WHERE account_id = 5;
IntegrityError: CHECK constraint failed: balance_cents >= 0
INSERT INTO transfers (from_account, to_account, amount_cents, created_on, status) VALUES (5, 5, 100, '2026-10-01', 'done');
IntegrityError: CHECK constraint failed: from_account <> to_account
Account 5 does not have 3,000.00, so the CHECK (balance_cents >= 0) rule stops it; a transfer from an account to itself breaks another CHECK rule.
UPDATE products SET stock = stock - 100 WHERE product_id = 4;
IntegrityError: CHECK constraint failed: stock >= 0
UPDATE order_items SET quantity = 0 WHERE order_id = 1 AND line_no = 1;
IntegrityError: CHECK constraint failed: quantity > 0
Product 4 does not have 100 in stock, so CHECK (stock >= 0) stops it; a quantity of 0 breaks CHECK (quantity > 0).
UPDATE enrollments SET grade = 120 WHERE student_id = 1 AND section_id = 30;
(0 rows changed)
UPDATE courses SET credits = 9 WHERE code = 'MATH101';
IntegrityError: CHECK constraint failed: credits BETWEEN 1 AND 6
A grade of 120 breaks CHECK (grade BETWEEN 0 AND 100); 9 credits break CHECK (credits BETWEEN 1 AND 6).
Notice that you define what "consistent" means, through keys, NOT NULL, CHECK and foreign keys (chapters 2 and 3). The database then guarantees those rules hold after each transaction. It cannot know a rule you never wrote down.
4. Isolation
While connection a is in the middle of a transfer, a second connection b looks at the table:
a.execute("BEGIN")
a.execute("UPDATE accounts SET balance = balance - 200 WHERE name = 'Rina'")
b = sqlite3.connect(path)
print("other connection sees:", b.execute("SELECT * FROM accounts ORDER BY name").fetchall())
a.execute("COMMIT")
print("after commit it sees:", b.execute("SELECT * FROM accounts ORDER BY name").fetchall())
other connection sees: [('Rina', 500), ('Sam', 100)]
after commit it sees: [('Rina', 300), ('Sam', 100)]
Connection b did not see the half-finished change; it saw the old balances until a committed. (The Sam row still shows 100 because this demo only ran the first half of a transfer.) How strict isolation is, and what happens when two transactions touch the same data, is the topic of lessons 6.4 to 6.6.
5. Durability
After the commit, close both connections and open the file again as if the program had been restarted:
a.close()
b.close()
print("reopened:", sqlite3.connect(path).execute("SELECT * FROM accounts ORDER BY name").fetchall())
reopened: [('Rina', 300), ('Sam', 100)]
The committed change is on disk. Databases get this by writing to a log or journal and flushing it to disk before COMMIT returns. The default in SQLite for how it does this is visible with pragmas:
print(sqlite3.connect(path).execute("PRAGMA journal_mode").fetchone(), sqlite3.connect(path).execute("PRAGMA synchronous").fetchone())
('delete',) (2,)
journal_mode is delete (the rollback journal is kept while a transaction runs and removed afterwards). synchronous is 2, which SQLite calls FULL: it waits for the data to reach the disk. Turning such safety settings down makes a database faster but weakens durability, which is a trade-off to make with open eyes.
Worked example: which promise failed?
| Story | Broken promise |
|---|---|
| A transfer crashed after subtracting but before adding, and the money was gone. | Atomicity |
| An order was saved with a quantity of -5. | Consistency (the rule was missing or not enforced) |
| A report showed Rina's balance while a transfer was half done. | Isolation |
| The shop confirmed an order, the server lost power, and the order was gone. | Durability |
The stories are made up.
Common mistakes
- Thinking ACID makes your data correct by itself. Consistency only enforces the rules you wrote.
- Thinking "isolated" means "never interacts". Levels of isolation differ (lesson 6.5).
- Assuming every database is ACID in every mode. Check what your database promises and which settings you changed.
- Confusing the "C" with the C in other acronyms (such as the CAP theorem, lesson 14.5, which is about distributed systems); they are different ideas.
Key takeaways
- ACID = Atomicity, Consistency, Isolation, Durability.
- Atomicity: all or nothing. Consistency: the rules you defined always hold.
- Isolation: unfinished work of one transaction is kept from others.
- Durability: after
COMMITreturns, the data survives a crash.
Practice
1. Write the four words behind ACID.
Show answer
Atomicity, Consistency, Isolation, Durability.
2. Which promise does ROLLBACK rely on?
Show answer
Atomicity.
3. A CHECK (price >= 0) rule rejects a negative price. Which promise is that?
Show answer
Consistency.
4. In the lesson, why did connection b still see Rina's old balance of 500?
Show answer
Connection a's change was not committed yet, and isolation keeps unfinished work invisible to others.
5. Why does turning off disk flushing speed up a database but make it riskier?
Show answer
Waiting for data to reach the disk takes time. Without it, a crash or power cut could lose changes the database already reported as committed, which breaks durability.
6. Can a database be consistent if you never defined any rules?
Show answer
It cannot protect you. Consistency means the rules you defined hold; with no rules, nothing stops wrong data.