What if a simple mistake in booking could cause chaos for hundreds of patients waiting for tests?
Why booking tests availability and concurrency in LLD - The Real Reasons
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run a small clinic and manage test bookings using a simple paper calendar. Patients call in, and you write down their appointments by hand. Sometimes two patients get booked for the same time slot because you missed updating the calendar quickly enough.
This manual method is slow and prone to errors. You can easily double-book a test slot, causing confusion and unhappy patients. It's hard to know in real-time which slots are free, and managing many bookings becomes overwhelming as your clinic grows.
By designing a system that tracks test availability and handles concurrency, you ensure that no two bookings overlap. The system updates instantly, preventing double bookings and showing real-time availability. This makes managing appointments smooth and reliable.
if slot not in booked_slots: booked_slots.append(slot) else: print('Slot already booked')
lock(slot) if slot not in booked_slots: book(slot) unlock(slot)
This approach enables seamless, real-time booking management that scales effortlessly as demand grows.
Online appointment systems for COVID-19 tests use availability and concurrency controls to prevent overbooking and long wait times.
Manual booking leads to errors and double bookings.
Concurrency control prevents overlapping appointments.
Real-time availability improves user experience and scalability.
Practice
Solution
Step 1: Understand concurrency in booking
Concurrency means multiple users try to book the same slot simultaneously.Step 2: Identify the problem caused by concurrency
If concurrency is not handled, multiple users can book the same slot, causing double bookings.Final Answer:
To prevent multiple users from booking the same slot at the same time -> Option CQuick Check:
Concurrency handling = prevent double bookings [OK]
- Thinking concurrency allows unlimited bookings
- Ignoring the need to prevent double bookings
- Assuming concurrency slows down the system intentionally
Solution
Step 1: Understand locking in booking systems
Locking a slot means reserving it temporarily to prevent others from booking it simultaneously.Step 2: Identify when to check availability
Availability must be checked and locked before confirming booking to avoid conflicts.Final Answer:
Lock the slot before confirming the booking -> Option BQuick Check:
Lock before confirm = correct availability check [OK]
- Checking availability after booking causes errors
- Ignoring availability checks leads to double bookings
- Relying on user honesty is not a system design
def book_slot(slot_id):
if is_available(slot_id):
reserve(slot_id)
confirm_booking(slot_id)
return 'Booked'
else:
return 'Unavailable'What issue can arise if two users call
book_slot at the same time for the same slot_id?Solution
Step 1: Analyze the code flow for concurrency
Both users check availability before reservation without locking, so both may see the slot as available.Step 2: Understand race condition effect
Without locking, both reserve and confirm booking, causing double booking.Final Answer:
Both users might get 'Booked' causing double booking -> Option AQuick Check:
Race condition = double booking risk [OK]
- Assuming system crashes automatically
- Thinking both get 'Unavailable' response
- Believing function serializes calls automatically
if check_availability(slot):
book(slot)Users report double bookings. What is the best fix?
Solution
Step 1: Identify the cause of double bookings
Without locking, multiple users can pass availability check simultaneously causing conflicts.Step 2: Apply concurrency control
Using locks or transactions ensures only one booking proceeds at a time for the same slot.Final Answer:
Add a lock or transaction around availability check and booking -> Option AQuick Check:
Locking fixes concurrency issues [OK]
- Removing availability check causes more errors
- Upgrading hardware does not fix concurrency logic
- Telling users to slow down is not a system fix
Solution
Step 1: Understand scalability needs
Thousands of users require a scalable approach that avoids bottlenecks.Step 2: Evaluate locking strategies
Single global lock serializes all requests causing delays; manual fixes cause poor user experience.Step 3: Choose optimistic locking with retries
This approach allows concurrent attempts, detects conflicts, retries, and updates availability promptly.Final Answer:
Use optimistic locking with retries and real-time slot availability updates -> Option DQuick Check:
Optimistic locking + updates = scalable concurrency [OK]
- Using global lock causes slow system
- Ignoring concurrency leads to double bookings
- Manual conflict fixes harm user experience
