Course map

Topic 071 / Advanced

Concurrency control: MVCC, optimistic and pessimistic locking

Chapter 5.4 · Engines, Durability, Transactions, and Concurrency

What and why

Isolation levels (→ 70) state what anomalies are disallowed; concurrency control is how the engine or the application enforces that — by giving every reader a consistent snapshot, by locking rows before touching them, or by detecting conflicts after the fact and retrying. Builds on: → 70 (transactions and isolation levels).

Diagram

Mechanism How Cost Best for
MVCC readers see a snapshot; writers create new row versions old versions need cleanup read-heavy, mixed workloads
Pessimistic lock SELECT … FOR UPDATE blocks conflicting access contention, possible deadlock high-contention critical sections
Optimistic concurrency version column; write fails if version moved wasted retry under high contention low-contention updates

How it works

  1. MVCC: each transaction sees a consistent snapshot as of its start; writers create new row versions instead of overwriting, so readers never block writers and vice versa; old versions are eventually cleaned up.
  2. Pessimistic locking: acquire a lock before reading a row you intend to update (SELECT … FOR UPDATE); other transactions wanting the same row block until you commit or roll back — prevents the conflict outright, at the cost of contention and possible deadlock (→ 7).
  3. Optimistic concurrency: take no lock; each row carries a version number; update only WHERE version = <version just read>, incrementing it; zero rows affected means a conflict occurred, and the caller retries.
  4. Choose by contention: low contention favors optimistic (retries are rare and cheap); high contention on the same rows favors pessimistic (avoids repeatedly wasted retries).

Practical example

Fixing topic 70's lost update, pessimistically:

BEGIN;
SELECT balance FROM account WHERE id = 1 FOR UPDATE;   -- blocks a concurrent FOR UPDATE on id=1
UPDATE account SET balance = balance - 10 WHERE id = 1;
COMMIT;

Optimistically:

UPDATE account SET balance = :new_balance, version = version + 1
WHERE id = 1 AND version = :version_just_read;
-- 0 rows affected ⇒ someone else updated first; re-read and retry

When to use it

  • Use when: pessimistic locking for short, high-contention critical sections (a seat or inventory hold); optimistic for low-contention updates with a cheap retry path.
  • Avoid when: holding a pessimistic lock across a network call or user think-time — that's how lock pile-ups happen.
  • Failure scenarios: a deadlock from two transactions locking the same two rows in opposite order (→ 7); a busy optimistic retry loop burning work under contention that was actually high, where pessimistic locking would have been cheaper.
  • Simpler alternative: serializable isolation (→ 70) gives the same correctness automatically, at a throughput cost, for cases too intricate to hand-code locking for.

Practice

(mechanism + numeric, 20–30 min, lab Postgres — merges this chapter's concurrency exercise). Reproduce topic 70's lost update, then fix it three ways — serializable isolation, SELECT … FOR UPDATE, and a version column — and run the same small concurrent workload (e.g., 10 parallel updaters hammering one row) against each. Compare throughput and retry counts. Done when all three are provably correct under concurrency and you can state which was fastest and why.

Shared lab setup

Key takeaway

MVCC, pessimistic locking, and optimistic concurrency are three ways to enforce the same correctness; pick by measured contention, not habit. Done when you can: fix a reproduced lost update three ways and explain the throughput difference. Used next in → 72; Projects 1, 7.

References

Kleppmann and Riccomini, Designing Data-Intensive Applications — see references.md.

Source directory

Search the guide