18. Geospatial Indexing
1
The picture to keep in your head2
18.1 Why does a normal latitude/longitude index fail?3
18.2 What are spatial indexes?4
18.3 How do "nearby within radius" queries work?5
18.4 How do you update moving drivers?6
18.5 How do you shard by region?7
Memory card and where the analogy breaks8
Practice: five levels9
Interview questions10
Solutions11
Sources12
Code lab18. Geospatial Indexing
Sources
3 min read+10 XP
Source notes
- Geohash encoding (alternating bits, base-32 alphabet without a, i, l, o) and the test vector (57.64911, 10.40744) →
u4pruydqqvj: standard geohash references; verified by the chapter's code. - Redis GEO: sorted set scored by a 52-bit interleaved geohash; radius queries check the centre area plus 8 neighbours;
GEOSEARCHreplacesGEORADIUSfrom Redis 6.2. Redis documentation,GEOADDandGEORADIUSpages, checked 2026-10-08. - S2: cell levels 0 to 30, leaf cells about 1 cm across. S2 Geometry documentation, "S2Cell Hierarchy", checked 2026-10-08.
- H3: hexagonal hierarchical index from Uber, 16 resolutions, each finer cell about one-seventh the area, open source. Uber Engineering blog, "H3: Uber's Hexagonal Hierarchical Spatial Index", checked 2026-10-08.
- Haversine formula and Earth's mean radius 6,371 km: standard references. Cell sizes and degree lengths computed by the chapter's code.
- Rider counts, ping interval and speeds are illustrative assumptions; arithmetic computed 2026-10-08.
- Level 3 and Level 4 code: tested 2026-10-08 (
code/ch18_geospatial.py, all checks pass).