Sub topic 9
How Databases Implement Isolation: Locks and MVCC
Assume you know SELECT, UPDATE, BEGIN, COMMIT, and ROLLBACK. You should also understand that several clients can run transactions at the same time. This chapter explains the mechanisms databases use to keep those transactions from interfering incorrectly.
Two ways to isolate transactions
A database usually combines two techniques:
- Locking controls who may access a piece of data at a time.
- Multiversion concurrency control (MVCC) keeps multiple committed versions of a row so readers can use a stable view without blocking writers.
The exact behavior depends on the database engine and isolation level.
Locking
A transaction can acquire a shared lock when it reads data and an exclusive lock when it changes data.
- Shared locks can usually coexist with other shared locks.
- An exclusive lock conflicts with reads or writes that require incompatible locks.
- A lock remains active until the statement ends or, commonly, until the transaction commits.
For example, this statement explicitly requests a lock:
BEGIN;
SELECT balance
FROM accounts
WHERE id = 1
FOR UPDATE;
UPDATE accounts
SET balance = balance - 25
WHERE id = 1;
COMMIT;A second transaction that also uses FOR UPDATE must wait until the first transaction commits or rolls back. This makes a read-modify-write operation safe because only one transaction at a time can modify the selected row.
Locks can apply to rows, index ranges, pages, or entire tables. A database may lock more data than the query appears to touch. This is called lock escalation or granularity change, depending on the engine. Larger locks reduce bookkeeping but increase blocking.
MVCC
MVCC means that a database stores enough row history to decide which version a transaction may see. A transaction reads a snapshot, which is the set of committed versions visible at a particular point.
A reader can therefore read an older committed row while another transaction creates a newer version. The reader does not need to wait for the writer, and the writer does not need to wait for an ordinary reader.
MVCC does not eliminate locks. Updates still need locks or conflict checks so that two transactions cannot change the same row incorrectly. Databases also use locks for explicit FOR UPDATE reads, schema changes, and some index or range operations.
How the mechanisms affect behavior
| Mechanism | Ordinary reader | Writer | Main cost |
|---|---|---|---|
| Blocking locks | May wait for a writer | May wait for another writer | Blocking and deadlocks |
| MVCC snapshot | Reads a visible row version | Creates a new version | Storage and cleanup work |
| Explicit row lock | Reads current data and reserves it | Other lockers wait | Lower concurrency for selected rows |
With MVCC, a transaction using a repeatable snapshot keeps seeing the versions visible to that snapshot. A transaction using read committed may choose a new snapshot for each statement. Thus, two identical queries can return different results even though neither query read uncommitted data.
Practical consequences
Locks and snapshots protect different things:
- Use an atomic statement such as
balance = balance + 10when the database can perform the whole change safely. - Use
SELECT ... FOR UPDATEwhen application logic must read a row, make a decision, and then update it. - Use a serializable transaction when a rule depends on a set of rows and explicit locking would be difficult.
- Keep transactions short. Long transactions hold locks longer and prevent old MVCC versions from being cleaned up.
A transaction can still fail. Two transactions may acquire locks in opposite orders and create a deadlock. The database normally aborts one transaction so the other can continue. Applications should retry the aborted transaction when the operation is safe to repeat.
Exercise
Why can an ordinary MVCC reader avoid waiting when another transaction updates the same row, and when would you still use FOR UPDATE?
Show answer
The reader can use a visible older row version from its snapshot. Use FOR UPDATE when the transaction must reserve the current row while it performs read-modify-write logic.