Handbooks  /  Optimistic vs Pessimistic Locking
Engineering~9 min readComparison
Head to Head

Optimistic vs pessimistic locking: assume conflict, or hope there isn’t one?

OptimisticvsPessimistic

Two users edit the same row at the same time. You can guard against that by locking the row up front so the second user waits — or by letting both proceed and catching the collision only if it actually happens. Which is right depends almost entirely on one thing: how often those two users really do collide.

01

The one distinction that decides everything

Pessimistic locking assumes a conflict is likely, so it takes a lock before reading-to-edit — SELECT … FOR UPDATE — and anyone else who wants that row waits until the lock is released. Optimistic locking assumes conflicts are rare, so it takes no lock: it reads the row (noting a version number or timestamp), does its work, and only at write time checks "is the version still what I read?" If yes, it writes and bumps the version; if the row changed underneath it, the write is rejected and the caller retries. One prevents collisions by blocking; the other detects them after the fact.

→ The rule

Pessimistic: lock first, others wait, no wasted work — but blocking and deadlock risk. Optimistic: no lock, full concurrency — but conflicts cause retries, and lots of conflicts means lots of wasted retries. The right choice is set by how contended the data actually is.

02

Head to head

DimensionOptimisticPessimistic
When it locksNever — checks a version on writeBefore editing (up front)
AssumesConflicts are rareConflicts are likely
Under low contentionFast — no lock overheadWasteful — locks nobody contends for
Under high contentionRetry storms — wasted workSerializes cleanly — others just wait
On conflictReject & retryBlock until the lock frees
Deadlock riskNone (no locks held)Real — must order lock acquisition
Implemented withVersion / timestamp column, compare-and-swapSELECT FOR UPDATE, row locks
03

When to use each

Reach for optimistic

  • Low contention — collisions are genuinely rare
  • Reads vastly outnumber writes to the same row
  • Long "think time" between read and write (a user editing a form)
  • You want maximum concurrency and no blocking
  • Distributed systems where holding locks is costly

Reach for pessimistic

  • High contention — many writers hit the same row
  • Retrying is expensive or the work is long
  • A conflict must be prevented, not merely detected
  • Short, sharp transactions that release the lock fast
  • Hot rows (inventory counts, counters) under load
04

Contention is the deciding variable

Both schemes are correct — they prevent the lost update (two writers both reading the old value and one silently overwriting the other). The choice between them is a performance bet on contention. Under low contention, optimistic wins easily: almost every write succeeds first try, and you paid zero locking overhead for the common case. Under high contention, the bet flips: optimistic writes keep colliding and retrying, and a stampede of retries can do more wasted work than simply queueing. There, pessimistic locking serializes access cleanly — everyone waits their short turn instead of racing, failing, and racing again.

→ The cheap default

Default to optimistic (a version column) — most rows aren’t hot, and you avoid locks entirely. Switch a specific hot row to pessimistic (or an atomic operation) once you measure real contention and retry churn on it. Don’t lock the whole database for the few rows that actually fight.

05

Retry storms versus blocking and deadlocks

Each strategy has a failure mode that shows up exactly when you picked the wrong one. Optimistic locking’s is the retry storm: on a hot row, writers keep reading the same version, colliding, and retrying in lockstep — throughput collapses as everyone redoes work that keeps getting invalidated. Worse, the retries can synchronize into waves. Optimistic under high contention doesn’t corrupt data; it just melts down on throughput.

Pessimistic locking’s failure mode is blocking and deadlock: while one transaction holds a lock, others stall, so long-held locks tank concurrency — and if two transactions grab locks in different orders (A then B versus B then A), they can wait on each other forever, a deadlock the database must detect and kill. That’s why pessimistic locks want to be short-lived and acquired in a consistent order. In short: optimistic wastes work on conflict, pessimistic makes others wait and risks deadlock — pick the failure you can better afford.

→ The trade you’re actually making

Optimistic trades guaranteed progress for zero locking overhead — great until contention makes retries dominate. Pessimistic trades concurrency and deadlock-freedom for guaranteed non-collision — great until locks are held too long.

06

A worked scenario: editing a profile vs decrementing flash-sale stock

A user edits their own profile bio. Two people editing the same profile at the same second is vanishingly rare, and there’s long "think time" while they type. This is textbook optimistic: read the row with its version, save with "update … where version = N," and in the one-in-a-thousand case someone else saved first, show "this changed, please reload." No locks, full concurrency, and the rare conflict is cheap to handle.

Now decrement the stock of one hot item during a flash sale — thousands of buyers hitting the exact same row in the same instant. Optimistic here is a disaster: nearly every write collides and retries, a full-blown retry storm. This wants pessimistic locking (or, better still, a single atomic operation like UPDATE … SET stock = stock - 1 WHERE stock > 0) so the contending writers serialize cleanly instead of racing and failing. Same "update a number" operation, opposite locking choice — because the contention is opposite.

→ The pattern generalizes

Rarely-contended, think-time-heavy edits → optimistic. Hot, heavily-contended rows → pessimistic or an atomic update. Measure contention per row; don’t apply one strategy to the whole database.

Frequently asked

Quick answers

What's the difference between optimistic and pessimistic locking?

Pessimistic locking takes a lock before editing a row, so anyone else who wants it waits — it assumes conflicts are likely and prevents them by blocking. Optimistic locking takes no lock: it reads a version number, does its work, and checks at write time whether the version still matches, retrying if the row changed underneath it. One prevents collisions by locking; the other detects them and retries.

Which is better?

It depends on contention. Under low contention, optimistic wins — almost every write succeeds first try with zero locking overhead. Under high contention, pessimistic (or an atomic operation) wins, because optimistic writes keep colliding and retrying. Default to optimistic and switch specific hot rows to pessimistic once you measure real contention.

How is optimistic locking implemented?

Usually with a version or timestamp column. You read the row and its version, and your write is conditional: "update … set …, version = version + 1 where id = ? and version = ?". If another writer already bumped the version, the update affects zero rows, and you know a conflict happened and retry with the fresh value. Compare-and-swap is the same idea at the value level.

What are the failure modes?

Optimistic locking under high contention causes retry storms — writers keep colliding and redoing work, collapsing throughput (though data stays correct). Pessimistic locking causes blocking and, if transactions acquire locks in different orders, deadlocks the database must detect and kill. Keep pessimistic locks short-lived and acquired in a consistent order to limit this.

Optimistic vs Pessimistic Locking · Vibe Engines · 2026
Finished this one? 0 / 139 Handbooks done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

Cite this page

Reference it in your work, paper or an AI's context window.

APASingh, S. (2026). Optimistic vs Pessimistic Locking. Vibe Engines. https://vibeengines.com/handbook/optimistic-vs-pessimistic-locking
MLASingh, Saurabh. “Optimistic vs Pessimistic Locking.” Vibe Engines, 2026, vibeengines.com/handbook/optimistic-vs-pessimistic-locking.
BibTeX
@online{vibeengines-optimistic-vs-pessimistic-locking,
  author       = {Singh, Saurabh},
  title        = {Optimistic vs Pessimistic Locking},
  year         = {2026},
  organization = {Vibe Engines},
  url          = {https://vibeengines.com/handbook/optimistic-vs-pessimistic-locking}
}