Sub topic 2
The ACID properties
This chapter assumes you know basic relational concepts (tables, rows, queries), can read simple SQL such as SELECT and UPDATE, and know what a transaction is: a group of reads and writes the database treats as one logical unit of work. ACID is the set of four guarantees that makes that unit trustworthy. GATE questions usually ask you to name which property a scenario violates, or which database component enforces it.
The four properties at a glance
ACID stands for Atomicity, Consistency, Isolation and Durability. Each one answers a different failure question.
| Property | Question it answers | Typical enforcing mechanism |
|---|---|---|
| Atomicity | What if the transaction stops halfway? | Undo logging and rollback (recovery manager) |
| Consistency | Does the database still satisfy its rules afterwards? | Integrity constraints, triggers, and correct transaction programs |
| Isolation | What if other transactions run at the same time? | Concurrency control (locking, timestamps, multiversioning) |
| Durability | What if the system crashes after commit? | Redo logging, write-ahead log, stable storage |
We use one running example throughout: transferring 100 from account A to account B. Assume A = 500 and B = 300 at the start.
# Transaction T1: transfer 100 from A to B
# read(A); A = A - 100; write(A)
# read(B); B = B + 100; write(B)
# commitAtomicity
Atomicity means a transaction is all or nothing. Either every write takes effect, or none does. There is no visible state in which only part of the transaction happened.
Suppose the system crashes after write(A) but before write(B). A is now 400 and B is still 300. The total has dropped from 800 to 700, so 100 has vanished. Atomicity requires the database to restore A to 500 when it recovers.
The recovery manager provides atomicity. It records the old value of each item in a log before the new value reaches the database. After a crash, it uses these records to undo the writes of any transaction that did not commit.
Two points matter for exams:
- Atomicity concerns failures (crashes, aborts, deadlock victims), not concurrency.
- A transaction that aborts for any reason, including a program error, must leave no trace.
Consistency
Consistency means a transaction takes the database from one consistent state to another consistent state. A consistent state satisfies all declared integrity constraints, such as primary keys, foreign keys, CHECK conditions, and application rules like "the total of A and B stays constant in a transfer".
This property is shared between the database and the programmer:
- The database checks declared constraints and rejects a transaction that would violate them.
- The programmer must write transactions that are logically correct. If the transfer code subtracted 100 from A but added 10 to B, the database could not know the business rule was broken unless you declared it.
The standard argument is: if the database is consistent before the transaction, the transaction is correct when run alone, and the other three properties hold, then the database is consistent afterwards. So consistency is usually described as a result of correct code plus atomicity and isolation, rather than something the system enforces by itself.
CREATE TABLE account (
id INTEGER PRIMARY KEY,
balance INTEGER NOT NULL CHECK (balance >= 0)
);With this CHECK, a withdrawal that would make a balance negative fails, and the whole transaction must roll back. That is a database-enforced consistency rule.
Isolation
Isolation means that concurrent transactions do not interfere. Each transaction should behave as if it were running alone, even though the system interleaves their operations for throughput.
Consider two transfers running at once without control. T1 moves 100 from A to B. T2 reads A and B to compute a total.
- T1 reads A (500), writes A = 400.
- T2 reads A (400) and B (300), and reports a total of 700.
- T1 reads B (300), writes B = 400.
T2 saw an intermediate state that never existed in any serial order. The correct total is 800. The standard formal definition of isolation is serializability: the result of concurrent execution must equal the result of some serial execution of the same transactions. Later chapters build the tools to test this (precedence graphs, locking, timestamps).
In practice, systems let you relax isolation through isolation levels, trading some anomalies for speed. Full isolation is the strictest end of that scale.
Durability
Durability means that once a transaction has committed, its effects survive any later failure, including a power loss or crash. If the system told the client "committed", the data must be there after restart.
The mechanism is the write-ahead log. Before reporting commit, the system forces the transaction's log records to stable storage (disk, or replicated storage). The data pages themselves may still be only in memory. After a crash, the recovery manager redoes the committed writes from the log.
The key rule: commit is acknowledged only after the commit record is safely on stable storage.
How the properties map to components
A common exam question asks which module is responsible for which property.
| Component | Property it mainly ensures |
|---|---|
| Transaction manager / recovery manager (undo) | Atomicity |
| Recovery manager (redo, log forcing) | Durability |
| Concurrency control manager | Isolation |
| Integrity constraint checker plus application logic | Consistency |
Violation examples for each property
You should be able to produce a concrete violation of each property on demand.
- Atomicity violated: the system crashes between
write(A)andwrite(B), and on restart A is 400 and B is 300. Money disappeared. - Consistency violated: a transaction deletes a customer row while orders referencing that customer remain, and no foreign key stops it. Or a buggy program credits B by 10 instead of 100.
- Isolation violated: two transactions both read balance 500 and each subtract 100 and write back. The final balance is 400 instead of 300. This is the lost update anomaly.
- Durability violated: the database reports commit, then loses power before the log reached disk, and after restart the committed transfer is missing.
Common errors and edge cases
- Confusing atomicity with isolation. Atomicity is about one transaction failing partway. Isolation is about several transactions overlapping. A lost update is an isolation failure, not an atomicity failure, even though data ends up wrong.
- Thinking the database guarantees consistency alone. It only enforces the constraints you declared. Undeclared business rules are the programmer's job.
- Assuming a committed transaction can be rolled back. After commit, atomicity no longer applies to it. To reverse it, you run a new compensating transaction.
- Assuming durability means the data is in the table file. It only means the log (or equivalent) is on stable storage. Data pages can be flushed later.
- Treating isolation as always all-or-nothing. Most systems default to a weaker level than full serializability, so some anomalies are allowed by default.
- Forgetting that an aborted transaction must also be isolated. If other transactions read its uncommitted writes, aborting it forces them to abort too. Recoverability, covered later, deals with this.
Seeing atomicity and rollback in SQL
The following runs in SQLite using Python's standard library. It shows a transfer that fails a constraint midway, and the rollback restoring the earlier state.
import sqlite3
conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE account (id INTEGER PRIMARY KEY, "
"balance INTEGER NOT NULL CHECK (balance >= 0))")
conn.execute("INSERT INTO account VALUES (1, 500), (2, 300)")
conn.commit()
def transfer(conn, src, dst, amount):
try:
conn.execute("UPDATE account SET balance = balance + ? WHERE id = ?",
(amount, dst))
conn.execute("UPDATE account SET balance = balance - ? WHERE id = ?",
(amount, src))
conn.commit()
return "committed"
except sqlite3.IntegrityError:
conn.rollback()
return "rolled back"
print(transfer(conn, 1, 2, 100)) # committed
print(transfer(conn, 1, 2, 900)) # rolled back: source would go negative
print(conn.execute("SELECT id, balance FROM account ORDER BY id").fetchall())
# [(1, 400), (2, 400)]In the second call, the credit to account 2 succeeds first, then the debit violates the CHECK. The rollback undoes the credit as well. The final balances show only the first transfer, which is atomicity and consistency working together.
Exercise
A transaction transfers 100 from A to B. The system crashes after write(A) is done and before write(B) is executed. On restart, A shows the reduced value and B is unchanged. Which ACID property was violated, and which component should have prevented it?
Show answer
Atomicity was violated, because only part of the transaction's effects persisted. The recovery manager should prevent it by using the undo information in the log to roll back the uncommitted transaction's write to A.
Exercise
Two transactions each read a balance of 500, each subtract 100, and each write the result back. The final balance is 400. Name the violated property and the anomaly, and state what a correct outcome would be.
Show answer
Isolation is violated. The anomaly is a lost update, because one write overwrote the other. A correct (serializable) outcome is a balance of 300, as in either serial order.
Exercise
The database reports "commit successful" for a transaction. Power fails a moment later, and after restart the transaction's changes are gone. Which property failed, and what rule about the log was broken?
Show answer
Durability failed. The commit was acknowledged before the transaction's log records were forced to stable storage, which breaks the write-ahead logging and commit rule.