Design a home rental marketplace like Airbnb
hard 60 minGuests search homes by place and dates, hold some nights, pay, and stay.
See it run
Search, hold, pay, sync calendars and suggest prices, one hop at a time.
Operations
Search a city
Each step leaves fewer homes for the next, so the costly steps see few.
Step 1 of 11
The guest asks
nights
15 to 19
guests
2
Paris, March 15 to 20, two guests. Why: place, dates and guests decide what can be shown at all.
Step 1: The guest asks
def search(q): where = geocode(q.text) # place to lat, lng ids = cache.get(area(where, q.guests)) if ids is None: ids = index.near(where, km=25, guests=q.guests, limit=1000) cache.set(area(where, q.guests), ids, ttl=300) free = [i for i in ids if bitmap[i] & q.mask == q.mask] priced = [price(i, q) for i in free] ranked = rank(priced, q.user) return mix(ranked)[:20]- Time
- about 122 ms
- Load
- 10,500/s peak
- Ask the calendar table
- 10.5M checks/s
Cache candidates for an area, not whole answers. Apply the bitmap last, so a cached list is still filtered by fresh nights.
In the real world
The same home rental marketplace doing real jobs. Pick one and step through it.
The provider took the money. Our server died before it heard.
Step 1 of 5
PAYING is stored
state
PAYING
Before the call, the booking says PAYING with its key.
Step 1: PAYING is stored
Common mistakes
The bugs interviewers watch for, with the fix beside each.
A lock named after the date range
criticalOverlapping ranges have different names, so both locks are granted.
Wrongredis.set("lock:456:0315:0320", me, nx=True)redis.set("lock:456:0317:0322", you, nx=True) # also okRight# one row for each night, all in one transactionn = take_if_free(listing, nights)assert n == len(nights)Releasing nights before cancelling the payment
criticalThe payment can still succeed, and the nights belong to somebody else.
Wrongrelease(nights)provider.cancel(payment)Rightif provider.cancel(payment).cancelled: release(nights)Trusting the price in the request
criticalAnyone can send a smaller number.
Wrongcharge(request["total"])Rightcharge(quote_of(hold_id).total)Nights stored as UTC times
majorMarch 15 in Tokyo becomes two dates elsewhere, and a clock change makes a night 23 hours.
Wrongcheck_in = datetime(2027, 3, 15, tzinfo=UTC)Rightcheck_in = date(2027, 3, 15) # the home's local dateSending mail inside the booking transaction
majorA slow mail server holds locks, and a rollback cannot unsend.
Wrongwith db.transaction(): confirm(b); send_mail(b)Rightwith db.transaction(): confirm(b); outbox.add("confirmed", b.id)
Interview tips
Open with the split
- 10,500 searches, 17 bookings a second.
- Looking may be stale, booking may not.
Define the unit
- One listing, one local date.
- Half-open stays.
- All or nothing.
Follow the money
- State before call.
- Same key on retry.
- Two ledger sides, daily check.
Expect trouble
- Reply lost: stay PAYING.
- Other site: report conflicts.
- Stream down: refusals rise, nothing breaks.