Timestamp & Optimistic Control
Not every system wants to wait for locks. Timestamps and optimistic validation let transactions run and check for conflicts later.
In plain English
Let everyone work, then check at the end whether their edits clashed.
The one idea
Timestamp ordering and optimistic concurrency control avoid blocking by validating conflicts; a transaction that loses validation aborts and retries.
Read-heavy workloads can spend more time waiting for locks than doing work. Validation trades blocking for aborts.
By the end
- 01Explain timestamp ordering.
- 02Describe the validation phase of optimistic concurrency control.
- 03Know what MVCC keeps multiple versions.
- 04Compare optimistic and pessimistic control.
Timestamp ordering
Each transaction gets a timestamp. Each data item tracks the last read and write timestamps. A read or write that would violate the timestamp order is rejected and the transaction restarts. This avoids locks but can cause repeated restarts under contention.
Optimistic concurrency control
A transaction runs without locks, recording its reads and writes. At commit, a validation phase checks whether any concurrent transaction changed the data it read. If there is a conflict, it aborts and retries. Optimistic control works well when conflicts are rare.
Multiversion concurrency control
MVCC keeps multiple versions of a row. Readers see a consistent snapshot without blocking writers, and writers create new versions. Old versions are reclaimed when no transaction needs them. This is why many modern databases allow reads and writes to proceed concurrently.
row id=1:
version 1: balance=100
version 2: balance=120
reader with old snapshot -> version 1
new reader -> version 2Optimistic checkout
T1 reads item x = 10 and prepares to write 9. T2 writes x = 11 and commits first.
T1 records the version of x it read.
The read set remembers the version, not just the value.
T2 commits a newer version of x.
T1's read set is now stale.
T1 enters validation and fails.
T1 aborts and retries with the new value instead of overwriting T2.
ConclusionOptimistic control is a bet that conflicts are rare. Under heavy write contention, the abort rate can erase the benefit.
The trade-off
No free correctness.
Gain
No lock waits for read-only work and good throughput when conflicts are rare.
Cost
Aborts, retries, and wasted work under contention; starvation is possible.
Verdict
Use OCC for read-mostly or short transactions; use locking when conflicts are frequent.
Recap
What to keep.
Takeaways
- +Timestamp ordering rejects out-of-order operations.
- +Optimistic control validates at commit and retries on conflict.
- +MVCC keeps multiple row versions for consistent reads.
- +Optimistic control is good when conflicts are rare.
- +Pessimistic locking is better under high contention.
Traps
- !Assuming optimistic control never aborts.
- !Forgetting that MVCC needs garbage collection for old versions.
- !Confusing timestamps with wall-clock time.
Focus
- →Trace a timestamp conflict.
- →Explain the validation phase.
- →Compare MVCC with locking.
Check yourself
Before you turn the page.
When does optimistic concurrency control validate a transaction?
What does MVCC keep?