Imagine you’re transferring €500 from your savings to checking account…
ACID ensures your data stays consistent and reliable
-- Monday 9:00 AM
UPDATE accounts SET balance = balance - 500 WHERE id = 'savings';
-- 💥 SYSTEM CRASH HERE 💥
UPDATE accounts SET balance = balance + 500 WHERE id = 'checking';
Result: €500 has vanished into the void 😱
A transaction is like jumping a canyon:
BEGIN TRANSACTION;
UPDATE inventory SET quantity = quantity - 1 WHERE product = 'PS5';
UPDATE orders SET status = 'confirmed' WHERE order_id = 123;
INSERT INTO shipping (order_id, address) VALUES (123, '...');
COMMIT; -- All 3 succeed, or all 3 fail
Spotify adding a song to your playlist:
BEGIN TRANSACTION;
INSERT INTO playlist_songs (playlist_id, song_id) VALUES (42, 999);
UPDATE playlists SET song_count = song_count + 1 WHERE id = 42;
UPDATE playlists SET updated_at = NOW() WHERE id = 42;
COMMIT;
If ANY step fails → entire operation rolls back → playlist unchanged
Your database has rules (constraints), and consistency ensures they’re ALWAYS true:
After every transaction, all rules still hold
-- Rule: Account balance >= 0
CREATE TABLE accounts (
id INTEGER PRIMARY KEY,
balance DECIMAL CHECK (balance >= 0)
);
-- This transaction will fail completely (atomicity + consistency)
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 1000 WHERE id = 1;
-- If balance would go negative, ENTIRE transaction fails
COMMIT;
The database is your bodyguard against bad data
Multiple users, zero interference:
User A and User B both click “Buy” on the last concert ticket
-- User A's transaction (10:00:00.000)
UPDATE tickets SET sold = true WHERE seat = 'A1' AND sold = false;
-- User B's transaction (10:00:00.001)
UPDATE tickets SET sold = true WHERE seat = 'A1' AND sold = false;
Result: Only ONE succeeds. The other gets “Sorry, sold out!”
Higher isolation = Safer but slower
Without isolation:
-- Current likes: 1000
-- User A reads: 1000
SELECT likes FROM posts WHERE id = 99;
-- User B reads: 1000
SELECT likes FROM posts WHERE id = 99;
-- User A updates: 1001
UPDATE posts SET likes = 1001 WHERE id = 99;
-- User B updates: 1001 (should be 1002!)
UPDATE posts SET likes = 1001 WHERE id = 99;
Lost update! One like vanished 👻
Once you get “Transaction Committed” → It survives:
How? Write-Ahead Logging (WAL)
INSERT INTO orders (customer, total) VALUES ('Alice', 99.99);
-- "Transaction Committed" ✅
-- 💥 POWER OUTAGE 💥
-- System restarts...
SELECT * FROM orders WHERE customer = 'Alice';
-- Order is still there!
Your committed data is sacred
| Database | ACID? | Notes |
|---|---|---|
| PostgreSQL | ✅ Full ACID | Gold standard |
| MySQL (InnoDB) | ✅ Full ACID | Default engine |
| SQLite | ✅ Full ACID | Even on your phone! |
| MongoDB | ⚠️ Partial | ACID for single documents, multi-doc since v4 |
| Redis | ❌ No | Speed over safety |
| Cassandra | ❌ No | Eventual consistency |
Nothing is free:
But for financial, medical, or critical data: ACID is non-negotiable
Sometimes you trade ACID for:
BASE (Basically Available, Soft state, Eventual consistency) = The rebellious cousin of ACID
Which ACID property is violated?
When in doubt, choose ACID (You can optimize later, but you can’t recover lost data)