AlgoViz

Who gets the last reservation?

medium 6 min

A library releases a limited print edition at noon.

Video lesson · Chapter 1 of 5

Two promises about one library book

0:00 / 3:44

The last copy changes hands

1:13Reel

Theory

Read or listen first. About 1 min.

What it is

A shop can show the same available copy to many readers. The promise begins when a write changes a particular copy from available to held. Store the customer’s hold identity and its deadline together. The identity distinguishes successive holders even if the item leaves and later returns to the same visible state.

Ownership changes when a hold expires

Last item10:04Held by Ada · H1Incoming requestClaim for Bo → waitExpiry at 10:05 lets unused stock return.
Ada holds the last item until 10:05. A competing claim must wait or fail while Ada’s hold is active.

Why it exists

Keeping a database transaction open while somebody enters payment details would tie a lock and a connection to human response time. A short transaction can instead record a durable hold. Other requests inspect that state and finish quickly while the customer checks out.

View diagram · Claiming one copy
Checkout → Guarded write (confirm hold); Expiry worker → Guarded write (expire hold); Guarded write → Hold record (match identity)
Matching the active hold prevents cleanup from touching Bo’s purchase.
Full size

Words to know

Hold identity
A unique claim on one item during one reservation period.
Conditional write
A mutation that succeeds only if the stored state still satisfies its predicate.

How it works

Step through the idea, or press Play.

Step 1 of 3

Make one durable claim. Conditionally change available to held, recording the hold ID and deadline. Affected rows tell the caller whether it won. A waiting room bounds traffic but cannot enforce ownership.

Understand it

Start here

Two readers at the library desk

Ada wants the only copy of a new book. The desk gives her two minutes to collect it. Watch the ticket attached to the book when Bo arrives after her time runs 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.

Structure

Claiming one copy

Claiming one copy

Checkout → Guarded write (confirm hold); Expiry worker → Guarded write (expire hold); Guarded write → Hold record (match identity)
Matching the active hold prevents cleanup from touching Bo’s purchase.

Two buyers, one copy

A acquires hold 12 at noon. At 12:03 B replaces the expired hold with hold 13. Now deliver A’s confirmation and its delayed cleanup in either order. Both must affect zero rows. B must still be able to confirm once.

The lifetime of one copy

The previous buyer returns

TimeRequestResult
12:00Ada claims copy 8Hold A expires at 12:02
12:03Bo claims expired copy 8Hold B replaces hold A
12:04Ada confirms hold AOwner check fails
12:05Delayed cleanup for A runsB’s hold remains intact

Where to spend the contention budget

For low contention, attempt a conditional update once and tell the loser the item is unavailable. For a bundle of items, acquiring rows in a stable order within one short transaction can simplify all-or-nothing allocation. During a popular sale, repeated losing writes can dominate database time. Limit admission before the database and monitor attempts per successful reservation.

Payment can outlive the hold

Suppose payment authorization succeeds just after the reservation expires. Treat fulfillment and payment as distinct states. Verify the hold before confirming the item and record a recoverable outcome if payment needs to be voided. Test the ordering where the payment result arrives twice and the cleanup process runs in between.

Match the hold

  • Compare item, owner, state and deadline.

Match only the item

  • An older timer can release a newer claim.

Summary

The whole topic in one minute.

A hold needs an identity, an authoritative deadline and guarded transitions. Cleanup improves availability; the confirmation path enforces ownership.

Sources