The hold that frees itself

You hold a seat and walk away. How does it come back?

The lesson

  1. Do not wait for a job to free a held seat.
  2. The expiry time is already on the row,
  3. so check it when the next person asks.

The code

the table one row for each seat
UPDATE show_seat
the change mark it held, by Rahul
SET status = 'held', held_by = 'Rahul',
expiry time ten minutes from now
held_until = now() + interval '10 minutes'
which row only the row of seat 14A
WHERE seat_id = '14A'
the check if the seat is free
AND (status = 'available'
or or its hold has run out
OR (status = 'held' AND held_until < now()));
1 row changed
it was free, or its hold had run out: the seat is yours
0 rows changed
someone still has it

What this lab leaves out

  • This is the first lab's query, plus one line. It also writes held_until, the time the hold ends. The first lab left that column out.
  • Two people asking for a run-out seat at once. They still get one winner, for the reason the first lab gives: the check is inside the change.
  • A hold that ends while she is paying. Here the holder had walked away. If she is in the middle of paying when the time passes, the video moves the seat to a separate state, "being paid for", with its own deadline, so it is not given to someone else under a live payment. No lab plays this yet.
  • Two other ways you will hear. A cleanup job on a schedule works, with a gap between when a seat should be free and when the job gets to it. A key in Redis with a timer deletes itself, but the message that it expired can be missed, and then the seat stays held with no timer left. The video keeps the time on the row for that reason.

In the video

This idea is taught at 19:31 in the video, with the rest of the design around it.