Design Yelp
medium 50 minBuild a service like Yelp: find good places near a point, read about them and write reviews.
Video lesson · Chapter 1 of 5
Find pizza within five kilometres
Theory
Read or listen first. About 1 min.
What it is
A local search service answers questions such as pizza near me. It keeps a record for each place, knows where each one is, and collects people's ratings and reviews.
This page designs a service like Yelp as an exercise. It is our own design. It does not claim to describe how that company's systems work.
Why it exists
People ask while standing on a street corner, so an answer that is slow or empty is useless. People also choose by trust, so a rating that can be bought is worse than no rating.
Space has two directions, but an ordinary sorted index has one. Turning a map into something an index can search is a skill used in delivery, ride hailing, games and dating apps too.
A short history
1974
Raphael Finkel and Jon Bentley describe the quadtree, which splits busy areas into four again and again.
1984
Antonin Guttman describes the R-tree, an index of nested rectangles used by spatial databases.
2004
Yelp is founded.
2008
Gustavo Niemeyer publishes geohash, which writes a map square as a short string.
Words to know
- Geohash
- A string naming a rectangle on the map. Each extra letter makes it 32 times smaller in area.
- Cell
- One square or rectangle of the map.
- Covering
- The set of cells that together contain the whole search circle.
- Quadtree
- A tree that splits a cell into four when it holds too many places.
- R-tree
- A tree of nested rectangles, used by spatial databases.
- Haversine formula
- The distance between two points on a sphere.
- Recall and ranking
- First collect everything that could be good, then sort the best to the top.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Cover. Find the cells that contain the search circle
Understand it
Squares make the search small. Measuring makes it right. Ranking makes it useful.
Scope
What are we building?
A person gives a point and perhaps some words. We return nearby places with rating, price, distance and whether they are open. People can open a place, read reviews and write one.
Search near me
Words, category, price, rating and open now, within a radius.
Place pages
Hours, address, photos, attributes, rating and reviews.
Reviews
One to five stars, text and photos, checked for fraud.
Owners
Claim a listing, fix hours, answer reviews in public.
The hard part
Two directions, one index
Suppose we index latitude and longitude separately. The latitude index returns a thin band around the whole world, and so does the other. Only a tiny part of both is near you. A spatial index keeps nearby points together, so one small read is enough.
Narrow, then measure, then rank
The finished system
Where the design ends up
This is the last version. The Design tab starts from one service and one database, and shows what forces each extra box.
The whole system
Summary
The whole topic in one minute.
Local search has two steps. Cells and words narrow 200 million places to a few hundred candidates, then exact distance, opening hours and a ranking score choose the first twenty.
PostgreSQL holds the truth. A search index, caches and rating totals are copies fed by a stream of changes, so they may lag by seconds and can be rebuilt.
- Choose a cell size at least as large as the radius, or compute a proper covering.
- Keep place and words in one index, so neither filter throws away the other's answers.
- Cells find candidates; exact distance decides.
- Store rating sum and count, updated from events, and rank with a smoothed average.
- Hours belong to the place's own time zone.