What is ACID?
ACID is a set of 4 rules that make databases reliable. Every serious database follows these rules.
- A - Atomicity
- C - Consistency
- I - Isolation
- D - Durability
Think of ACID like safety rules at a bank. They protect your money (data) from mistakes and problems.
How ACID Works
A - Atomicity
What it means: A transaction must complete fully or not at all. There is no middle state.
Real-life example: Sending money to a friend
Imagine you transfer 100 dollars to your friend. Two things must happen:
- Your account loses 100 dollars
- Your friend's account gains 100 dollars
What if step 1 works but step 2 fails? You would lose money and your friend would get nothing. That is a disaster.
Atomicity prevents this. If step 2 fails, step 1 is automatically reversed. Either both happen or neither happens.
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE user_id = 1;
UPDATE accounts SET balance = balance + 100 WHERE user_id = 2;
COMMIT;If the second UPDATE fails, the first UPDATE is cancelled. Your money is safe.
C - Consistency
What it means: Data must always follow the rules you set. The database checks these rules before saving.
Real-life example: Age must be realistic
You create a rule that age must be between 0 and 120. The database will reject any data that breaks this rule.
CREATE TABLE users (
name VARCHAR(100),
age INTEGER CHECK (age >= 0 AND age <= 120)
);
INSERT INTO users (name, age) VALUES ('John', 25); -- Works
INSERT INTO users (name, age) VALUES ('Jane', -5); -- Rejected
INSERT INTO users (name, age) VALUES ('Bob', 150); -- RejectedThe database moves from one valid state to another valid state. It never allows invalid data.
I - Isolation
What it means: Multiple users can work on the database at the same time without affecting each other.
Real-life example: Two people buying the last ticket
Imagine there is 1 concert ticket left. Two people try to buy it at the exact same moment.
Without isolation:
- Person A sees 1 ticket available
- Person B sees 1 ticket available
- Both click buy
- Both get the ticket (but there was only 1)
With isolation:
- Person A starts buying
- Person B must wait
- Person A completes the purchase
- Person B sees 0 tickets available
The database handles this automatically. It makes sure transactions do not interfere with each other.
D - Durability
What it means: Once you save data, it stays saved. Even if the power goes out or the server crashes, your data is still there.
Real-life example: Saving a document
When you click Save on a document, you expect it to still be there tomorrow. Even if your computer crashes right after saving.
Databases work the same way. Once a transaction is committed, the data is written to permanent storage.
INSERT INTO orders (customer, total) VALUES ('John', 250);
COMMIT;
-- Server crashes here
-- When server restarts, this order is still in the databaseThe database uses special techniques like write-ahead logging to ensure data survives crashes.
Why ACID Matters
Without ACID, bad things happen:
Without Atomicity: Money disappears during transfers Without Consistency: Invalid data enters your database Without Isolation: Users overwrite each other's work Without Durability: Data is lost after crashes
ACID is especially important for:
- Banking and financial systems
- E-commerce and order processing
- Healthcare records
- Any system where data accuracy matters
Summary
ACID keeps your database safe and reliable:
- Atomicity: Transactions complete fully or not at all
- Consistency: Data always follows your rules
- Isolation: Multiple users do not interfere with each other
- Durability: Saved data stays saved forever
These 4 properties are why you can trust databases with important data like money, medical records, and business transactions.