4.2 The Four Properties of an ACID Transaction
The four properties of a transaction are expressed together by the word ACID. Memorizing each property as is can be confusing, so it is better to understand them through their purpose and role.
| Property | Prevents | Example | Related feature |
|---|---|---|---|
| Atomicity | Only part of a transaction being applied | No state remains where the withdrawal happened but the deposit did not | Log and Undo (Chapter 5) |
| Isolation | Transactions' states getting mixed | The intermediate state of a transfer is not visible to other transactions | Locks and MVCC (Chapter 7) |
| Consistency | Constraints not being kept | The balance never goes negative | Constraints |
| Durability | A transaction being lost | After seeing "transfer complete", the record survives a power outage | Log and fsync (Chapters 5-6) |
Atomicity means a transaction cannot be divided further. An atom is, as the word says, an indivisible unit. Even for a transaction made of several update operations, the database allows only the states where all are applied or none are.
Consistency is a bit different in nature. Consistency relates not to the transaction itself but to external constraints. If the database has the constraint "the balance must be 0 or more", a transaction must keep this constraint in every case. When constraints such as CHECK, foreign keys, and UNIQUE are defined, the database rejects a transaction that breaks them.
A practical question arises here. A rule such as "the total of all balances must not change before and after a transfer" is briefly broken while the transaction runs, right after the withdrawal and before the deposit. If constraints were checked at every statement in the transaction, a transfer would be impossible. So constraint checking happens at two possible points. The default is to check immediately when a statement ends, but some constraints can be configured to defer checking to commit time. The SQL standard's DEFERRABLE feature is this. PostgreSQL and Oracle support it.
Isolation relates to concurrency. Without isolation, if you query the bank's total balance at the moment after user A's withdrawal finished and just before user B's deposit, the total comes out 100,000 won short of the real amount. Isolation guarantees that the intermediate state of an active transaction is not visible.
Durability is the property that a transaction's changes are not lost. Even if the database server terminates abnormally right after a user's transfer completes, the transfer record must remain intact after the server starts again. If the database has responded to the client with the transaction's completion code, it guarantees that the transfer record is preserved in any situation.
These four properties are not implemented by a single technique. Atomicity and durability are guaranteed by the log covered in Chapters 5-6, and isolation by the locks and MVCC covered in Chapter 7. Part 2 focuses on how ACID is implemented inside the database.
Figure 4-2. ACID map: the four properties, the devices that keep them, and the chapters that cover them
flowchart TB
subgraph prop ["Property"]
A["A Atomicity"]
C["C Consistency"]
I["I Isolation"]
D["D Durability"]
end
subgraph risk ["Accident prevented"]
A2["Only partly applied<br/>Withdrawal done, deposit not"]
C2["Constraint violation<br/>Balance goes negative"]
I2["Intermediate state visible<br/>Total reads 100,000 won short"]
D2["Completed change lost<br/>Power outage after completion response"]
end
subgraph mech ["Device that keeps it and chapter"]
A3["Log and Undo<br/>Chapter 5"]
C3["Constraints<br/>CHECK, foreign key, UNIQUE"]
I3["Locks and MVCC<br/>Chapter 7"]
D3["Log and fsync<br/>Chapters 5-6"]
end
A --> A2 --> A3
C --> C2 --> C3
I --> I2 --> I3
D --> D2 --> D3
R["Consistent data"]
C3 --> R
A3 -.-> R
I3 -.-> R
D3 -.-> R
In the diagram, each vertical line is one property. The top row is the four letters of ACID, the middle row is the accident that occurs without that property, and the bottom row is the device that actually keeps the property and the chapter of this book that covers it. Reading top to bottom along the solid lines gives one row of the table above, such as "atomicity prevents the accident of being only partly applied, and the log and Undo keep it (Chapter 5)".
The lines for atomicity and durability both end in the log at the bottom. The two properties are implemented with the same log: atomicity takes the side that uses Undo to revert in-progress changes, and durability takes the side that uses fsync to leave committed changes on disk. Isolation is kept not by the log but by separate devices, locks and MVCC.
The "Consistent data" at the bottom shows how consistency is produced. What the database checks directly for consistency is constraints such as CHECK, foreign keys, and UNIQUE (solid line), but as the dotted lines show, the devices of the other three properties also support consistency. If a transaction is only half applied, intermediate states get mixed, or committed changes disappear, the data does not stay consistent even when constraints are checked. Consistency is not the work of one device but the result of constraints and the other three properties together.