| Scale | Users | Rooms | Bookings per day | System Changes |
|---|---|---|---|---|
| Small | 100 | 500 | 200 | Single server, simple database, no caching |
| Medium | 10,000 | 50,000 | 10,000 | Load balancer, read replicas, caching for availability checks |
| Large | 1,000,000 | 500,000 | 100,000 | Sharded database, distributed cache, asynchronous booking processing |
| Very Large | 100,000,000 | 10,000,000 | 10,000,000 | Multi-region deployment, event-driven architecture, CDN for static content |
Hotel, Room, Booking classes in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
At small to medium scale, the database is the first bottleneck. It handles all booking transactions and availability checks. As users and bookings grow, the database CPU and I/O get overwhelmed, causing slow responses and possible booking conflicts.
- Database Read Replicas: Offload read queries like room availability checks to replicas to reduce load on the primary database.
- Caching: Use in-memory caches (e.g., Redis) for frequently accessed data like room availability to reduce database hits.
- Horizontal Scaling: Add more application servers behind a load balancer to handle increased user requests.
- Database Sharding: Split booking data by hotel or region to distribute load across multiple database instances.
- Asynchronous Processing: Use message queues to handle booking confirmation and payment processing asynchronously to improve responsiveness.
- Multi-region Deployment: Deploy services closer to users to reduce latency and improve availability.
- At 10,000 bookings/day (~0.12 QPS), a single database can handle the load easily.
- At 100,000 bookings/day (~1.2 QPS), database load increases; read replicas and caching become necessary.
- Storage: Each booking record ~1 KB, so 10 million bookings need ~10 GB storage, manageable with modern databases.
- Network bandwidth: Assuming 1 KB per booking request/response, 100,000 bookings/day require ~100 MB data transfer, well within 1 Gbps network capacity.
Start by describing the system components and their roles (Hotel, Room, Booking). Then discuss expected traffic and data growth. Identify the first bottleneck (usually the database). Propose scaling solutions step-by-step, explaining why each is needed. Use real numbers to justify your choices. Finally, mention trade-offs and monitoring strategies.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Add read replicas to distribute read queries and reduce load on the primary database. Also, implement caching for frequent read operations like room availability checks. This reduces database CPU and I/O usage, preventing overload.
Practice
Solution
Step 1: Understand the role of each class
The Hotel class manages the overall hotel, Booking handles reservations, and Room stores details about each room.Step 2: Identify which class holds room details
Since Room is designed to represent individual rooms, it stores room number, type, and availability.Final Answer:
Room -> Option AQuick Check:
Room class = stores room info [OK]
- Confusing Hotel with Room class
- Thinking Booking stores room details
- Assuming Guest class stores room info
Solution
Step 1: Recall Python constructor syntax
Python constructors use the special method __init__ with self as the first parameter.Step 2: Match the correct method signature
def __init__(self, room, guest, date): correctly uses def __init__(self, room, guest, date): which is the standard constructor format.Final Answer:
def __init__(self, room, guest, date): -> Option AQuick Check:
Constructor = __init__ method [OK]
- Using method name other than __init__
- Omitting self parameter
- Using class name as method name
class Room:
def __init__(self, number):
self.number = number
self.is_available = True
class Booking:
def __init__(self, room):
self.room = room
self.room.is_available = False
room101 = Room(101)
print(room101.is_available)
booking1 = Booking(room101)
print(room101.is_available)Solution
Step 1: Check initial availability of room101
When room101 is created, is_available is set to True, so first print outputs True.Step 2: Booking changes room availability
Booking constructor sets room101.is_available to False, so second print outputs False.Final Answer:
True\nFalse -> Option DQuick Check:
Initial True, then set False by Booking [OK]
- Assuming availability stays True after booking
- Confusing order of prints
- Ignoring side effect on room object
class Room:
def __init__(self, number):
self.number = number
self.is_available = True
class Booking:
def __init__(self, room, guest):
self.room = room
self.guest = guest
def book(self):
if self.room.is_available:
self.room.is_available = False
print("Booking successful")
else:
print("Room not available")
room = Room(201)
booking = Booking(room)
booking.book()Solution
Step 1: Check Booking constructor parameters
Booking __init__ requires room and guest, but only room is passed when creating booking instance.Step 2: Identify missing argument error
Omitting guest argument causes a TypeError at runtime.Final Answer:
Missing guest argument when creating Booking instance -> Option BQuick Check:
Constructor args mismatch = missing guest [OK]
- Ignoring missing guest argument
- Assuming book method must return value
- Thinking is_available must be a method
Solution
Step 1: Analyze class responsibilities
Hotel should manage Rooms, Booking should link Rooms and Guests, keeping clear separation.Step 2: Evaluate design for scalability
Have Hotel class contain a list of Room objects, and Booking class references Room and Guest; Hotel checks availability before booking. cleanly separates concerns, allowing Hotel to check availability and Booking to handle reservations, supporting easy maintenance and scaling.Final Answer:
Hotel manages Rooms; Booking references Room and Guest; Hotel checks availability -> Option CQuick Check:
Separation of concerns = Have Hotel class contain a list of Room objects, and Booking class references Room and Guest; Hotel checks availability before booking. [OK]
- Combining all logic in one class
- Booking managing Rooms directly
- Ignoring availability checks in Hotel
