System Design: Designing Airbnb (Hotel/Home Booking)
Designing a platform like Airbnb or Booking.com involves two distinct technical challenges: Search (helping users find the perfect place) and Concurrency (ensuring that once found, the place can be booked without overlap).
1. Core Requirements
- Property Listing: Owners can upload photos and details of their homes.
- Search: Users can search by location, date range, price, and category.
- Booking & Availability: Checking if a property is available for specific dates and locking it for a user.
- Payments: Handling secure transactions globally.
2. The Search Architecture
Airbnb is a read-heavy system. Users search far more often than they book.
- The Search Engine: Use Elasticsearch. It is much faster than SQL for complex filters (e.g., "3 bedrooms, pool, pet-friendly, under 00").
- Geospatial Search: Use Geohashing (like in our Yelp article) to find properties near a user's target city.
- Indexing: Property metadata is stored in a relational database (Postgres) and synchronized to Elasticsearch via Change Data Capture (CDC) to ensure search results are up-to-date.
3. Managing Availability (The Inventory Challenge)
Availability is a time-series problem. A property is not just "available" or "not available"; it is available on specific dates.
- Data Structure: Instead of a simple boolean, store availability as a bitmask or a separate
availabilitytable:property_id, date, status (Available/Booked). - Querying: To find homes available for
May 1st - May 5th, the system queries the database for properties that have no "Booked" status for any of those 5 dates.
4. Preventing Double-Booking (Concurrency)
What if two users click "Book" for the same house at the same time?
- Step 1: Database Locking: Use a Relational Database (PostgreSQL) with ACID transactions.
- Step 2: Transaction Flow:
- Start Transaction.
SELECT * FROM availability WHERE property_id = 123 AND date BETWEEN ... FOR UPDATE. (This locks the rows).- If all dates are free, insert the booking.
- Commit Transaction.
- Distributed Locking: For better performance, use Redis Redlock to hold a temporary lock on the property while the user is on the payment screen.
5. Scaling Global Search: CDNs and Caching
- Media: Photos are the largest data source. Use a CDN (CloudFront) to serve images from the edge.
- Metadata Cache: Popular search results (e.g., "Paris in Summer") are cached in Redis for 5-10 minutes.
6. Real-time Pricing (Dynamic Pricing)
Pricing changes based on demand, seasonality, and occupancy.
- The Logic: A background worker or stream processor (Flink) calculates the optimal price every few hours and updates the
Listing Service.
Summary
Building Airbnb is a balancing act between the "elastic" search requirements (Elasticsearch) and the "rigid" consistency requirements of booking (PostgreSQL). By separating search from the transactional booking path, you can build a system that is both fast and 100% accurate.
