Kelly Mears

Optimistic Locking

Letting concurrent writers proceed unchecked and catching conflicts only at commit time, betting that collisions are rare.

Data & Content2 min read278 words10 out · 7 in
also calledOptimistic concurrency control

Optimistic locking lets multiple transactions read and prepare to write the same row concurrently, without taking any lock up front, and only checks for a conflict at the moment of commit. The usual mechanism is a version number or timestamp column on the row: a transaction reads the row along with its current version, does its work, and on write includes a condition — "update this row only if its version still equals what I read" — that fails if anyone else committed a change in between. A failed check means the transaction retries against the fresh data rather than silently overwriting someone else's change.

This is the opposite bet from Pessimistic Locking, which stops other writers from touching a row at all for the duration of one transaction. Optimistic locking bets that two transactions rarely touch the same row at the same moment, so paying nothing up front and occasionally retrying is cheaper overall than making every writer wait behind a lock most of the time for a collision that mostly doesn't happen. Under low contention this is a clear win — no lock overhead, no writer blocked by a slow reader — but under high contention on the same rows, retries pile up and it can perform worse than a lock would have, because every failed attempt has to redo its work from scratch.

It fits naturally with stateless web request handling, where a "lock," if taken, would have to be held across an HTTP request and a possibly slow or abandoned client — exactly the scenario pessimistic locking handles badly, since a lock held by a client that never comes back blocks everyone else indefinitely.

See also9

ACID

The four guarantees a transactional database makes so concurrent, interrupted work never leaves data half-done.

Data & Content17 connections

Idempotence

The property that performing an operation twice has the same effect as performing it once.

Systems & Tooling34 connections

Optimistic UI

Updating the UI as if a server request has already succeeded, before the server has actually responded.

Web Platform21 connections

Race Condition

A defect whose occurrence depends on the relative timing of concurrent operations.

Testing & Verification36 connections

Isolation Level

How much concurrent transactions are allowed to see of each other's unfinished work, a dial between correctness and speed that most databases set lower than people assume.

Data & Content9 connections

Force Push

Overwriting a remote branch with local history that doesn't contain it, the operation that makes rewritten history shareable and silently deletes other people's work when used carelessly.

Version Control & Delivery12 connections

Lock-Free Data Structure

Concurrent data structures built on atomic hardware instructions instead of locks, so a stalled thread can never block the others, at the price of some of the hardest code in computing.

Computation & Algorithms7 connections

Time-of-Check to Time-of-Use

A race where a program checks a condition and then acts on it, but the world changes in between, the root of classic file-permission exploits and of double-spending bugs in web applications.

Systems & Tooling12 connections

Upsert

Inserting a row if it doesn't exist and updating it if it does, in one atomic statement, which is harder than it sounds because the obvious check-then-insert version races with itself.

Data & Content13 connections

Linked from7