Geo-Spatial & Location Systems
Index moving drivers, restaurants, and landmarks in 2D space for sub-100 ms proximity queries.
In plain words
Location systems answer questions like "what is near me?" quickly by dividing the map into cells, so they only search the cells around you instead of the whole world.
Think of it like…
Dividing a city map into numbered square/hexagonal tiles: to find nearby taxis, you only check your tile and its 8 neighbors instead of every taxi in the country.
Where you’ve seen it
When you open Uber, it looks only at drivers in the map cells around you to find the nearest car in milliseconds.
Words you’ll hear
- Geohash
- Encodes a location as a short string; nearby places share prefixes.
- Quadtree
- A map split into four squares, recursively, where it is busy.
- H3 / S2
- Grid systems (hexagons or squares) from Uber and Google.
- Proximity search
- Finding everything within a distance of a point.
Visual diagram
Theory & deep dive
Why 2D `BETWEEN lat AND lon` indexes fail (and how Grid Hashing fixes it)
A standard B-Tree sorts one number well, not two (lat AND lon) together. Querying lat BETWEEN A AND B AND lon BETWEEN C AND D still scans huge strips across the whole globe.
Geohash, Google S2, and Uber H3 map 2D earth coordinates into 1D grid cell IDs where nearby locations share the same prefix or cell ID. Always query the user’s cell plus its 8 neighboring cells so a driver 50 meters away across a grid boundary is never missed!
How it works, step by step
Step 1 of 41. Ingest frequent location pings cleanly
Drivers send GPS updates every 3–5 seconds. Write current positions to an in-memory geo store (Redis GEO / sharded cell index) and stream history to Kafka/cold storage.
- Ping Interval
- 3–5 seconds
- Hot Store
- In-memory / Redis
When to use it & trade-offs
What it is
Geo-spatial systems convert 2D (latitude, longitude) coordinates into 1D prefixes or hierarchical grid cells (Geohash, Quadtrees, Google S2 / Uber H3) to support fast “find nearest K drivers within 3 km” queries.
Why it matters: Filtering WHERE lat BETWEEN ... AND lon BETWEEN ... on 10M moving drivers forces expensive 2D scans; spatial cells turn proximity into simple prefix/cell lookups.
When to reach for it
- Ride-hailing (Uber/Lyft), food delivery (DoorDash), nearby restaurants (Yelp), and dating discovery
- Real-time surge pricing per H3 hexagon based on local supply/demand counts
- Live courier/driver location streaming on a map while trip is active
When to avoid or be careful
- Updating PostgreSQL rows every 3 seconds per active driver when an in-memory geo index handles ephemeral pings
- Querying only a single Geohash cell without checking its 8 surrounding neighbors
Spatial Indexing Structures
Strengths: Simple 1D string prefix (u4pru), native Redis GEORADIUS support.
Watch out: Boundary edge cases (must query 8 neighbors) and fixed grid sizes.
Strengths: Splits dense cells automatically so every leaf holds ~100–500 places.
Watch out: Frequent moving-driver updates require tree rebalancing/locking.
Strengths: Hexagons (H3) have uniform distance to all 6 neighbors; S2 handles spherical earth cleanly.
Watch out: Requires spatial library integration and multi-resolution aggregation.
Interview signals & numbers to know
When this topic comes up
- nearby drivers
- within 5 km
- ride hailing
- food delivery
- ETA routing
Benchmarks & rules of thumb
- Driver ping cadence
- Every 3–5 seconds
- Geohash precision 6
- ~1.2 km × 0.6 km
- Geohash precision 5
- ~4.9 km × 4.9 km
- Offer lock timeout
- 10–15 seconds
Common mistakes & follow-ups
Common interview mistakes
- Returning drivers sorted by Haversine straight-line distance when a river or one-way highway doubles the real driving ETA
- Querying only one Geohash cell - a driver 50 meters away across a cell boundary gets missed
- Writing high-frequency ephemeral GPS pings directly into a primary SQL table
Likely interviewer follow-ups
- Why must you query a Geohash cell AND its 8 neighbors for proximity search?
- Why does Uber prefer hexagonal H3 cells over square grids for surge pricing and dispatch?
- How do you avoid assigning one driver to two simultaneous ride requests?
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply Geo-Spatial & Location Systems inside real High-Level Design prompts or review Q&A flashcards.