Learn
OCC · Software systemsgrowing

Optimistic Concurrency

Optimistic Concurrency Control

Last updated
Jul 16, 2026
Relationships
6

Mental model

Let people work in parallel; when someone commits, verify that the assumptions they started from are still true.

01 · Conflict lab

Create one stale write on purpose.

Alice and Bob begin from the same version. Step through the moment an invisible overwrite becomes a visible, recoverable conflict.

DOCUMENT #42Simulation ready
AAlice
Local copy

Not read yet

Waiting

Database · current record

Q3 Plan

version 7
BBob
Local copy

Not read yet

Waiting

The database is at version 7. Neither editor has read it yet.

02 · Strategy switch

Optimistic and pessimistic systems pay different costs.

The choice is not “safe versus unsafe.” It is whether ordinary work should pay with waiting or rare conflicts should pay with rework.

Everyone works; the commit point checks the ticket.

No conflict means almost no waiting. A collision means at least one participant must retry, merge, or stop.

Best when
Low contention
Main risk
Retry storms

03 · Concept neighborhood

Acronyms become useful when you know what kind of thing each one is.

OCC is a strategy. Its neighbors include a storage model, an atomic primitive, transaction semantics, retry safety, and merge models.

MVCC

storage model

Multi-Version Concurrency Control

Keeps versions so readers can see consistent snapshots.

CAS

atomic primitive

Compare-and-Swap / Compare-and-Set

Changes a value only while it still equals the expected value.

Isolation

semantics

Transaction Isolation

Defines what concurrent transactions may observe.

Idempotency

retry safety

Idempotent Operation

Makes retrying safe from duplicate effects.

OT / CRDT

merge model

Operational Transformation / Conflict-free Replicated Data Type

Transforms or converges concurrent intent instead of only rejecting it.

PCC

strategy

Pessimistic Concurrency Control

Secures exclusive access before doing critical work.

04 · Judgment practice

Read the shape of the conflict, not just the definition.

Choose a scenario. The strongest design is often a hybrid, because different parts of the system have different contention and failure costs.

Choose a scenario to reveal the design judgment.

Optimistic Concurrency

Optimistic Concurrency Control (OCC) solves a deceptively simple problem: two people can read the same record, make different changes, and accidentally erase each other's work.

Its defining move is not to lock the record while everyone thinks. It lets work proceed, then validates the writer's original assumption at the moment of commitment.

The failure it prevents: lost update

Suppose Alice and Bob both read document #42 at version 7.

  • Alice changes the title and saves first. The database advances to version 8.
  • Bob is still editing the old version 7. He changes the owner field and submits the whole object.
  • Without concurrency control, Bob's stale object can silently restore the old title.

That is a lost update. The dangerous part is not that two people read together. It is that Bob's write was accepted even though the premise behind it was stale.

The mechanism

OCC normally has three stages:

  1. Read the business data and a concurrency token such as a version number or Entity Tag (ETag).
  2. Work locally without holding a long-lived exclusive lock.
  3. Validate and write as one atomic operation. If the token still matches, commit and advance it. If not, report a conflict.
UPDATE documents
SET title = 'Q3 Growth Plan',
    version = version + 1
WHERE id = 42
  AND version = 7;

One affected row means version 7 was still current. Zero affected rows means the record changed after it was read.

The comparison and write must be atomic. A separate SELECT followed later by an unconditional UPDATE leaves a Time of Check to Time of Use (TOCTOU) race between the two statements.

The same idea in HTTP

Hypertext Transfer Protocol (HTTP) exposes this pattern with Entity Tag (ETag) and If-Match:

GET /documents/42
ETag: "v7"

PUT /documents/42
If-Match: "v7"

If the resource is no longer version 7, the server can reject the write with 412 Precondition Failed. The condition is evaluated where the write happens, not guessed by the client from an earlier read.

Optimistic versus pessimistic

DimensionOptimistic Concurrency ControlPessimistic Concurrency Control
Default assumptionConflicts are uncommonConflicts are likely or very costly
Control pointValidate at commitLock or queue before work
No-conflict costLittle waitingLock and waiting overhead remain
Conflict costRetry, merge, or discard workUsually wait rather than redo
Common risksRetry storms, livelock, poor conflict UXDeadlocks, timeouts, lower throughput

A useful approximation is:

optimistic cost ≈ validation + conflict probability × redo cost
pessimistic cost ≈ locking + waiting + deadlock/timeout handling

Real systems often mix them: optimistic editing for ordinary records, short locks or queues for a few hot resources, and idempotency keys around external side effects.

Conflict is part of the protocol

Detecting a conflict is only half the design. The product must decide what happens next:

  • reject and ask the user to refresh;
  • re-read and retry a deterministic operation;
  • merge independent fields;
  • show a three-way merge;
  • serialize a hot resource through a queue or short transaction.

Automatic retry also needs idempotency, bounded retries, and backoff with jitter. Retrying an operation that sends money, email, or third-party requests can otherwise duplicate the side effect.

Where it fits

OCC is strongest when reads dominate writes, conflicts are rare, users may edit for a long time, and retry or merge is affordable.

It degrades when many requests fight over one hot record, conflicts invalidate expensive work, or a business invariant spans records that a single version token cannot protect.

What it is not

  • Not Last Write Wins. OCC makes stale writes visible instead of silently accepting the last arrival.
  • Not Multi-Version Concurrency Control (MVCC). MVCC mainly answers which version a reader sees; OCC answers whether a writer's premise is still current.
  • Not Compare-and-Swap (CAS). CAS is an atomic primitive that can implement the broader OCC strategy.
  • Not Optimistic User Interface. Optimistic UI changes perceived latency; OCC protects concurrent correctness.

Five things to keep

  1. OCC means “do not block first; validate the premise at commit.”
  2. The check and write must be atomic.
  3. Version, ETag, and CAS are carriers or mechanisms, not the complete policy.
  4. A conflict needs a designed exit: reject, retry, merge, or serialize.
  5. Choose between waiting and rework based on contention and failure cost, not ideology.

Relationships

Concept neighbors

PCC

strategy

Pessimistic Concurrency Control

Acquire exclusive access before doing the critical work, trading retries for waiting and lock management.

MVCC

storage model

Multi-Version Concurrency Control

Keep multiple versions so readers can see a consistent snapshot without blocking writers.

CAS

atomic primitive

Compare-and-Swap / Compare-and-Set

Replace a value only when it still equals the expected value; a common building block for OCC.

Isolation

semantics

Transaction Isolation

Defines what concurrent transactions may observe and which anomalies the database prevents.

Idempotency

retry safety

Idempotent Operation

Makes a repeated request produce the intended effect once, which is essential before automatic retries.

OT / CRDT

merge model

Operational Transformation / Conflict-free Replicated Data Type

Preserves concurrent intent by transforming or converging operations instead of simply rejecting one writer.

Evidence trail

Primary sources

Subscribe to my newsletter

I build with AI and write about what works. Subscribe to get new posts delivered.

No tracking. No spam. Pure content.

© 2020-2026 Aaron Guo