Bird
Raised Fist0
LLDsystem_design~7 mins

Booking conflict resolution in LLD - System Design Guide

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 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.
Solution
The system checks availability before confirming a booking and locks the resource during the booking process to prevent others from booking it simultaneously. If a conflict is detected, the system rejects or queues the request, ensuring only one booking succeeds for the same time slot.
Architecture
User 1 Client
Booking Server
User 2 Client
─────────┘

This diagram shows two users attempting to book the same resource. The booking server checks availability and locks the resource to prevent conflicts.

Trade-offs
✓ Pros
Prevents double bookings by locking resources during booking.
Ensures data consistency and user trust in the booking system.
Can handle concurrent booking requests safely.
✗ Cons
Locking can reduce system throughput under very high load.
Complexity increases with distributed systems needing distributed locks.
Potential for deadlocks or resource starvation if not managed carefully.
Use when booking resources that cannot be double-booked and when concurrent booking requests exceed 100 per second.
Avoid if booking conflicts are rare (under 10 per hour) or if eventual consistency is acceptable for the application.
Real World Examples
Airbnb
Prevents double booking of rental properties by locking availability during the booking process.
Uber
Resolves conflicts when multiple riders request the same driver simultaneously by locking driver availability.
Booking.com
Ensures hotel room availability is locked during booking to avoid overbooking.
Code Example
The before code allows double bookings because it does not lock the resource during booking. The after code uses locks per resource and time slot to ensure only one booking can succeed at a time, preventing conflicts.
LLD
### 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
OutputSuccess
Alternatives
Optimistic concurrency control
Allows multiple bookings to proceed but checks for conflicts before final commit, rolling back if conflicts exist.
Use when: Choose when conflicts are rare and system prefers higher throughput over immediate locking.
Eventual consistency with compensation
Accepts bookings immediately and resolves conflicts later by compensating or notifying users.
Use when: Choose when immediate consistency is not critical and user experience can tolerate delays.
Summary
Booking conflict resolution prevents double bookings by locking resources during the booking process.
It ensures data consistency and user trust by handling concurrent requests safely.
Choosing the right conflict resolution strategy depends on system load and tolerance for conflicts.

Practice

(1/5)
1. What is the primary goal of booking conflict resolution in a system?
easy
A. To ignore booking times and accept all requests
B. To allow multiple bookings at the same time for efficiency
C. To delete all previous bookings automatically
D. To prevent overlapping reservations for the same resource

Solution

  1. Step 1: Understand booking conflict concept

    Booking conflict resolution ensures no two bookings overlap for the same resource.
  2. Step 2: Identify the goal of conflict resolution

    The goal is to prevent double-booking by checking time overlaps and rejecting or adjusting conflicting bookings.
  3. Final Answer:

    To prevent overlapping reservations for the same resource -> Option D
  4. Quick Check:

    Conflict resolution = prevent overlaps [OK]
Hint: Conflict resolution means no double bookings allowed [OK]
Common Mistakes:
  • Thinking multiple bookings at same time are allowed
  • Assuming conflict resolution deletes bookings
  • Ignoring time overlaps in bookings
2. Which of the following code snippets correctly checks if two time intervals (start1, end1) and (start2, end2) overlap?
easy
A. if start1 < end2 and start2 < end1: overlap
B. if start1 > end2 or start2 > end1: overlap
C. if end1 <= start2 or end2 <= start1: no overlap
D. if start1 == end2 or start2 == end1: overlap

Solution

  1. Step 1: Understand time interval overlap condition

    Two intervals overlap if one starts before the other ends and vice versa.
  2. Step 2: Match condition to code

    Condition start1 < end2 and start2 < end1 correctly detects overlap.
  3. Final Answer:

    if start1 < end2 and start2 < end1: overlap -> Option A
  4. Quick Check:

    Overlap check = start1 < end2 and start2 < end1 [OK]
Hint: Overlap if intervals cross each other in time [OK]
Common Mistakes:
  • Using <= instead of < causing false negatives
  • Confusing no overlap with overlap conditions
  • Checking equality as overlap incorrectly
3. Given existing bookings: [(10, 12), (14, 16), (18, 20)], what will be the result of checking a new booking (12, 14) for conflict using the overlap condition start1 < end2 and start2 < end1?
medium
A. Conflict with (10, 12)
B. Conflict with (14, 16)
C. No conflict
D. Conflict with all existing bookings

Solution

  1. Step 1: Check overlap with each existing booking

    Check (12,14) against (10,12): 12 < 12 is false, no overlap. Against (14,16): 12 < 16 true, 14 < 14 false, no overlap. Against (18,20): no overlap.
  2. Step 2: Determine conflict result

    No overlaps found with any existing booking intervals.
  3. Final Answer:

    No conflict -> Option C
  4. Quick Check:

    New booking fits between existing without overlap [OK]
Hint: Check each existing booking for overlap carefully [OK]
Common Mistakes:
  • Assuming touching intervals overlap
  • Ignoring strict less than condition
  • Confusing start and end times
4. Identify the bug in this booking conflict check code snippet:
def is_conflict(new_start, new_end, existing_bookings):
    for start, end in existing_bookings:
        if new_start <= end and new_end >= start:
            return True
    return False
medium
A. The condition incorrectly uses <= and >= causing false conflicts
B. The condition allows bookings that end exactly when another starts
C. The function does not return anything
D. The loop does not iterate over bookings

Solution

  1. Step 1: Analyze the overlap condition

    Condition new_start <= end and new_end >= start includes cases where bookings just touch at edges, causing false conflicts.
  2. Step 2: Correct condition for strict overlap

    Use new_start < end and new_end > start to detect true overlaps only.
  3. Final Answer:

    The condition incorrectly uses <= and >= causing false conflicts -> Option A
  4. Quick Check:

    Use strict inequalities for overlap [OK]
Hint: Use < and >, not <= or >= for overlap checks [OK]
Common Mistakes:
  • Using inclusive operators causing false positives
  • Forgetting to return a boolean
  • Not iterating over all bookings
5. You are designing a booking system for meeting rooms. To handle conflict resolution at scale, which approach is best to ensure no overlapping bookings and high performance?
hard
A. Use a centralized lock on the entire booking database for each new booking
B. Check for conflicts by querying only relevant time slots and use optimistic concurrency control
C. Allow all bookings and resolve conflicts manually later
D. Store bookings without timestamps and rely on user honesty

Solution

  1. Step 1: Understand scalability and conflict resolution needs

    Centralized locking (Use a centralized lock on the entire booking database for each new booking) causes bottlenecks; manual or no checks (Options C, D) cause errors.
  2. Step 2: Choose efficient conflict detection method

    Querying only relevant time slots reduces load; optimistic concurrency control handles race conditions efficiently.
  3. Final Answer:

    Check for conflicts by querying only relevant time slots and use optimistic concurrency control -> Option B
  4. Quick Check:

    Efficient conflict check + concurrency control = scalable solution [OK]
Hint: Query relevant slots + optimistic control for scalable conflict resolution [OK]
Common Mistakes:
  • Using global locks causing slowdowns
  • Ignoring concurrency issues
  • Not filtering bookings by time before checking