Sub topic 15
Multiversion and Optimistic Concurrency Control
This chapter assumes you know transactions, schedules, conflicts, serializability, and basic SQL. The focus is how systems reduce blocking when many transactions read data at the same time.
Multiversion concurrency control
Multiversion concurrency control (MVCC) keeps several committed versions of a row. A transaction reads the version visible in its snapshot, usually chosen from the transaction's start time. An update creates a new version instead of overwriting the old version immediately.
For example, suppose account A has these versions:
| Version | Value | Created by | Visible to |
|---|---|---|---|
| v1 | 1000 | T1 | Older snapshots |
| v2 | 900 | T2 | Snapshots after T2 commits |
A transaction that started before T2 committed can still read v1. A later transaction reads v2.
This gives readers a consistent view and usually allows readers and writers to proceed without blocking each other. The database later removes versions that no active transaction can still see.
A simplified execution is:
MVCC does not automatically guarantee serializability. A common implementation provides snapshot isolation: each transaction reads a snapshot, and two transactions cannot both commit conflicting writes to the same row. However, write skew can still occur when transactions read different rows and then update different rows based on a shared rule. Serializable MVCC adds extra checks or locks to reject such executions.
Optimistic concurrency control
Optimistic concurrency control (OCC) assumes conflicts are uncommon. A transaction performs three phases:
- Read phase: read database values and record the read set and proposed writes.
- Validation phase: check whether the transaction conflicts with transactions that committed while it was running.
- Write phase: install its writes if validation succeeds. Otherwise, abort and retry.
A write-write conflict occurs when two transactions try to change the same item. A read-write conflict occurs when one transaction read an item that another committed transaction changed before validation.
def validate(read_set, write_set, committed_writes):
"""Return True when this transaction may commit."""
for other_writes in committed_writes:
if read_set & other_writes:
return False
if write_set & other_writes:
return False
return True
committed = [{"A"}] # A previous transaction changed A
my_reads = {"A", "B"}
my_writes = {"C"}
print(validate(my_reads, my_writes, committed)) # FalseThe example aborts because the current transaction read A after another transaction committed a write to A. In a real database, validation and the final write step must be coordinated so that another transaction cannot commit between them.
Choosing between the approaches
| Approach | Main benefit | Main cost |
|---|---|---|
| MVCC | Readers usually do not block writers | Extra versions and possible write conflicts |
| OCC | Little locking during normal reads | Aborts and retries during contention |
| Serializable MVCC | Strong correctness guarantee | More validation, locking, or aborts |
Use MVCC when workloads contain many reads and transactions need stable snapshots. Use OCC when conflicts are rare and retries are inexpensive. Under heavy contention, repeated OCC aborts can reduce throughput.
Exercise
T1 reads A and writes B. Before T1 validates, T2 commits a write to A. Under the OCC rule in this chapter, may T1 commit?
Show answer
No. T1's read set contains A, and T2's committed write set also contains A, so validation detects a read-write conflict and T1 must abort or retry.