1. Domain Model: Hotels, Rooms & Reservations
A hotel has multiple rooms of different types (Standard, Deluxe, Suite). A reservation spans a date range `[checkInDate, checkOutDate)`. Two reservations for the same physical room conflict if their date ranges overlap: `startA < endB && endA > startB`.
2. Preventing Double-Booking: Concurrency Models
Pessimistic Locking: Acquiring a database `SELECT FOR UPDATE` on the room row serializes checkouts but degrades throughput.
Optimistic Locking (Recommended): Each room inventory record has an integer `version` field. When committing a reservation, the update verifies the version hasn't changed. If another thread reserved the room first, the transaction fails gracefully and retries.
3. Code Implementation
Here is the room reservation manager implementation:
class Booking {
constructor(public roomId: string, public checkIn: Date, public checkOut: Date) {}
overlaps(start: Date, end: Date): boolean {
return this.checkIn < end && this.checkOut > start;
}
}
class HotelManager {
private bookings: Booking[] = [];
public bookRoom(roomId: string, checkIn: Date, checkOut: Date): boolean {
const hasConflict = this.bookings.some(b => b.roomId === roomId && b.overlaps(checkIn, checkOut));
if (hasConflict) return false;
this.bookings.push(new Booking(roomId, checkIn, checkOut));
return true;
}
}Model this architecture on the HLDForge Canvas
Turn static box diagrams into living, collaborative systems. Drag and drop microservices, Kafka clusters, and cache tiers. Run automated SPOF detection and STRIDE threat modeling in real time.
Written by HLDForge Architecture Team
Lead Machine Coding Specialist
The HLDForge Architecture Team publishes practical engineering guides, real-world case studies, and high-concurrency blueprints to help engineers build scalable and fault-tolerant software.