← Back

Optimistic Concurrency Control

8 min read

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:

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:

  1. 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.
  2. 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.
  3. 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

Note

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.

Phase 2: Validation

Phase 3: Write

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:

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).

Phase 1: Both Read Freely, No Locks

  1. 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).
  2. 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

Phase 3: T2 Validates — and Fails

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