The problem
An index on (lat) or (lat, lng) can find a latitude band, but "nearby" needs both dimensions at once. Scanning a band of a large city and filtering by distance is far too slow at scale. The trick: map 2D space onto 1D cell IDs, so nearby places share ID prefixes or neighbouring cells.
Grid systems
| System | Idea | Strengths | Watch out for |
|---|---|---|---|
| Geohash | Recursively split the world into a base32 grid; longer string = smaller cell | Simple; prefix = containment; works in any KV store | Rectangular cells vary in size by latitude; edge effects |
| Quadtree | Split a region into 4 children only where dense | Adapts to density (more cells in Mumbai than the Thar desert) | In-memory structure, rebalancing on updates |
| S2 (Google) | Project the sphere onto a cube, Hilbert curve ordering | Precise coverage of arbitrary shapes, good locality | More complex |
| H3 (Uber) | Hexagonal hierarchical grid | Hexagons have equal-distance neighbours, great for surge pricing and aggregation | Not perfectly nested |
| Geohash length | Approx. cell size |
|---|---|
| 4 | ~39 km × 20 km |
| 5 | ~4.9 km × 4.9 km |
| 6 | ~1.2 km × 0.6 km |
| 7 | ~150 m × 150 m |
Boundary effects
Two points 10 m apart can sit on either side of a cell boundary and have completely different geohashes. Always search the target cell plus its 8 neighbours, then filter by true distance (haversine) and sort.
Querying only the user's own cell. A driver across the street in the neighbouring cell is invisible. Always include neighbours and expand the ring if you find too few results.