ACID
Definition
Section titled “Definition”ACID is a set of properties of database transactions intended to guarantee data validity despite errors, power failures, and other mishaps. In the context of databases, a sequence of database operations that satisfies the ACID properties is called a transaction.
~ Wikipedia
The concept
Section titled “The concept”As full-stack developers, we often treat the database as a black box that just “stores stuff”. But when building critical applications—like e-commerce platforms, banking apps, or booking systems—understanding how the database ensures data integrity is crucial.
ACID stands for Atomicity, Consistency, Isolation, and Durability. These principles ensure that your database transactions are processed reliably.
Examples
Section titled “Examples”Atomicity (All or Nothing)
Section titled “Atomicity (All or Nothing)”The Concept: A transaction is treated as a single unit. Either all of its operations are executed, or none of them are. There is no “half-done” state.
The Example: Imagine a money transfer feature in your banking app. You need to:
- Subtract $100 from Alice’s account.
- Add $100 to Bob’s account.
If step 1 succeeds but step 2 fails (e.g., server crash), Alice loses money and Bob gets nothing. Atomicity ensures that if step 2 fails, step 1 is rolled back.
BEGIN; UPDATE accounts SET balance = balance - 100 WHERE user_id = 'Alice'; -- System crash here? No problem. The change above is rolled back. UPDATE accounts SET balance = balance + 100 WHERE user_id = 'Bob';COMMIT;Consistency (Follow the Rules)
Section titled “Consistency (Follow the Rules)”The Concept: A transaction must bring the database from one valid state to another valid state, maintaining all defined rules (constraints, cascades, triggers).
The Example:
Your database has a rule: inventory_count cannot be negative.
If a user tries to buy 5 items but only 3 are in stock, the database should
reject the transaction because resulting in -2 would violate the consistency
rule.
-- Constraint: CHECK (inventory_count >= 0)UPDATE products SET inventory_count = inventory_count - 5 WHERE id = 123;-- Error: new row for relation "products" violates check constraintIsolation (Wait Your Turn)
Section titled “Isolation (Wait Your Turn)”The Concept: Transactions occur independently without interference. The intermediate state of a transaction is invisible to other transactions.
The Example:
Two users, John and Jane, try to book the last seat on a flight at the exact
same millisecond.
Without isolation, both might read that seats_available = 1, both book it, and
the system oversells.
With isolation, the database locks the row or serializes the requests so one
goes first, and the second sees seats_available = 0.
Durability (Written in Stone)
Section titled “Durability (Written in Stone)”The Concept: Once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors.
The Example:
Your API returns a 200 OK to the frontend after a user saves their profile.
One second later, the data center loses power.
Durability guarantees that when the power comes back, that profile change is
still there. It was written to non-volatile memory (disk) before the success
message was sent.
Resources
Section titled “Resources”- ACID on Wikipedia
- Transactions in PostgreSQL
- Designing Data-Intensive Applications by Martin Kleppmann (The Bible of data engineering)