Design "businesses near me"
A proximity service ("businesses near me") answers k-nearest-neighbor queries over millions of points of interest in under 100 ms. The core is a spatial index plus a two-stage query (coarse cell lookup, then exact refine).
Geo & Location, 8 components, about 35 min
1. Scope
In scope
- • Find businesses within radius / k-nearest
- • Filter by category
- • Update business profiles
- • Handle dense + sparse regions
Out of scope
- • Reviews ranking ML
- • Routing/directions
- • Real-time hours from sensors
2. Functional requirements
Must have
- • Return nearby POIs for a lat/lng
- • Filter by category/attributes
- • Fast (<100 ms)
- • Reflect profile updates
Nice to have
- • Ranking by rating/popularity
- • Open-now filter
- • Pagination
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • 200M POIs worldwide
- • 100K nearby-queries/sec peak
- • POI profile ~2 KB
- • Geo cells indexed
Derived numbers
5. API design
k-nearest POIs matching filters
Update a business profile
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
Spatial indexing options
Geohash encodes lat/lng into a string prefix so nearby points share prefixes — easy to bucket. Google S2 maps the sphere to a space-filling curve (better distance properties near poles/edges). Quadtrees recursively split dense regions (e.g., split a cell at 100 POIs), so cities and oceans both index efficiently. PostGIS or Elasticsearch geo_point give these out of the box.
Two-stage query
Stage 1: compute the query cell and its neighbors, fetch candidate POI ids from the index (coarse, fast). Stage 2: compute exact distances to candidates, filter by category/open-now, sort, and return top-k. This avoids scanning all POIs while still returning precise results.
Hot cells & caching
Popular areas (downtown) get hammered. Cache hot cells' candidate lists in Redis with a short TTL (1 min) so repeated nearby queries are served from memory.
Separating reads from writes
Browsing is read-heavy; merchant edits are write-skewed. Keep the business profile DB separate from the geo index; a profile-ingest worker propagates edits to both, so heavy read traffic isn't affected by write patterns.
9. Bottlenecks
- Dense regions → quadtree/finer cells
- Hot cells → Redis cache
- Index update lag on edits → async ingest, acceptable freshness
10. Trade-offs
Index type
Choose: Geohash or S2/H3 cell ids stored with each place, indexed in a normal database or search engine; query covering cells.
Instead of: Quadtree in memory: adapts to density but must be rebuilt and replicated.
Places change rarely, so a static cell index replicated for reads is simple and fast.
Read path
Choose: Read replicas or a search cluster for queries; the primary only takes owner edits.
Instead of: One database for both: owner edits compete with 100K QPS of reads.
Read and write rates differ by orders of magnitude.
Radius handling
Choose: Choose cell precision from the radius and expand rings until enough results are found.
Instead of: One fixed precision: too many cells for large radii, too few results in rural areas.
Density varies hugely between cities and countryside.
11. Failure modes
Hot region (city center at lunch)
Query shards for that area overload.
Mitigation: Cache popular queries by cell and category; replicate hot shards.
Stale business data
Users sent to closed places.
Mitigation: Hours stored and evaluated at query time in the place's local time zone; owner and user reports feed updates.
Geohash edge effect
Nearby places in an adjacent cell missed.
Mitigation: Always query the 8 neighbors along with the center cell.
12. What interviewers probe
Answer each one yourself before opening it.
What geohash precision for a 1 km radius?Show a strong answer
Precision 6 cells are about 1.2 km × 0.6 km; query the center plus neighbors and filter by exact distance.
How do you rank results?Show a strong answer
Combine distance, rating, popularity and open-now in a scoring function, later a learned ranker.
How big is the index?Show a strong answer
200M places × (id + cell + lat/lng ≈ 40 B) ≈ 8 GB: it fits in memory on each replica.
Why not just use PostGIS?Show a strong answer
PostGIS with a GiST index works well up to a large size; the cell approach matters when you need to shard or serve from a cache.
Sign in to track this guide in your curriculum progress.