4.4.20 · D3Databases

Worked examples — Optimistic vs pessimistic concurrency control

3,586 words16 min readBack to topic

This page is the exhaustive case-list for the parent topic. The parent gave you the idea (lock-first vs work-then-check) and the break-even inequality. Here we hit every corner: low/high contention, the exact break-even point, degenerate inputs (, ), write skew that single-row checks miss, deadlock, and a real-world flash sale. Every number asserted on this page is re-checked by machine code in the hidden Verify block that ships with this note (the same numbers you see in each "Verify:" line).

Before we start, one reminder of the two quantities everything hinges on — and where the optimistic formula comes from, since the parent stated it but this page leans on it in every cell:

Everything below is a specific point on the -axis or a specific anomaly shape. Let's name all the cells first.


The scenario matrix

Think of the whole topic as a grid. One axis is how likely a conflict is (the number ). The other axis is what shape the conflict has (same row? different rows? no real conflict at all?). Every worked example below is tagged with the cell it lands in.

Cell Class Concrete input What it stresses
A (no conflict ever) read-only report, degenerate low end: does the formula stay finite?
B (low contention) , , OCC should win comfortably
C (exact break-even) , , the tie — both cost the same
D (high contention) , , PCC should win
E (limiting / degenerate high end) flash sale, OCC cost (livelock)
F conflict shape = same row lost update, two ATMs row lock / version check catches it
G conflict shape = different rows write skew, two doctors single-row check misses it
H deadlock (PCC's own failure mode) T1↔T2 cross-lock why OCC never deadlocks
I real-world word problem inventory microservice pick a strategy from a story
J exam twist "is MVCC optimistic?" trap that mixes storage with strategy

We now walk every cell. Cells A–E are points along the contention axis; F–J are conflict-shape and reasoning cells.

Reading the figure below (in words): the horizontal axis is from to . The flat blue line at height is the pessimistic cost ms — flat because it never depends on . The rising orange curve is the optimistic cost — it starts at on the far left, climbs slowly, and shoots up toward the right edge as nears . The two curves cross at the dashed gray vertical line . Left of the crossing (green band) OCC is cheaper; right of it (red band) PCC is cheaper. Each red dot is labelled with its cell letter (A–E) right on the plot, so the "far-left dot" / "high dot" pointers in the text below always match a labelled point in the graphic.

Figure — Optimistic vs pessimistic concurrency control

Worked examples

Cell A — : no conflict ever


Cell B — : low contention


Cell C — : exact break-even


Cell D — : high contention


Cell E — : the limiting degenerate case


Cell F — conflict shape = same row (lost update)


Cell G — conflict shape = different rows (write skew)


Cell H — deadlock: PCC's own failure mode


Cell I — real-world word problem


Cell J — exam twist (the trap)


Recall Which cell is which — self-test

, cost of OCC ::: ms (retry term vanishes; formula stays finite). Why is excluded up front ::: because has a term that diverges to infinity there — it's a wall, not a value. What shared cost cancels from the comparison ::: the base read/compute/write cost , paid once under both strategies. Break-even with ::: . OCC cost at exact break-even ::: ms — a tie with PCC. OCC cost at (hot counter) ::: ms — PCC wins. Limit of OCC cost as ::: (livelock). Anomaly where writes hit different rows ::: write skew (needs predicate/serializable, not row check). Which strategy can deadlock ::: pessimistic (2PL); OCC aborts instead, never deadlocks. When does OCC validation run ::: at the atomic commit instant (write phase); the first to reach it wins, guaranteeing progress. Cell I decision (million SKUs, p=0.001) ::: optimistic — conflicts far below threshold. Is MVCC the same as optimistic ::: No — MVCC is storage, orthogonal to the strategy.