Databases are the backbone of modern applications, and ensuring data reliability is crucial. The ACID properties (Atomicity, Consistency, Isolation, and Durability) define a set of rules that guarantee reliable database transactions.
Atomicity
Definition
Atomicity ensures that a transaction is treated as a single unit of work. Either all operations in the transaction are completed successfully, or none are applied.
Example ✅
Suppose a bank transaction transfers $100 from Alice to Bob. The transaction involves:
- Subtract $100 from Alice's account.
- Add $100 to Bob's account.
If the database crashes after subtracting money from Alice but before adding it to Bob, the entire transaction is rolled back. Neither account is affected partially.
Example ❌
In a poorly implemented system, if the money is deducted from Alice’s account but the system crashes before adding it to Bob’s account, Alice loses $100, and Bob doesn’t get it. This violates atomicity.
Consistency
Definition
Consistency ensures that a transaction transforms the database from one valid state to another according to all defined rules, constraints, and triggers.
Example ✅
A database enforces that every order must have a valid customer ID. When a new order is inserted with an existing customer ID, the database remains consistent.
Example ❌
If an order is inserted with a non-existent customer ID or a negative product quantity is stored, the database state is invalid, violating consistency.
Isolation
Definition
Isolation ensures that concurrent transactions do not interfere with each other. Each transaction should execute as if it were the only one in the system.
Example ✅
Two users simultaneously book seats on a flight. Proper isolation prevents double-booking by ensuring one transaction completes before the other modifies the seat availability.
Example ❌
Without proper isolation, two transactions might read the same available seat and both book it, causing double-booking. This is called a race condition.
Durability
Definition
Durability ensures that once a transaction is committed, it will survive system crashes or failures. The results are permanently stored.
Example ✅
After a transaction commits a payment, even if the server crashes immediately afterward, the payment record remains in the database.
Example ❌
If a database keeps transactions in memory and a crash occurs before writing to disk, the committed transaction is lost. This violates durability.