AlgoViz

Design a home rental marketplace like Airbnb

hard 60 min

Guests search homes by place and dates, hold some nights, pay, and stay.

Video lesson · Chapter 1 of 5

A Paris apartment can be nearby and still be unavailable

0:00 / 5:21

Theory

Read or listen first. About 2 min.

What it is

A home rental marketplace lets hosts offer a room or a home for chosen nights, and lets guests find, book and pay for a stay. The service is in the middle: it keeps the calendar, takes the money and passes it on.

What is sold is not a home but a night in a home. Each night exists once. A stay is several nights in a row that must all be free together, and all be taken together.

Why it exists

Hosts and guests do not know each other. The service gives them one trusted calendar, one price, one payment and one place to settle a problem.

For a designer it is a lesson in two opposite loads. Thousands of people look each second and can live with a list that is a few seconds old. A few people book each second, and for them nothing but the truth will do.

A short history

  1. 1995

    Vrbo starts in Colorado: holiday homes listed online by their owners.

  2. 2008

    Airbnb is founded in San Francisco.

  3. September 2009

    RFC 5545 defines the iCalendar format that hosts use to share free and busy dates between sites.

  4. September 2012

    PostgreSQL 9.2 adds range types, so a constraint can forbid two overlapping stays.

  5. October 2018

    Airbnb engineers publish a paper about deep learning in their search ranking.

Words to know

Night
The unit of sale: one listing on one local date.
Half-open stay
Check-in night included, check-out date not. March 15 to 20 is five nights, the 15th to the 19th.
Hold
Nights reserved for one guest for a short time while they pay.
Availability bitmap
One bit per night: 1 is free, 0 is taken.
Candidate
A listing that might fit, before dates, price and ranking are checked.
Ledger
A list of money movements that is only added to. Every movement has two sides that sum to zero.
Outbox
A table of events written in the same transaction as the data, then published.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Search. Place, dates and guests: candidates from the index, free nights from bitmaps

Understand it

The red shed
RED SHEDRed shed: door chartMonTueWedThuFriA chart over the door

The board helps you choose a shed. Only the door can lend it.

Scope

What are we building?

A service where hosts list homes, guests search by place, dates and number of guests, and a guest can book and pay. Hosts are paid after the stay begins. Both sides can write to each other and review each other.

  • Search

    Place, dates, guests and filters, ranked, in under 200 ms.

  • Book

    Hold nights, pay, confirm. One night is never sold twice.

  • Pay

    Take money from the guest now, pay the host later, refund by rule.

  • List

    Photos, house rules, prices, and a calendar shared with other sites.

Similar applications

ProductThe twist
VrboWhole homes
Booking.comHotels: many equal rooms, so a count can replace a calendar for each room
Hotels.comHotel rooms with a reward scheme
ExpediaFlight, hotel and car in one trip: several suppliers must all agree
TripAdvisorReviews first, booking second
AgodaHotels and homes, strong in Asia

The finished system

Where the design ends up

This is the last version. Design begins with one service and one database, and names the break that forces each box.

The whole system

Guests and hosts reach a CDN for photos and a gateway. The gateway sends searches to a search service, which uses a search index and night bitmaps, and holds to a booking service, which writes PostgreSQL and calls a payment service with a ledger. The card provider is outside. PostgreSQL changes flow through an event stream to workers, which update the index and the bitmaps.
Blue is searching, pink is booking and paying, orange happens later. Dashed means asynchronous or only on a miss.

Where to read what

QuestionInterview tabConcept page that owns the idea
What must it do?ScopeScoping
How big is it?NumbersNumbers that decide
What is the interface?API and dataIdempotency, pagination
How does it grow?DesignChange data capture, CQRS
What is hard?Deep divesConcurrency control, full-text search
How does money move?Deep divesDigital wallet, saga
Can I do it aloud?Mock45 minutes

Summary

The whole topic in one minute.

Looking and booking are two systems. Search uses an index by place plus a bitmap of free nights, both slightly behind. Booking takes the nights in one database transaction, and that transaction is the only thing that decides.

Money follows a fixed path: hold the nights, mark the booking as paying, charge once with an idempotency key, confirm, record both sides in a ledger, and pay the host a day after check-in. Anything unclear is asked again, never guessed.

  • Sell nights, not date ranges: one row for each listing and night.
  • A stay is all or nothing, so take its nights in one transaction.
  • Bookings are few (17 a second at peak). The problem is correctness, not write volume.
  • 365 nights fit in 46 bytes, so all availability fits in one machine's memory.
  • A hold has an expiry. A payment in progress is not allowed to expire silently.
  • Charge once, record both sides, pay the host later, and compare with the provider every day.

Sources