12. What is ACID?
What is ACID?
ACID stands for:
- Atomicity:- All or nothing
- Consistency:- Data stays valid and rules are followed
- Isolation:- Transactions don’t interfere with each other
- Durability:- Committed data stays, even after failure
These four properties ensure that transactions in databases behave correctly, even if something goes wrong like a crash or failure.
1. Atomicity – All or Nothing
- A transaction is a single unit of work.
- Either everything inside the transaction happens, or nothing happens.
- If one step fails, the entire transaction is rolled back.
Example:
- Transfer $100 from A to B
- 1. Subtract $100 from Account A ✅
- 2. Add $100 to Account B ❌ (fails)
Rollback the subtraction too. No changes happen at all.
2. Consistency – Valid State
- After a transaction, the data must be in a valid state, following all rules and constraints (like foreign keys, uniqueness).
- The database moves from one valid state to another.
Example:
You can't insert a user with a duplicate email if there's a unique constraint on email. That keeps data consistent.
3. Isolation – No Conflict Between Transactions
- Multiple transactions can happen at the same time, but they shouldn’t affect each other.
- The database ensures intermediate results are not visible to other running transactions.
Example:
If two users try to book the last movie seat at the same time, isolation ensures only one of them gets it.
There are levels of isolation (READ\_COMMITTED, REPEATABLE\_READ, SERIALIZABLE) that control how strict this is.
4. Durability – Once Committed, Always Saved
- Once a transaction is committed, the changes are permanent, even if there's a power failure or crash.
- This is achieved using logs (like redo logs) and disk writing mechanisms.
Example:
You make a payment, and the system crashes. Since the transaction was committed, your payment is not lost.
Comments
Post a Comment