AlgoViz

Design a movie ticket booking system

hard 90 min

Sell the numbered seats of a cinema: choose, hold for ten minutes, pay, cancel.

Watch lesson4:35

Where it shows up

The same pattern appears across everyday software.

  • pretix: carts that end

    In the API of this open source ticket shop a cart position has an `expires` time, after which it no longer blocks quota. A hold with an end, and a comparison.

  • pretix: seats and check-in

    A seat knows the order position or cart position that has it, and whether it is blocked by hand. A check-in can be refused as already redeemed, canceled or unpaid.

  • seats.io: hold tokens

    Its API holds objects temporarily with a hold token, and booking held seats means sending that token. The booking number of this page, seen from outside.

  • Stripe Checkout: limited inventory

    The guide for limited stock sets an expiration time for a payment session, between 30 minutes and 24 hours, so that unpaid items return to sale.

  • Stripe: place a hold on a card

    Authorize now, capture later. Authorizations end after some days that depend on the card network: the listed window for online card payments is 7 days for the large networks, and shorter in some cases.

  • Stripe: idempotent requests

    A key makes a retry safe, parameters are compared with the first request, and keys may be removed after at least 24 hours.

  • Redis: SET with NX and PX

    The page on distributed locks sets a random value with a time limit and removes it only if it is still the same value. It advises fencing tokens where correctness matters.

  • PostgreSQL: row locks and deadlocks

    The manual says that the best defense against deadlocks is to take locks on several objects in a consistent order. Our sorted seats.

  • Java: DelayQueue

    A queue whose elements can be taken only when their delay has expired. A ready-made sweeper for one process.

  • Caffeine: refresh

    Refreshing loads a new value in the background while the old one is still returned: the programme cache of the search page.