Skip to content
Preptima
hardDesignCodingMidSeniorStaff

One global lock makes your reservation system correct and slow. How do you split it without deadlocking?

Shrink the critical section first, then replace the global lock with lock striping, a fixed array of locks keyed by resource hash. Lock ordering by ascending stripe index rules out deadlock for multi-resource bookings, tryLock bounds waiting, and invariants the old lock covered by accident each need an owner.

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

Answer

Short answer

Split the lock by resource, using lock striping, and keep one global order for taking several locks. Each room or seat hashes to one of a fixed set of locks. A booking that needs several resources sorts their stripe indexes and locks them in ascending order, so no cycle can form. Add tryLock with a deadline to bound waiting, and give an explicit owner to every invariant the global lock was guarding without anyone noticing.

Was this answer helpful?

Likely follow-ups

  • How do you pick the number of stripes, and what does choosing too few cost you?
  • A user may hold at most three active reservations across all rooms. Which lock guards that, and where does it sit in the order?
  • The availability screen must show every free room at 10:00 consistently. Do you take all the stripes, or accept a snapshot?
  • Two bookings both time out on tryLock and retry together forever. What stops that?

Related questions