When optimistic concurrency beats pessimistic locking

abort_cost x conflict_rate >= lock_wait

Blocking spends latency to avoid wasted work, aborting spends work to avoid waiting, and measured contention decides which bill is smaller.

Two-phase locking and serializable snapshot isolation both guarantee serializable results, so the choice is about cost, not correctness. Two-phase locking acquires locks before it touches rows and holds them until commit, so a waiter pays for the holder's whole transaction. Snapshot isolation reads without locks and checks at commit, so a conflicting transaction pays by throwing away its finished work.

Under two-phase locking, each row has one lock with shared and exclusive modes. A read takes the shared lock, and many readers can hold it at once. A write needs exclusive mode, so it waits for every current reader to release before it upgrades. Locks accumulate during the transaction and release only at commit. Two transactions that each hold a shared lock and both upgrade wait on each other forever, so the system detects the cycle and aborts one. Snapshot isolation takes a different route. It reads a snapshot without locks, tracks each transaction's reads and writes, and at commit aborts on a dangerous structure, which the application then retries.

The abstraction hides that both schemes look identical at low load and diverge only under contention. The same code runs fast with one client and degrades with many. Consider one inventory row in a flash sale. Under snapshot isolation, many checkouts read it at once, one commits, and the rest abort and retry against the row that just changed, so retries multiply the load on the hottest key. Under two-phase locking, those checkouts queue on one lock, and the tail latency grows with queue length times lock hold time. If a transaction calls an email service while holding that lock, every client waits on the slowest email send in the system. Each scheme fails differently, and a single-client benchmark shows neither.

Measure the fraction of concurrent transactions whose read and write sets overlap, then price one abort: the whole transaction plus a retry. Choose optimistic concurrency when overlap is rare, transactions are short, and a retry is safe, meaning no external side effects or idempotent ones. Choose pessimistic locking when conflicts are common, when the work is expensive or cannot be repeated, or when a hot key needs a latency ceiling. Then keep lock hold times short and never call the network while holding a lock. If neither fits, remove the contention by partitioning the counter or routing the hot path through a queue. Track aborts and lock waits as separate metrics, because they are two bills and dashboards usually show only one.

orders of magnitude
lock wait
bounded by holder's transaction length

Time a waiter spends queued behind a lock holder. The holder's open duration sets the ceiling.

abort cost
whole transaction plus one retry

Work discarded at commit. It scales with transaction length, not with the statement that caused the conflict.

lock hold time
the full transaction under 2PL

Locks release only at commit, so any network call inside the transaction extends every waiter's delay.

retry rate
rises as overlap grows

Fraction of transactions aborted and rerun as overlap grows, so throughput can fall while the CPU stays busy.

field note

The naive design protects a write by taking a lock. Locking each row for the whole transaction and trusting the database to break deadlocks looks safe on paper. Under load, though, locks convert contention into waiting. One slow transaction holds everyone queued behind it, and the deadlock detector still wakes up to abort one of them anyway.

FoundationDB took the opposite route. Every transaction reads at a read version and buffers its writes, so nothing is locked while the work runs. At commit, the transaction fails if anything it read has changed. A five-second limit keeps each transaction short, so a retry costs little.

The lesson is that both designs abort sometimes. Locking aborts a deadlocked transaction, and optimistic commit aborts a conflicting one, so asking which design never aborts misses the point. The real question is which cost you can measure. Retries stay acceptable only when transactions are short and conflicts are rare, because each abort discards the whole transaction.