Sub topic 2
The ACID Properties
Assume you know basic tables, rows, SELECT, and UPDATE statements. You should also understand that a transaction is a group of database operations treated as one logical unit. ACID describes the guarantees a database aims to provide for that unit.
ACID stands for:
- Atomicity: all operations happen, or none happen.
- Consistency: a committed transaction preserves database rules.
- Isolation: concurrent transactions behave as if their operations were suitably separated.
- Durability: committed results survive failures.
Atomicity
Atomicity makes a transaction indivisible from the database’s point of view. Consider transferring 100 units from account 1 to account 2:
CREATE TABLE account (
id INTEGER PRIMARY KEY,
balance INTEGER NOT NULL CHECK (balance >= 0)
);
INSERT INTO account VALUES (1, 500), (2, 200);
BEGIN;
UPDATE account
SET balance = balance - 100
WHERE id = 1;
UPDATE account
SET balance = balance + 100
WHERE id = 2;
COMMIT;If the second update fails, the database must roll back the first update. Otherwise, 100 units disappear. A system crash during the transaction should produce the same all-or-nothing result.
An atomicity violation occurs when only part of a transaction becomes visible or permanent.
Consistency
Consistency means that a transaction changes one valid database state into another valid database state. The rules may include:
- Primary-key and foreign-key constraints.
CHECKconstraints.- Uniqueness rules.
- Application rules, such as preserving the total money in a transfer.
For example, the constraint balance >= 0 prevents an account from becoming negative. If a transaction attempts to create an invalid state, the database rejects it or rolls it back.
Consistency does not mean that the database automatically understands every business rule. If the application forgets to check that a transfer has sufficient funds, a logically invalid result might still be accepted unless a database constraint enforces that rule.
Isolation
Isolation controls what concurrent transactions can observe while they execute. A transaction should not accidentally use another transaction’s unfinished or conflicting work.
Without adequate isolation, one transaction might read a value that another transaction later changes. Another common problem is a lost update: two transactions read the same old value, calculate new values, and one overwrites the other’s result.
Isolation is usually configurable. Stronger isolation reduces anomalies but can reduce concurrency or increase waiting. The SQL isolation levels provide different trade-offs, which are studied later in the tutorial.
Durability
Durability means that once the database reports COMMIT success, the committed result will remain after a crash or restart. Databases usually achieve this with a transaction log and stable storage. The log records enough information to redo committed changes during recovery.
A durability violation occurs when a system acknowledges a commit but loses the change after a power failure.
Seeing the four guarantees together
| Property | Main question | Example violation |
|---|---|---|
| Atomicity | Did every operation succeed together? | Debit succeeds, credit fails |
| Consistency | Are all required rules still true? | A foreign key points to no row |
| Isolation | Can concurrent work interfere incorrectly? | One update overwrites another |
| Durability | Does a commit survive a crash? | Committed data disappears after restart |
For GATE questions, separate consistency from isolation carefully. Consistency concerns validity rules and states. Isolation concerns interference between transactions.
Exercise
A transfer debits account A, the database reports COMMIT, and then a power failure occurs. After restart, the debit is missing. Which ACID property was violated?
Show answer
Durability. A successful commit must survive a system failure.