Design a hotel reservation system
Lead BankSoftware Engineer · OnsiteLast updated
This problem tests your ability to design a distributed reservation system that handles high concurrency, prevents double-booking, and maintains eventual consistency with external payment services.
Problem Statement
Design a hotel reservation system that allows users to search for room availability and book rooms. The system needs to support high read traffic for search and high write traffic for bookings during peak seasons.
Functional Requirements
- Users can search for rooms based on location, check-in/check-out dates, and room type.
- Users can initiate a booking for a specific room.
- The booking process includes a 10-minute hold where the room is reserved while the user pays.
- Integration with a third-party payment gateway is required to finalize the booking.
Non-Functional Requirements
- Consistency: Prevent double-booking at all costs.
- Availability: The search function must be highly available and responsive.
- Reliability: Ensure that if a payment succeeds, the reservation is recorded even if internal services crash.
Deliverables
- API Design: Define the primary endpoints for searching and booking.
- Data Model: Propose a schema for rooms, reservations, and hold states.
- Concurrency Strategy: Explain how you will handle race conditions when two users attempt to book the last room simultaneously.
- System Architecture: Describe the components involved, focusing on how to manage the transition from 'held' to 'confirmed' status after the asynchronous payment call.
Hints
Hint 1
Consider using a distributed lock or a database-level atomic update (e.g., SELECT FOR UPDATE or a version column) to ensure that a room cannot be reserved twice.
Hint 2
Think about using a state machine for the booking process (e.g., PENDING, HELD, CONFIRMED, CANCELLED) to track the transition from search to payment.
Hint 3
Use a message queue or a reliable event-driven approach to decouple the reservation service from the third-party payment provider to handle timeouts and retries gracefully.
Follow-up questions
- How would you handle the 'zombie' bookings where a user initiates a hold but never completes the payment process?
- How would you scale the search service if the system needs to support millions of searches per second across different regions?
- If the third-party payment provider experiences a 30-second outage, how does your system ensure the user's booking isn't prematurely cancelled?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.