← All concepts
Reliability & correctness
Distributed locks & holds
Stop two processes from claiming the same thing at once — a seat, a driver, a time slot.
Bookings and dispatch need exclusivity across many servers. A short-lived lock or hold in a shared store marks an item as taken while the rest of the process finishes.
See it in action
Step through the scenarios on the diagram.
Laying out diagram…
1/3Server A sets lock:seat:12A only if it doesn't exist — it succeeds and gets fencing token 33.
- Server A
- — Wants seat 12A
- Server B
- — Also wants seat 12A
- Lock store
- — lock:seat:12A with a TTL
- Bookings DB
- — Checks the fencing token
How it works
- Set a key only if absent, with a TTL (SET NX EX) so crashed holders release automatically.
- Or use database row locks / conditional updates (available ≥ qty).
- Fencing tokens prevent a stale holder from acting after its lock expired.
Trade-offs
- Clock and network issues make cache-based locks imperfect; critical money paths should rely on database constraints.
Seen in
- UberReal-time ride matching at global scale
- AmazonHigh-traffic e-commerce marketplace
- BookMyShowMovie and event tickets with live seat selection
- Airline reservationsFlights, seats, bookings and check-in for one airline
- AirbnbSearch, book and message for vacation rentals
- Meeting schedulerFind a time, book a room, invite everyone
- Car rentalReserve a car by class, pick up, drive, return
- Urban CompanyBook vetted professionals for home services
- Practo / teleconsultationFind doctors, book appointments, consult over video