Optimistic Concurrency Control
The Drawbacks of Pessimistic Concurrency Control (2PL)
While Two-Phase Locking guarantees serializability, its pessimistic mindset — assuming transactions will constantly clash — comes at a heavy performance cost:
- Lock management overhead: every single read or write request must register with a global Lock Manager to acquire shared or exclusive locks.
- Thread blocking and stalling: when one transaction holds an exclusive lock, other transactions touching the same data are forced to pause and wait in queues.
- Deadlocks: concurrent lock requests can form circular waiting dependencies, forcing the database to run active detection algorithms (wait-for graphs) or prevention rules and abort "victim" transactions.
- Multicore bottlenecks: on modern multi-core CPUs, threads spend substantial time fighting over Lock Manager latches and sitting idle in wait queues rather than processing queries.
The Real-World Trade-Off: When Contention Is Rare
In many real-world workloads, transactions are very short (executing in milliseconds) and rarely touch the exact same records at the same time. Paying the upfront tax of lock acquisition and thread-blocking for every query is unnecessary when conflicts almost never happen.
Typical low-contention use cases:
- User profile and account settings updates: millions of users updating their own profile settings, billing details, or passwords — each user modifies their own distinct records, with zero overlap across concurrent user IDs.
- E-commerce product catalog browsing: thousands of shoppers browsing product pages simultaneously. These operations are almost entirely read-only, so they cannot conflict with each other.
- Event logging and sensor telemetry: ingesting time-series metrics or IoT telemetry appends new rows indexed by unique timestamps or device IDs, so incoming writes don't collide.
Enter Optimistic Concurrency Control (OCC)
Instead of locking data upfront, Optimistic Concurrency Control (OCC) assumes conflicts are rare: transactions execute freely without locks, and conflicts are checked only at commit time using timestamps. Invented at CMU in 1981 by H.T. Kung and John Robinson, OCC divides execution into three strict phases:
Phase 1: Read
- When a transaction begins, the database creates a private workspace — a local memory scratchpad.
Where do the copies actually live? The OCC scratchpad is strictly in the transaction's local, transient memory — nobody else in the database can see or touch it. This is the crucial distinction from Multi-Version Concurrency Control (MVCC): in a single-version database running OCC, the shared global database still holds only one official physical copy of each record; on a successful commit the scratchpad changes overwrite that copy in place, and on failure the scratchpad is simply thrown away. MVCC, by contrast, stores multiple physical versions of the same logical record inside the shared database itself (disk and buffer pool), tagged with begin/end timestamps — so readers can walk the global version chain and read without locks, with no scratchpad or validation needed at all.
Because OCC is a conflict-checking protocol and MVCC is a multi-version storage format, modern engines often combine them: a transaction buffers changes in its private OCC workspace during execution, and after passing validation, instead of overwriting the existing row in place, the engine appends a brand-new version into the global MVCC version chain — the combination covered in Multi-Version Concurrency Control.
- All reads copy the data into this workspace (which also guarantees repeatable reads), and all updates are made strictly inside the private workspace.
- No global database objects are modified and no locks are acquired — other running transactions never see these uncommitted edits.
Phase 2: Validation
- When the transaction calls
COMMIT, it enters validation and is assigned a unique, monotonically increasing timestamp. - The database checks the transaction's read set and write set against other transactions to verify serializability wasn't violated (see Concurrency Anomalies for the underlying theory; more on these sets and the two validation directions below).
Phase 3: Write
- If validation succeeds: the transaction flushes its private workspace changes into the global database, stamping the modified records with its commit timestamp.
- If validation fails: a conflict was detected. The database discards the workspace, aborts the transaction, and restarts it.
Read Sets, Write Sets, and the Two Validation Directions
What Are Read Sets and Write Sets?
During the Read Phase, the database tracks every object a transaction touches in its private workspace:
- Read Set (RS): the set of all objects a transaction reads during its execution.
- Write Set (WS): the set of all objects a transaction modifies during its execution — held as private local copies until commit.
These two tiny bookkeeping lists are the entire basis of conflict detection. A conflict exists whenever a read set and a write set overlap on the same object.
Forward Validation — Looking Forward at Active Transactions
The validating transaction compares its Write Set against the Read Sets of currently active, still-running transactions. The question it answers: am I about to overwrite data that some running transaction has already read? If the overlap is non-empty, that active transaction would end up committing stale reads — so one of the two must lose.
Backward Validation — Looking Backward at Committed Transactions
The validating transaction compares its Read Set against the Write Sets of transactions that already committed while it was running. The question it answers: did I read data that somebody has since changed and committed? If the overlap is non-empty, the validating transaction's work was built on stale values — it must abort and restart.
Modern engines overwhelmingly prefer backward validation, because checking against finalized, committed history is much simpler than coordinating with the volatile, constantly-changing private workspaces of active threads.
The direction flip is the key detail: forward validation compares my write set against active read sets; backward validation compares my read set against committed write sets.
The Example: A Transfer and an Audit
Two accounts in the global database: A = $100 and B = $200 (total $300).
- T1: transfer $20 from A to B.
- T2: audit the total balance (A + B).
Phase 1: Both Read Freely, No Locks
- T1 executes locally:
- Reads A = $100 and B = $200 into its private workspace.
- Computes new local values: A = $80, B = $220.
- Read Set = , Write Set = (holding the updates A = $80, B = $220).
- T2 executes concurrently at full speed:
- Reads A = $100 and B = $200 from the untouched global database.
- Computes total = $300 locally.
- Read Set = , Write Set = ∅ (read-only).
Neither transaction held a lock. Neither blocked the other. Now validation decides who survives.
Phase 2: T1 Commits First
- T1 calls
COMMITand receives timestamp TS = 1. - Nothing else has committed yet — T1 passes validation.
- Write phase: T1 flushes its workspace. Live database is now A = $80, B = $220.
Phase 3: T2 Validates — and Fails
- T2 calls
COMMITand receives timestamp TS = 2. - Backward validation: T2 checks its Read Set against the Write Set of T1, which committed while T2 was running: ∩ = — not empty. Conflict detected.
- T2 read the old values ($100, $200), but T1 changed exactly those keys and committed before T2 validated. If T2 were allowed to commit, its audit would record a total of $300 built on stale state while the live database reflects T1's updates — violating serializability.
- T2 fails validation: its workspace is discarded, the transaction aborts, and it restarts.
- On restart: T2 reads the newly committed values (A = $80, B = $220) and outputs a total of $300 — matching a valid serial execution (T1 fully before T2).
The Same Conflict, Forward-Validated
Had the engine used forward validation, the same collision surfaces from the other side: when T1 enters validation while T2 is still running, T1 compares its Write Set against active T2's Read Set — again not empty. Seeing that T2 has already read the very data T1 is about to overwrite, T1 forces the active T2 to abort and restart, then commits safely.
So in this example, T2 is the loser in both directions — the conflict is detected either way, and T2 restarts either way. What changes is who notices, and when: backward validation lets T2 discover the conflict at its own commit (after T1 is already durable), while forward validation lets T1 catch it early and kill T2 before it wastes any more work.
Key Takeaways from the Example
- Zero lock overhead: neither transaction ever held a lock or blocked the other during execution.
- Timestamps are assigned at validation, not at start — the database orders transactions by when they finish, giving it flexibility to pick a convenient serial order.
- The trade-off: T2 lost all the work in its private workspace when it failed validation. This is exactly why OCC shines under low contention and degrades when many transactions fight over the same keys — everyone keeps aborting and restarting.
Related reads
- Two-Phase Locking — the pessimistic counterpart, and why its lock queues hurt under low contention
- Concurrency Anomalies — the conflict theory validation is enforcing
- Concurrency Control Theory in Databases — the problem statement, from processors up to ACID