Problem Statement
When multiple users try to book the same resource (like a hotel room or a meeting slot) at the same time, conflicts occur. Without proper handling, double bookings happen, causing customer frustration and operational chaos.
This diagram shows two users attempting to book the same resource. The booking server checks availability and locks the resource to prevent conflicts.
### Before: No conflict resolution class BookingSystem: def __init__(self): self.bookings = {} def book(self, resource_id, time_slot): if (resource_id, time_slot) in self.bookings: return False # Double booking possible but not prevented self.bookings[(resource_id, time_slot)] = True return True ### After: With locking to prevent conflicts import threading class BookingSystem: def __init__(self): self.bookings = {} self.locks = {} self.global_lock = threading.Lock() def book(self, resource_id, time_slot): key = (resource_id, time_slot) with self.global_lock: if key not in self.locks: self.locks[key] = threading.Lock() lock = self.locks[key] with lock: if key in self.bookings: return False # Conflict detected self.bookings[key] = True return True