← Back to System Design, page 4

system-design-0070 · Medium fresher

Design a proximity search service like Yelp or Google Maps "restaurants near me" that finds places within a few kilometres from 100 million listings.

Store places in a database and index them by location cell (a geohash prefix or a quadtree node). A search computes the user's cell and its eight neighbours, fetches candidate places from those cells, filters by exact distance and other filters, and ranks the results. Place data changes rarely, so the index is read-heavy, cacheable and replicated. 100 million places at 1 KB each is only about 100 GB.

Full answer

Requirements

Functional:

  • Search places near a point within a radius (for example 2 km), with filters such as category and "open now".
  • View place details, photos and reviews.
  • Owners add or update places; users add reviews.

Non-functional:

  • Search results in under about 200 ms.
  • Read-heavy: searches far outnumber updates.
  • New or edited places may take a few minutes to appear in search.

Capacity estimates

Assumptions: 100M places, 1 KB per place record, 50,000 searches per second at peak, about 10 place updates per second.

  • Place data: 100M x 1 KB = about 100 GB, which fits on a single large database server and easily in a few index replicas.
  • Searches are the load: 50,000 per second, served by many read replicas of the geo index and a cache for popular areas.
  • Reviews and photos are far larger but are fetched only on the details page, from separate services.

API

GET /v1/search?lat=19.07&lng=72.87&radius_m=2000&category=cafe&open_now=true&cursor=.. -> [{"place_id", "name", "distance_m", "rating", "price_level"}...] GET /v1/places/{id} POST /v1/places/{id}/reviews {"rating": 4, "text": "..."}

Data model

  • places: place_id, name, lat, lng, geohash (precision 6 to 8), category, rating, price_level, hours, updated_at.
  • Index on (geohash) so a prefix query returns all places in a cell.
  • reviews: review_id, place_id, user_id, rating, text, created_at (separate service and store).

High-level design

mermaid
flowchart LR
  U[User app] --> LB[Load balancer]
  LB --> S[Search service]
  S --> C[(Cache by cell and filters)]
  S --> GI[(Geo index replicas)]
  LB --> P[Place service]
  P --> DB[(Places DB primary)]
  DB -- "change stream" --> GI
  LB --> RV[Reviews service]

How the geo lookup works

A geohash turns latitude and longitude into a string where a shared prefix means nearby. Precision 6 is a cell of roughly 1.2 km by 0.6 km. For a 2 km search:

  1. Choose a precision whose cell size is close to the radius (precision 5 or 6).
  2. Compute the user's cell and its 8 neighbours (needed because a user near a cell edge has close places in the next cell).
  3. Fetch places in those 9 cells, compute the real distance with the haversine formula, keep those within the radius.
  4. Apply filters, rank by distance, rating and relevance, and return a page.

A quadtree is an alternative: it splits busy areas (city centres) into small nodes and leaves empty areas (deserts) as large nodes, so each leaf holds a similar number of places.

Bottlenecks and trade-offs

  • Uneven density. A fixed geohash precision gives thousands of places per cell in Manhattan and none in rural areas. Use adaptive radius (grow the search until enough results) or a quadtree.
  • Caching. Cache results per (cell, category) for a minute; popular areas get high hit rates.
  • Freshness. Changes flow to index replicas through a change stream; a few minutes of delay is acceptable.
  • Geohash versus quadtree versus S2 or H3. Geohash works in any database with a string index; quadtrees adapt to density but are usually in-memory; S2 and H3 give better-shaped cells for global coverage.
  • "Open now" depends on local time zones and holidays; compute it at query time from stored hours.

By level

Fresher: Explain that places are grouped into map squares so the search only looks at nearby squares, then checks exact distance. A fresher should mention caching popular areas.

Mid: Describe geohash precision and neighbours, the read path with replicas and cache, and the update path with a change stream. Do the capacity math.

Senior: Compare geohash, quadtrees, S2 and H3, handle density with adaptive radius, and discuss ranking and freshness. Expect clear reasoning about where the load is.

Key points

  • Index places by location cell (geohash prefix or quadtree)
  • Search the user's cell plus 8 neighbours, then filter by exact distance
  • 100 million places is only about 100 GB
  • Read replicas and caching handle 50,000 searches per second
  • Adaptive radius or quadtrees handle uneven density

Common mistakes

  • Scanning all places and computing distance for each
  • Searching only the user's own cell and missing places across the edge
  • Using one fixed cell size everywhere without handling density
  • Writing every search through the primary database

Follow-ups

  • How would you rank results beyond distance?
  • How do you handle a user moving while results load?
  • How would you search along a route instead of around a point?

Tips

  • Explain geohash prefixes with an example
  • Mention the neighbour cells
  • Note how small the data set is

Evaluation rubric

  • Correct geo indexing method
  • Neighbour cells and exact distance filtering
  • Capacity math for data and reads
  • Density and freshness trade-offs