DBMS/Lesson 6 of 9/5 min read
Transactions and ACID
Transactions, ACID properties and how each is implemented, transaction states, schedules, conflict and view serializability, precedence graphs, and recoverable, cascadeless and strict schedules.
On this page
Transaction
A transaction is a logical unit of work made of one or more operations that must execute as a whole. Classic example: transfer 100 from A to B.
BEGIN;
UPDATE account SET balance = balance - 100 WHERE id = 'A';
UPDATE account SET balance = balance + 100 WHERE id = 'B';
COMMIT; -- or ROLLBACK on errorAt the storage level a transaction is a sequence of read(X) and write(X) operations ending in commit or abort.
ACID
Transactions guarantee four properties.
Atomicity
All or nothing. If the transfer fails after debiting A, the debit is undone. Implemented by the recovery manager using undo logs (see logging and recovery).
Consistency
A transaction takes the database from one valid state to another, preserving all constraints (keys, foreign keys, checks, and business rules like "total money is unchanged"). It is the joint responsibility of the application (correct logic) and the DBMS (constraints).
Isolation
Concurrent transactions do not see each other's intermediate states; the result is as if they ran one after another. Implemented by the concurrency control manager using locks or MVCC (see concurrency control). Strength is tunable with isolation levels.
Durability
Once committed, changes survive crashes and power loss. Implemented by forcing the log to stable storage before acknowledging commit (write-ahead logging), plus backups and replication.
| Property | Component responsible |
|---|---|
| Atomicity | Recovery manager (undo log) |
| Consistency | Application + integrity constraints |
| Isolation | Concurrency control (locks, MVCC) |
| Durability | Recovery manager (redo log, WAL) |
Transaction States
active --> partially committed --> committed
| |
v v
failed -----> aborted (then restart or kill)- Active: executing.
- Partially committed: last statement executed, changes may still be in memory buffers.
- Committed: changes are durable.
- Failed: normal execution cannot continue.
- Aborted: rolled back; the database is restored to the state before the transaction. It can be restarted (if the error was not logical) or killed.
Schedule
A schedule is the order in which operations of concurrent transactions are executed.
- Serial schedule: transactions run one after another, no interleaving. Always correct but slow.
- Non-serial (concurrent) schedule: operations interleave. Faster, but may produce wrong results.
Serializability
A concurrent schedule is serializable if its effect equals some serial schedule. Serializability is the standard correctness criterion for isolation.
Conflict Serializability
Two operations conflict if they belong to different transactions, access the same data item, and at least one is a write:
| Pair | Conflict? |
|---|---|
| read / read | No |
| read / write | Yes |
| write / read | Yes |
| write / write | Yes |
A schedule is conflict serializable if it can be turned into a serial schedule by swapping adjacent non-conflicting operations.
Precedence Graph
Test for conflict serializability:
- One node per transaction.
- Edge
Ti -> Tjif an operation ofTiconflicts with and comes before an operation ofTj. - Acyclic graph means conflict serializable; a topological order gives the equivalent serial schedule.
Example:
T1: R(A) W(A)
T2: R(A) W(A)T1 R(A) before T2 W(A) gives T1 -> T2. T2 R(A) before T1 W(A) gives T2 -> T1. Cycle, so not conflict serializable. This is the lost update problem.
View Serializability
A weaker condition. Two schedules are view equivalent if they have the same initial reads, the same reads-from relationships, and the same final writes. Every conflict serializable schedule is view serializable, but not vice versa; the extra ones contain blind writes (writes without a prior read). Testing view serializability is NP-complete, so databases use conflict serializability.
Recoverability
Serializability is not enough; schedules must also behave well when transactions abort.
Recoverable Schedule
If Tj reads data written by Ti, then Ti must commit before Tj commits. Otherwise, if Ti aborts after Tj committed, Tj used data that never existed and cannot be undone.
Cascading Rollback
If T2 reads uncommitted data from T1 and T1 aborts, T2 must also roll back, and so on: one abort cascades through many transactions. Wasteful.
Cascadeless Schedule
Transactions only read data written by committed transactions. Prevents cascading rollbacks. Also called avoiding cascading aborts (ACA).
Strict Schedule
A transaction can neither read nor overwrite a data item until the last transaction that wrote it has committed or aborted. Makes undo simple (just restore the before-image). Strict two-phase locking produces strict schedules.
strict ⊂ cascadeless ⊂ recoverableQuick revision
- ACID: atomicity (undo), consistency (constraints), isolation (concurrency control), durability (redo, WAL).
- Conflicts need different transactions, same item, at least one write.
- Precedence graph acyclic = conflict serializable.
- Strict ⊂ cascadeless ⊂ recoverable.