System DesignSep 26, 2026⏱️ 3 min read
Design a Proximity Service Like Yelp: Geohash vs Quadtree & Spatial Indexing

Design a Proximity Service Like Yelp: Geohash vs Quadtree & Spatial Indexing

Why BETWEEN lat_min AND lat_max Doesn't Scale

Running a naive SQL query with bounding box coordinates (WHERE latitude BETWEEN ... AND longitude BETWEEN ...) fails because standard B-Tree indexes optimize one dimension at a time. Filtering 200 million businesses across latitude leaves millions of rows still requiring intersection with longitude.
Location-based discovery platforms (Yelp, Google Places, Uber Pickups) convert 2D coordinates (lat, lon) into 1D spatial indexes so range lookups run in milliseconds.
Proximity Service Geohash vs Quadtree

Proximity Service Geohash vs Quadtree


1. Read vs. Write Asymmetry

  • Search QPS: 100M DAU × ~5 searches/day $\approx 5,000\text{ QPS}$ (Peak: 15K+ QPS).
  • Business Updates: New restaurants open or change hours rarely (writes are tiny and can tolerate seconds of eventual consistency via nightly/CDC cache sync).
  • Total Data: 200M businesses fit comfortably in ~200 GB for full details, and only ~1.7 GB for (business_id, lat, lon) spatial index entries!

2. Geospatial Indexing Strategies Compared

Approach A: Geohash (Base32 Grid Encoding)

Geohash recursively splits the Earth into alternating latitude/longitude quadrants, encoding a 2D point into a short Base32 string (e.g., 9q8zn).
  • Property: Longer shared prefix = closer geographic distance.
  • Precision Guide: Level 4 ($\approx 39\text{km} \times 19\text{km}$), Level 5 ($\approx 4.9\text{km} \times 4.9\text{km}$), Level 6 ($\approx 1.2\text{km} \times 0.6\text{km}$).
  • Critical Boundary Gotcha: Two stores 50 meters apart across a prime grid boundary can have completely different prefixes! Solution: Always query the target geohash cell plus its 8 surrounding neighbor cells (9 cells total) in parallel!
  • Redis Native Support: GEOADD / GEOSEARCH stores 52-bit geohashes inside sorted sets out of the box.

Approach B: Quadtree (In-Memory Adaptive Tree)

Starts with one root bounding box for the entire world and recursively splits any square containing more than $K$ (e.g., 100) businesses into 4 quadrants.
  • Superpower: Adapts dynamically to population density! Manhattan splits deeply into city blocks, while the Pacific Ocean remains a single leaf node.
  • Memory: Storing (lat, lon, business_id) for 200M entries in a Quadtree requires ~1.7 GB RAM—easily fits in memory on a single replicated cluster of read nodes!
Index EngineDensity AdaptationUpdate ComplexityBest Infrastructure Fit
Geohash + RedisStatic grid sizeVery easy (HSET/GEOADD)Dynamic businesses / Redis clusters
Quadtree (In-Memory)Adaptive to dense citiesRebalance on inserts (rebuild nightly)Static discovery (Yelp/Maps POI)
Google S2 / Uber H3Uniform hexagons/spherical cellsLow distortion near polesRide-sharing / dynamic pricing grids

3. Production Caching Architecture

Split the read path into two specialized Redis caches backed by a read-replicated SQL Business DB:
  1. Geohash Index Cache: geohash_prefix -> [business_id_1, business_id_2, ...]
  2. Business Detail Cache: business_id -> { name, address, rating, hours, photo_urls }
When a user searches within 5 km, the Location-Based Service maps (lat, lon, radius) to the matching Geohash precision + 8 neighbors, unions cached IDs in parallel, computes exact Haversine distance filtering, and batch-hydrates business details from Redis in < 20ms.

Share this article

Comments