Skip to content
This repository was archived by the owner on Oct 25, 2026. It is now read-only.

Booking and Locking

Syed Ahmer Shah edited this page Oct 25, 2026 · 2 revisions

The booking flow

sequenceDiagram
    autonumber
    actor C as Customer
    participant App as Laravel
    participant DB as MySQL
    participant Q as Queue
    C->>App: Pick seats (POST cart)
    App->>DB: BEGIN · lock user → show → seats → cart (FOR UPDATE)
    App->>DB: check v_seat_availability, insert cart_items
    DB-->>App: COMMIT (seats held 10 min)
    C->>App: Confirm checkout (idempotency key)
    App->>DB: BEGIN · same lock order · re-check hold, prices, coupon, gift card, points
    App->>DB: insert booking + booking_seats (seat_lock = 1), delete cart
    DB-->>App: COMMIT
    App->>Q: receipt email, loyalty, notifications
    App-->>C: QR e-ticket
Loading

Booking rules

  • Up to 4 seats per booking, one show per booking. Adult and child tickets can be mixed, with at least one adult.
  • Seats are held for 10 minutes with a countdown. Expired holds go back on sale and the show's waitlist is told.
  • Unpaid bookings can be cancelled online until 2 hours before the show; seats, coupon use, gift card balance and points are returned.

Status transitions

stateDiagram-v2
    [*] --> confirmed: checkout
    confirmed --> completed: show finished
    confirmed --> no_show: not admitted
    confirmed --> cancelled: customer or admin
    completed --> [*]
    no_show --> [*]
    cancelled --> [*]
Loading

Allowed moves live in Booking::STATUS_TRANSITIONS. Booking::claimStatus() runs UPDATE ... WHERE id = ? AND booking_status = <expected> and throws StaleStatusException if nothing matched, so a booking can't be refunded twice.

Why a seat can't be sold twice

Level Mechanism
Row locks SELECT ... FOR UPDATE in a transaction, always in the order user → show → seats → cart, so two requests can't deadlock each other
Status lock Compare-and-set on booking_status (above)
Unique constraint booking_seats (show_id, seat_id, seat_lock): seat_lock is 1 while sold and NULL once released, so MySQL refuses a second live ticket
Idempotency keys Checkout carries a client key (bookings.idempotency_key, unique); a double-click returns the booking already made
Guarded updates Conditional UPDATEs for counters and one-time notifications
Atomic cache locks ProcessShowWaitlist is ShouldBeUnique and WithoutOverlapping; scheduled tasks use withoutOverlapping()
Deadlock retry Booking transactions use DB::transaction(..., 3)

The full story, including the IDOR and key-handling fixes, is in One seat, two buyers: race conditions, IDOR and keys in BookMyMovie.

Clone this wiki locally