AlgoViz

Pessimistic locking under contention

medium 4 min

Acquire ownership before using a value to make a dependent decision.

Video lesson · Chapter 1 of 5

Two agents and one concert seat

0:00 / 3:00

Two buyers want the same pair of seats

1:10Reel

Theory

Read or listen first. About 1 min.

What it is

A pessimistic reservation obtains control of the inventory rows before relying on their availability. The holder checks the complete bundle while competing buyers wait.

Reserve two seats as a pair

TimeBuyer ALock 4Lock 9Book bothBuyer BWait for seat 4Every buyer requests 4 before 9.
A takes locks on seats 4 and 9 in order, checks both and commits the pair. B waits at the first lock.

Why it exists

A queued buyer can discover that availability changed while it waited. Checking the locked records gives the buyer a sound basis for its next decision.

Words to know

Lock acquisition order
The sequence in which an operation requests locks when it needs several resources.

How it works

Step through the idea, or press Play.

Step 1 of 3

Sort the seat IDs. Both buyers use the same resource order.

Understand it

Worked example

Pessimistic locking under contention in practice

A bundle requires two seats. Lock their inventory rows in increasing seat ID, verify both, and commit both claims. If either is unavailable, roll back without leaving half a bundle.

Related case

Who gets the last reservation?

A library releases a limited print edition at noon. Thousands see one copy available. A customer can reserve it for two minutes while checking out.

Two readers at the library desk
One copy at the library deskAdaticket ACopy 8availableBowaitingBoth readers see one copy

Compare item, owner, state and deadline.

Sources