Bird
Raised Fist0
LLDsystem_design~7 mins

Why booking tests availability and concurrency in LLD - Why This Architecture

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Problem Statement
When multiple users try to book the same test slot at the same time, the system can allow double bookings or fail to update availability correctly. This causes confusion, lost revenue, and poor user experience because the system does not handle concurrent requests properly.
Solution
The system manages test slot availability by controlling access to booking data so that only one booking can succeed for a slot at a time. It uses concurrency control techniques like locking or atomic operations to ensure that availability updates happen safely without conflicts, preventing double bookings.
Architecture
User 1
(Request) ─┼──────▶
Service ├──────▶
User 2
(Request) ─┼───────────┘
Lock/Mutex
Lock/Mutex

This diagram shows two users sending booking requests to the booking service. The service uses a lock or mutex to control access to the database test slot records, ensuring only one booking updates availability at a time.

Trade-offs
✓ Pros
Prevents double booking by serializing access to test slot availability.
Maintains data consistency and accurate availability status.
Improves user trust by avoiding booking conflicts.
✗ Cons
Locking can reduce system throughput under very high concurrency.
Complexity increases with distributed locks or multi-datacenter setups.
Potential for deadlocks or increased latency if not implemented carefully.
Use when your system handles multiple simultaneous booking requests for the same test slots, especially if you expect more than 100 concurrent users.
If your booking volume is very low (under 10 concurrent requests) or test slots are unique per user, the overhead of concurrency control may not be justified.
Real World Examples
Uber
Uber prevents multiple drivers from accepting the same ride request by locking the ride assignment, ensuring only one driver can book the ride.
Airbnb
Airbnb manages concurrent booking requests for the same property dates by locking availability calendars to avoid double bookings.
Amazon
Amazon controls inventory availability during flash sales by locking stock counts to prevent overselling.
Code Example
The before code reads and updates availability without any protection, so two requests can read the same available count and both book, causing double booking. The after code uses a lock to ensure only one thread updates availability at a time, preventing conflicts.
LLD
### Before: No concurrency control, leads to double booking
class BookingService:
    def book_slot(self, slot_id):
        slot = database.get_slot(slot_id)
        if slot.available > 0:
            slot.available -= 1
            database.save_slot(slot)
            return True
        return False

### After: Using a lock to prevent concurrent updates
import threading
lock = threading.Lock()

class BookingService:
    def book_slot(self, slot_id):
        with lock:
            slot = database.get_slot(slot_id)
            if slot.available > 0:
                slot.available -= 1
                database.save_slot(slot)
                return True
            return False
OutputSuccess
Alternatives
Optimistic Concurrency Control
Instead of locking, it checks for conflicts after the fact and retries if conflicts occur.
Use when: Use when conflicts are rare and you want to maximize throughput without locking overhead.
Eventual Consistency with Conflict Resolution
Allows temporary inconsistencies and resolves conflicts asynchronously later.
Use when: Use when immediate consistency is not critical and system can tolerate short-term conflicts.
Summary
Concurrent booking requests can cause double bookings without proper control.
Using locks or atomic operations ensures only one booking updates availability at a time.
This approach improves data consistency and user experience but may reduce throughput under high load.

Practice

(1/5)
1. Why is it important to handle concurrency when booking test slots in a system?
easy
A. To allow unlimited bookings for the same slot
B. To slow down the booking process intentionally
C. To prevent multiple users from booking the same slot at the same time
D. To avoid showing available slots to users

Solution

  1. Step 1: Understand concurrency in booking

    Concurrency means multiple users try to book the same slot simultaneously.
  2. Step 2: Identify the problem caused by concurrency

    If concurrency is not handled, multiple users can book the same slot, causing double bookings.
  3. Final Answer:

    To prevent multiple users from booking the same slot at the same time -> Option C
  4. Quick Check:

    Concurrency handling = prevent double bookings [OK]
Hint: Concurrency means multiple users booking simultaneously [OK]
Common Mistakes:
  • Thinking concurrency allows unlimited bookings
  • Ignoring the need to prevent double bookings
  • Assuming concurrency slows down the system intentionally
2. Which of the following is a correct way to ensure availability checks during booking in a system?
easy
A. Check slot availability after booking confirmation
B. Lock the slot before confirming the booking
C. Allow booking without checking availability
D. Ignore concurrency and rely on user honesty

Solution

  1. Step 1: Understand locking in booking systems

    Locking a slot means reserving it temporarily to prevent others from booking it simultaneously.
  2. Step 2: Identify when to check availability

    Availability must be checked and locked before confirming booking to avoid conflicts.
  3. Final Answer:

    Lock the slot before confirming the booking -> Option B
  4. Quick Check:

    Lock before confirm = correct availability check [OK]
Hint: Lock slot before booking to avoid conflicts [OK]
Common Mistakes:
  • Checking availability after booking causes errors
  • Ignoring availability checks leads to double bookings
  • Relying on user honesty is not a system design
3. Consider this simplified booking flow code snippet:
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?
medium
A. Both users might get 'Booked' causing double booking
B. The system crashes due to race condition
C. Both users get 'Unavailable' response
D. Only one user can call the function at a time automatically

Solution

  1. Step 1: Analyze the code flow for concurrency

    Both users check availability before reservation without locking, so both may see the slot as available.
  2. Step 2: Understand race condition effect

    Without locking, both reserve and confirm booking, causing double booking.
  3. Final Answer:

    Both users might get 'Booked' causing double booking -> Option A
  4. Quick Check:

    Race condition = double booking risk [OK]
Hint: Check-then-act without lock causes double booking [OK]
Common Mistakes:
  • Assuming system crashes automatically
  • Thinking both get 'Unavailable' response
  • Believing function serializes calls automatically
4. In a booking system, the code uses a simple availability check without locking:
if check_availability(slot):
    book(slot)

Users report double bookings. What is the best fix?
medium
A. Add a lock or transaction around availability check and booking
B. Remove availability check to speed up booking
C. Increase server hardware to handle more requests
D. Notify users to book slower

Solution

  1. Step 1: Identify the cause of double bookings

    Without locking, multiple users can pass availability check simultaneously causing conflicts.
  2. Step 2: Apply concurrency control

    Using locks or transactions ensures only one booking proceeds at a time for the same slot.
  3. Final Answer:

    Add a lock or transaction around availability check and booking -> Option A
  4. Quick Check:

    Locking fixes concurrency issues [OK]
Hint: Use locks or transactions to fix concurrency bugs [OK]
Common Mistakes:
  • Removing availability check causes more errors
  • Upgrading hardware does not fix concurrency logic
  • Telling users to slow down is not a system fix
5. You are designing a test booking system that must handle thousands of users booking slots concurrently. Which design approach best ensures availability and prevents double bookings?
hard
A. Show all slots as available and accept bookings first come, first served
B. Allow users to book without checks and fix conflicts later manually
C. Use a single global lock for all bookings to serialize requests
D. Use optimistic locking with retries and real-time slot availability updates

Solution

  1. Step 1: Understand scalability needs

    Thousands of users require a scalable approach that avoids bottlenecks.
  2. Step 2: Evaluate locking strategies

    Single global lock serializes all requests causing delays; manual fixes cause poor user experience.
  3. Step 3: Choose optimistic locking with retries

    This approach allows concurrent attempts, detects conflicts, retries, and updates availability promptly.
  4. Final Answer:

    Use optimistic locking with retries and real-time slot availability updates -> Option D
  5. Quick Check:

    Optimistic locking + updates = scalable concurrency [OK]
Hint: Optimistic locking scales better than global locks [OK]
Common Mistakes:
  • Using global lock causes slow system
  • Ignoring concurrency leads to double bookings
  • Manual conflict fixes harm user experience