Question #48 of 60•Databases•Hard• ~3 min read

How do you design a Nearby Places Geospatial Service (Uber / Yelp)?

Executive Takeaway

Deconstruct Spatial Indexing using Geohash, Google S2 geometry, and QuadTree data structures for fast range queries.

Architectural Breakdown & AnalysisFAANG Aligned

Searching for nearby restaurants or drivers using standard 2D SQL WHERE lat BETWEEN x AND y queries causes full table scans at scale.

1. Geohash Indexing

  • Converts 2D latitude and longitude coordinates into a 1D string hash (e.g., 9q9hv).
  • Characters share common prefixes for geographically close locations. Shorter prefixes denote larger bounding boxes.

2. Google S2 Geometry

  • Maps Earth onto a 3D cube decomposed into hierarchical cells (Level 0 to 30).
  • Provides uniform cell area shapes without Geohash edge-boundary anomalies.

3. QuadTree Spatial Index

In-memory tree where every parent node splits into 4 sub-quadrants when node item counts exceed threshold capacity.

NOTE — Database Support

Leverage PostGIS (PostgreSQL) or Redis Geospatial Commands (GEOADD, GEORADIUS) for built-in spatial indexing.

Key Architectural Comparison

Geospatial IndexStructureQuery ComplexityBest Used For
Geohash (Base-32)1D string prefix grid.Fast B-Tree range query.Point searches with rectangular bounding boxes.
Google S2 Geometry64-bit integer Hilbert curve.$O(\log N)$ integer comparisons.Global spatial indexing without polar distortion (Uber).
QuadTree2D recursive tree.Fast spatial quadrant pruning.In-memory spatial index with varying density (Yelp).
Previous QuestionHow do you design secure storage for Passwords, API Keys, and Sensitive PII?Next Question Why is Shared Database between microservices an anti-pattern (Database-per-Service)?

Continue Learning on Being SDE

Explore deep-dive system architecture modules, low-level object-oriented designs, and interview cheat sheets.