Sub topic 1
Transactions and the I in ACID
A transaction is a group of database operations that the database treats as one unit. For example, transferring money from one account to another requires two changes:
- Subtract money from the sender.
- Add the same money to the receiver.
Both changes must happen, or neither should happen.
A transaction is commonly described with ACID:
- Atomicity means all operations in the transaction succeed, or all are undone.
- Consistency means a committed transaction preserves the database's rules, such as primary-key and foreign-key constraints.
- Isolation controls what concurrent transactions can see while they run.
- Durability means committed changes survive a crash.
This chapter focuses on isolation. Assume you already know basic SQL, including SELECT, UPDATE, BEGIN, COMMIT, and ROLLBACK, and that you understand tables and primary keys.
What isolation means
A database serves many clients at once. Two transactions can read or change related rows during overlapping periods. Isolation defines the visibility rules for those overlaps.
A fully isolated transaction would behave as though it were the only transaction using the database. This behavior is easy to reason about, but it can reduce concurrency. Transactions may wait for locks, use more resources, or be rejected because they conflict.
Most systems therefore offer several isolation levels. Each level permits some kinds of overlap in exchange for better throughput or lower waiting time. Choosing a level is a correctness decision, not merely a performance setting.
Consider two transactions that both inspect an account balance:
BEGIN;
SELECT balance FROM accounts WHERE id = 1;
UPDATE accounts
SET balance = balance - 25
WHERE id = 1;
COMMIT;If another transaction changes the same account between the SELECT and UPDATE, the first transaction needs a defined rule. Should it see the old value, the new value, or wait? The isolation level helps answer that question.
Isolation and the transaction boundary
Isolation applies to a transaction, not just to one SQL statement. The database tracks which reads and writes belong together between BEGIN and COMMIT or ROLLBACK.
A transaction that reads data twice may receive the same result both times, or it may observe a committed change made by another transaction. The answer depends on the selected isolation level and the database engine.
COMMIT makes the transaction's changes visible according to the engine's rules. ROLLBACK discards its uncommitted changes. Other transactions generally must not observe changes that might still be rolled back.
Why the trade-off matters
Higher isolation can prevent more anomalies, such as reading data that another transaction has not committed or seeing a changing result during one transaction. The cost can include lock waits, deadlocks, reduced throughput, and serialization failures that require a retry.
Lower isolation can improve concurrency for workloads that tolerate approximate or changing reads. Financial transfers, inventory reservations, and other invariant-sensitive operations usually need stronger guarantees or carefully designed atomic statements.
The rest of this tutorial examines the anomalies caused by concurrent transactions, maps them to SQL isolation levels, and explains how locks and multiversion concurrency control implement those guarantees.
Exercise
Why can a database avoid using full isolation for every transaction?
Show answer
Full isolation can make concurrent transactions wait or fail more often, reducing throughput. A lower level may provide enough correctness with better concurrency for a particular workload.