Skip to content
Preptima
mediumCodingDesignMidSenior

Two users click "book" on the last seat in your in-memory booking class at the same moment. Where does the lock go?

The bug is a check-then-act race condition: both threads see the seat free before either marks it taken. The lock must cover the check and the write together, scoped to one show so unrelated shows never wait, and released before payment, with a timed hold standing in for the seat while the user pays.

5 min readUpdated 2026-10-10Target archetype: Big Tech, Enterprise Captive, Product Startup

Answer

Short answer

The lock goes around the decision, not around the data. Booking is a check-then-act sequence: read "is this seat free", then write "this seat is mine", and the race condition lives in the gap between the two. Hold one lock across both steps, scope it to the show so other shows never contend, and release it before payment, leaving a timed hold on the seat instead.

Was this answer helpful?

Likely follow-ups

  • A user selects four adjacent seats and wants all of them or none. What changes in where the lock goes?
  • How would you write a test that fails reliably against the unlocked version of book?
  • The hold expires while the payment provider is still processing the charge. What does confirm do, and who refunds?
  • Why does making the seat status field volatile not fix this?

Related questions

Related Topics