Design a Proximity Service like Yelp
Understanding the Problem
📍 What is a proximity service? A proximity service answers "what's near me?": restaurants within a mile, gas stations along a road, coffee shops open now. Yelp, Google Maps' nearby search and the restaurant list in a food delivery app all have one inside. The core problem is spatial: ordinary database indexes are built to search one dimension at a time, and "near" is a two-dimensional question.
You might be asked for this as "design Yelp," "design nearby search," or "design a location-based restaurant recommendation service." The last one is the same system with a stronger emphasis on ranking: instead of "everything within a mile," the user wants "the best places for me around here." We'll design the search first and cover recommendations as a deep dive.
Functional Requirements
Core Requirements
- Users should be able to search for businesses near a location, within a radius, optionally filtered by category ("sushi"), keyword, price and "open now."
- Users should be able to view a business's details: name, address, hours, photos, rating and reviews.
- Business owners should be able to add a business or update its details, including its location.
Below the line (out of scope)
- Writing reviews and the moderation behind them. We'll assume reviews and ratings exist.
- Reservations, ordering and delivery.
- Turn-by-turn directions and maps.
Non-Functional Requirements
Core Requirements
- The system should return search results in under 200ms at p99 (server side).
- The system should handle 100M daily active users doing about 5 searches a day, and about 200M businesses worldwide.
- The system should be highly available. Search is the product.
- Business updates should be visible in search within minutes. They're rare and don't need to be instant.
Below the line (out of scope)
- Strong consistency between a business update and search results.
- Real-time moving objects (drivers, friends). That needs a different design, because positions change every few seconds.
Some quick math:
- Searches: 100M × 5 = 500M a day, or about 6k QPS on average and roughly 20k QPS at peak.
- Business updates: 200M businesses, each updated maybe a few times a year: a few million updates a day at most, under 100 writes a second.
- The location index: for each business we need an ID (8 bytes), latitude and longitude (16 bytes) and a few bytes of category and rating for filtering, call it 50 bytes. 200M × 50 bytes = 10 GB.
The location index fits in the memory of one machine. That's the most useful number in the problem: we can keep the whole spatial index in RAM, replicate it as many times as traffic needs, and treat it as a read-only structure that's rebuilt or patched from the database. Business details (descriptions, hours, photos) are much bigger, but they're fetched by ID, which any key-value store or cache does well.