Module 39: System Design — Proximity Service & Nearby Places (Yelp / Uber)

Comprehensive architectural breakdown of a Location-Based Proximity Service (Yelp, Google Maps, Uber Nearby), covering Geohash encoding, Quadtrees, Google S2 cells, and sub-50ms radius geospatial queries.


Prerequisites

Before tackling proximity services, review these core modules:


ELI5 (30-Second Summary)

Imagine you open your phone and search for "pizza near me". Your phone knows your GPS coordinates (Latitude: 37.7749, Longitude: -122.4194).

In the physical world, the Earth is a 2D surface. If you ask a standard SQL database SELECT * FROM restaurants WHERE lat BETWEEN ... AND lon BETWEEN ..., the database has to scan millions of rows because standard indexes only sort in 1 dimension.

A Proximity Service chops the surface of the Earth into a grid of tiny square patches like a checkerboard, giving each patch a short string code (Geohash, like "9q8yy"). All restaurants inside that patch share the exact same string code! Now, finding nearby restaurants is as fast as looking up a single word in a dictionary!


Physical Analogy

Think of an Old-Fashioned City Map Grid Index:

City Map Analogy Proximity Service Concept Technical Description
Grid Square "D-4" on Folded Map Geohash (e.g., dr5ru) Hierarchical string encoding 2D latitude/longitude into a 1D string.
Looking up Square D-4 in map index Geospatial Index Lookup (Redis GEO) Sub-millisecond lookup of entities sharing the same geohash prefix.
Zooming into Downtown Sub-Grid Quadtree / Geohash Precision (Length 6) Length 6 Geohash covers ≈ 1.2km × 0.6km; Length 7 covers 150m × 150m.
Checking the 8 Surrounding Grid Squares 8 Neighbor Cell Search Solves the Boundary Problem by querying the central cell plus 8 adjacent neighbors.
Taxi GPS Beacon Every 4 Seconds Dynamic Location Stream (Kafka + Redis) High-write dynamic driver stream separated from static business data.

The Problem It Solves

Building a proximity service for apps like Yelp, Uber, or Google Maps poses unique geospatial challenges:

  1. The 2D Indexing Problem: Traditional B-Tree and LSM indexes sort data in 1 dimension. Indexing latitude and longitude independently results in a massive 2D bounding-box table scan (O(N)).
  2. High-Write Dynamic GPS Updates (Uber/Lyft): Millions of active drivers broadcast GPS coordinates every 3-5 seconds, creating hundreds of thousands of location writes per second.
  3. The Boundary Problem: If a user stands near the edge of a grid square, a restaurant 50 meters away might be in an adjacent grid square. Systems must query neighboring cells to prevent missing close places.

Visual Architecture

Geospatial Proximity Search (GeoHash / QuadTree / Uber H3)

flowchart TD
    subgraph Client["Mobile Client"]
        User["User App (lat: 37.77, lon: -122.41)"] -->|"GET /v1/places/nearby?r=2km"| Gateway["API Gateway"]
    end

    subgraph QueryFlow["1. Proximity Read Path (< 20ms)"]
        Gateway --> GeoSvc["Proximity Service"]
        GeoSvc --> HashCompute["Compute User Geohash (e.g. '9q8yy')"]
        HashCompute --> NeighborCompute["Calculate 8 Neighbor Hashes"]
        NeighborCompute --> RedisGeo[("Redis Geospatial Cluster (In-Memory)")]
        RedisGeo -->|"Return Place IDs"| Hydrate["Place Metadata Hydrator"]
        Hydrate --> PlaceDB[("Places Relational DB / Cache")]
        PlaceDB --> ReturnPlaces["Return Sorted JSON by Distance"]
    end

    subgraph DynamicWriteFlow["2. High-Frequency Driver Location Updates"]
        Driver["Uber Driver (GPS Beacon)"] -->|"POST /v1/locations (every 4s)"| Ingress["Location Ingestion Gateway"]
        Ingress --> Kafka["Kafka Location Stream"]
        Kafka --> LocationWorker["Location Worker Pool"]
        LocationWorker --> RedisGeo
    end

Real Numbers & System Sizing

Consider a Yelp-like service with 100 Million Active Users and 200 Million Global Places:

Geohash Length Cell Width × Height Typical Real-World Use Case
4 39 km × 19 km Large metropolitan region
5 4.9 km × 4.9 km City district / neighborhood
6 1.2 km × 0.6 km Proximity search (walk/short drive)
7 153 m × 153 m Precise street / building block
8 38 m × 19 m Accurate curb / door location

Deep Dive: Under the Hood

1. Geohash Encoding

Geohash interleaves the binary representations of latitude and longitude:

  1. Divide Latitude [-90, +90] into binary splits (greater than 0 = 1, less than 0 = 0).
  2. Divide Longitude [-180, +180] into binary splits.
  3. Interleave bits: lon[0], lat[0], lon[1], lat[1]...
  4. Encode the combined bitstream into Base-32 (0-9, b-z excluding a, i, l, o to avoid visual confusion).
  5. Key Property: All points sharing a common prefix belong to the same spatial cell. Prefix matching (LIKE '9q8yy%') performs an instantaneous range query over 2D space!

2. Solving the Boundary Problem

If you only query the user's current cell, a restaurant 10 meters away across the grid border will be completely missed. The 9-Cell Algorithm:

3. Spatial Index Comparison: Geohash vs. Quadtree vs. Google S2


Trade-offs & Decision Matrix

Mechanism 2D SQL Bounding Box Geohash (Redis GEO) Quadtree (In-Memory) Google S2 Geometry
Lookup Latency Slow (50-300 ms) Ultra-fast (< 5 ms) Fast (5-15 ms) Ultra-fast (< 3 ms)
Memory Footprint Low (disk-based) Moderate High (node tree pointers) Minimal (64-bit ints)
High Dynamic Writes Terrible (locks tables) Excellent (Redis in-memory) Moderate (tree rebalancing) Excellent
Polar Distortion Severe near poles Noticeable Moderate Zero (spherical projection)

Common Pitfalls & Anti-Patterns

  1. Forgetting the 8 Neighboring Cells: Querying strictly the user's geohash misses businesses right across the street if they sit on a grid border. Always query the center cell plus 8 neighbors.
  2. Storing Dynamic Moving Targets with Static Businesses: Storing static restaurants (updated once a year) and moving Uber drivers (updated every 4 seconds) in the same database table leads to index lock contention and performance collapse. Separate into a static Places DB and an ephemeral Redis/Kafka Driver Location store.
  3. Calculating Great-Circle Haversine Distance on the Entire Database: Running acos(sin(lat)...) across 200 million rows scans the entire table. Use spatial indexes to prune candidates down to < 100 places before calculating exact Haversine distance.

Try It Yourself

A runnable, zero-dependency Python simulation demonstrating Geohash encoding, 9-cell boundary neighbor expansion, and Haversine distance radius filtering is included in this directory.

Run the simulation:

python demo.py

Production References & Further Reading


Top 5 Interview Questions with Model Answers

Q1: How does Geohash convert two-dimensional coordinates (latitude, longitude) into a one-dimensional searchable index?

Model Answer: Geohash recursively subdivides the Earth's latitude [-90, +90] and longitude [-180, +180] intervals into binary splits. At each level:

  1. If the coordinate falls in the upper half of the range, assign bit 1; if lower half, assign bit 0.
  2. The alternating bits (longitude bit, latitude bit, longitude bit...) are interleaved into a single continuous bitstream.
  3. The bitstream is grouped into 5-bit chunks and encoded into Base-32 characters. This preserves spatial locality: points located geographically close to each other share long common prefixes in their Geohash strings, enabling fast O(1) prefix range scans.

Q2: What is the "Boundary Problem" in grid-based spatial systems, and how is it resolved?

Model Answer: The boundary problem occurs when a user stands close to the border of their spatial grid cell. A venue only 10 meters away across the boundary will have a completely different Geohash string. If we only query the user's cell, close venues are missed. We solve this using the 9-Cell Query Technique:

Q3: Why does Uber use Google S2 / H3 instead of standard Geohash?

Model Answer:

Q4: How do you architecture a system where 1 million active drivers send GPS updates every 4 seconds?

Model Answer:

  1. Dedicated Ingestion Highway: GPS updates bypass the core database and stream directly into an Apache Kafka topic partitioned by geohash_prefix.
  2. Ephemeral In-Memory Datastore (Redis): Stream workers update driver coordinates in Redis clusters using GEOADD drivers <lon> <lat> <driver_id> with a 15-second TTL.
  3. Read/Write Separation: Rider search queries read exclusively from the hot in-memory Redis cluster. Historical trip playback data is asynchronously flushed to cold storage (ClickHouse/S3) without impacting live matching.

Q5: How do Quadtrees adapt to varying population densities between rural and urban areas?

Model Answer: In a Quadtree, each spatial node represents a bounding box. When the number of entities within a node exceeds a fixed capacity threshold (e.g., N > 100):


Check Yourself Quiz

1. What mathematical property does Geohash provide for spatial searches? A) Encrypts GPS coordinates using RSA.
B) Interleaves latitude and longitude bits so nearby geographic points share identical string prefixes.
C) Compresses coordinates with MP3 audio encoding.
D) Guarantees zero network latency.

Answer: B. Shared string prefixes represent shared bounding boxes, enabling fast prefix lookups.
2. How many neighboring cells must be inspected to solve the boundary problem in a grid spatial system? A) 0 (only the user's cell)
B) 8 neighboring cells (total 9 cells queried)
C) 100 cells
D) 4,000 cells

Answer: B. The 8 adjacent cells plus the center cell guarantee no boundary misses.
3. Approximately what area is covered by a Geohash of length 6? A) 1,000 km x 1,000 km
B) 1.2 km x 0.6 km
C) 1 meter x 1 meter
D) The entire Earth

Answer: B. Length 6 covers approximately 1.2km x 0.6km, ideal for neighborhood proximity searches.
4. Why are dynamic driver GPS updates separated from static restaurant databases? A) Because drivers do not eat food.
B) To prevent high-frequency write updates (every 4 seconds) from degrading read performance and locking static database tables.
C) Because SQL cannot store numbers.
D) Because Redis is only for strings.

Answer: B. Separating dynamic streaming writes from static catalog databases protects availability and throughput.
5. Which spatial index uses hexagons where all neighbor centers are equidistant? A) B-Tree
B) Uber H3
C) Hash Table
D) Linked List

Answer: B. Uber H3 partitions the globe into hexagonal cells where all 6 neighbors share equal distances.