How to design Yelp
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Indexing locations
- Edges, neighbours and the radius
- Caching and regions
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
- Yelp
- Uber
The problem
A proximity service answers one question: which businesses sit within a chosen distance of where someone is standing, and then opens the page of the one they tap. The data changes slowly while reads keep coming: roughly 5,000 searches a second, with 200 million businesses to search through. The answer turns on converting a two-dimensional "near me" question into a key lookup: give each business a compact key naming the map cell it sits in, read that cell together with the ring of cells around it, and check the true distance on the candidates that come back. The whole index comes to about 1.7 GB, which settles how to store it: copy it to replicas rather than split it, and let the search rate, not the size, decide how much to cache.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
How many businesses are there, and how many searches a day do people run? | The business count sizes the index, and the search rate sizes the read path. Together they tell you whether one machine can hold the index and whether the database can be on the search path at all. | About 200 million businesses and 100 million daily users, each searching about 5 times a day: about 5,000 searches a second. A smaller framing of the same question assumes 10 million businesses, which changes little about the shape of the design. |
Can the user choose the radius, and is there a maximum? | A fixed set of radii lets you precompute one cell size per radius and cache by it. An arbitrary radius forces you to cover the circle with cells at query time. | A fixed set: 0.5, 1, 2, 5 or 20 km. Start with the user's choice and widen it when a search comes back nearly empty. |
How quickly must an owner's edit show up in search? | Freshness decides how long you may cache, and whether the index needs updating on each edit or in a batch. | Edits can appear the following day. That allows a daily cache lifetime and a nightly batch, which is far simpler than live index updates. |
Is the user standing still while they search, or moving? | A moving user turns this into a live location problem, where the client re-queries and the server may stream. A stationary user makes each search independent, so the cache can be keyed by place. | Assume the user is stationary for a search. Moving objects, like drivers, are a different question. |
Do results need ranking or filters, such as category or open now? | Filters decide where the result set is narrowed: inside the index, in a search engine, or in memory after the cells are read. Ranking beyond distance brings in ratings and relevance, which is a different system. | Order by distance for now. Be ready for a follow-up about filtering by type or opening hours, and have a place for it in the design. |
Where do our users live, and are there rules on where their location data may go? | Location is personal data. If some countries require it to stay local, the deployment needs a region there and routing that keeps requests inside it. | Users search from many countries. Treat location as private data and be ready to deploy in more than one region. |
Is the result a list or a map, and does it page? | A map view needs bounding-box queries and tile rendering; a paged list needs a stable order and a cursor. They lead to different APIs. | A paginated list sorted by distance. Viewing results on a map is out of scope. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
About 5,000 searches a second | The search path must be stateless and horizontally scaled, and it should avoid a database query per search where it can: each search becomes a few key lookups against a cache of cell contents, with the database behind it serving misses from replicas. An indexed query against read replicas is a defensible first version; the cache is what you add when the replicas run hot. |
Low latency search (Hello Interview: under 500 ms) | A search does a handful of memory lookups in parallel, then a distance filter over the candidates from nine cells, a small set next to the table even where a dense downtown returns thousands. No scan, no join, and the business details come from a cache too. |
Highly available; eventual consistency is fine | No single machine on the read path: several service instances, a cache cluster, and read replicas of a small index. Edits reach the index and the caches later, within a day, so the read side keeps answering while the write side is down. |
Location data is private and handled within the law | Don't log raw coordinates or key a cache by them, keep what you store coarse, and be able to run a region inside a country whose law requires its users' data to stay there, with routing that keeps those requests inside it. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Search rate | 100 million daily users × 5 searches = 500 million searches a day; ÷ 100,000 seconds ≈ 5,000 a second (÷ 86,400 seconds ≈ 5,800) | About 5,000 searches a second, a little more with a true day; assume the peak is some multiple of that | The read path: how many service instances and cache lookups the design must carry. |
Row reads if each search scanned the business table | 200 million rows × 5,000 searches a second = 10^12 row reads a second | A trillion row reads a second | Why a geospatial index is not optional: a scan path on a single relational server is nowhere near that workload, so each search must touch a few cells, not the whole table. |
Geohash length for each radius | Wikipedia's geohash table gives the error of a cell by code length: length 4 about ±20 km, length 5 about ±2.4 km, length 6 about ±0.61 km. Choose the shortest length whose cell plus its neighbors covers the radius: 0.5 km → length 6; 1 km and 2 km → length 5; 5 km and 20 km → length 4 | Three cell sizes, lengths 4 to 6, cover all five radii | How many precisions to index and cache: three lists of ids per business, one per length. |
Geo index size | Alex Xu's notes put the quadtree index at about 1.71 GB and call a geohash index similar in size. Cross-check: 200 million × 8-byte business id = 1.6 GB of ids by themselves, so the figure is mostly ids; a six-character code per row would add about 1.2 GB to a plain table, so treat 1.7 GB as the floor and a few GB as the ceiling. Either fits in one server's memory | About 1.7 GB for the whole index at one precision | Whether one server holds the index in memory: it does, so copy it to replicas rather than split it. |
Redis memory for cell lists at three precisions | 8-byte id × 200 million businesses × 3 precisions = 4.8 GB ≈ 5 GB | About 5 GB of values per copy of the cache | One Redis cluster per region can hold a full copy, so each region serves cell lookups locally. |
Cache lookups a second | 5,000 searches a second × 9 cells per search = 45,000 cell lookups a second | About 45,000 small reads a second across the cache | The cache read load, spread over cache nodes and regions; the nine lookups per search run in parallel so latency stays at one round trip. |
Quadtree leaves | 200 million businesses ÷ 100 businesses per leaf = 2 million leaves | About 2 million leaf nodes | The memory of an in-memory quadtree, and why building it at startup takes minutes, which decides how you roll out new servers. |
Business table size | 200 million businesses × about 1 KB of details each (an assumption) = 200 GB | About 200 GB | Whether the business table needs sharding: a single primary with replicas carries it, so sharding waits until the table outgrows one server. |
Core entities
| Entity | What it holds |
|---|---|
Business | One row per place: where it is, what it is called, and the details shown on its page. Owned by the business service and written by owners. |
Geo index row | The map cell a business sits in. One row per business at the finest precision; a coarser cell is a prefix range on the same key, so a cell's contents are a range read. |
Cached cell list | A Redis entry per cell at each cached precision: the ids of the businesses inside it, kept for a day. |
The API
| Endpoint | Notes |
|---|---|
| A read, served by the location service. The radius is one of the five allowed values and picks the cell size. Results come back sorted by distance with a page cursor. Don't log the coordinates at full precision. |
| Served by the business service from a Redis cache of business details, with the database behind it on a miss. Business data changes rarely next to how often it is read, which is why this call is cached too. |
| Restricted to the owner. Writes the row to the primary and computes the geohash for the index row in the same transaction. The new business appears in search after the nightly refresh, which is the agreed freshness. |
| Restricted to the owner. If the location changed, the index row moves: delete the old (geohash, id) pair and insert the new one. Cached cell lists keep the old picture until their day is up or the nightly job replaces them. |
| Restricted to the owner. Removes the row and its index rows. A cached cell list may still name the id for up to a day; the hydration step treats a missing business as gone and drops it from the results rather than failing the search. |
The high-level design
Step 1 of 4
Two services and one database
Split the traffic by what it does. A location service answers searches and does nothing else: it takes no writes and keeps no state, so you add instances for lunchtime and remove them at night. A business service handles the page view and the owner's add, update and delete. Both sit behind a load balancer and share one MySQL primary holding the business table and a geohash index table, where each row is a business id with the code of its map cell. The search reads the index with a prefix match on that code rather than scanning the table.
What breaks first
The deep dives
Indexing locations. A user wants the businesses within 2 km of a point, out of 200 million. How do you find them without reading the table, and does that index live on one machine or many?
Edges, neighbours and the radius. A user stands a step away from a cell boundary and searches within 500 m. Walk the request from the point to the sorted results, and say which businesses a naive lookup would miss.
Caching and regions. Lunchtime traffic concentrates on a few big cities, and users search from many countries. What do you cache, under which key and for how long, and where does the service run?
Concepts this guide uses
Sources
- Alex Xu, System Design Interview volume 2, chapter 17: Proximity Service (notes at books.dwf.dev)
- System design notes: Proximity Service chapter
- htadev: Design a Proximity Service System
- Copilot Interview: Design a Proximity Service (Yelp / Nearby Places) walkthrough
- Wikipedia: Geohash
- Wikipedia: Quadtree
- Redis documentation: GEOSEARCH
- Redis documentation: Geospatial data type
- Redis documentation: EXPIRE
- OneUptime: How Redis geospatial indexes work internally
- System Design School: Design Yelp