Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Distributed Architecture
Lesson 17 of 24

Geospatial indexing

Find things near a point fast with geohashes, quadtrees and hexagonal grids.

18 min read

Why ordinary indexes fail

A query for restaurants within 2 km needs two-dimensional proximity. Separate B-tree indexes on latitude and longitude each return a long thin strip of the globe, and intersecting those strips reads far too many rows. Geospatial indexes map two dimensions onto cells, so nearby points share a cell identifier that a normal index can look up. The query becomes: find the few cells that cover the search circle, fetch their points, then compute exact distances on that small candidate set and discard anything outside the radius.

Geohash

A geohash interleaves the bits of latitude and longitude and encodes them in base 32. Each extra character subdivides the cell, and points sharing a prefix are close together.

  • 5 characters: about 4.9 km by 4.9 km
  • 6 characters: about 1.2 km by 0.6 km
  • 7 characters: about 150 m by 150 m
  • Always query the target cell plus its 8 neighbours: two points 10 m apart can sit on either side of a cell edge with very different prefixes

Quadtrees, S2 and H3

A quadtree splits a region into four quadrants recursively until each leaf holds fewer than some threshold of points, say 100. Dense Manhattan gets tiny cells and empty desert gets huge ones, so each lookup touches a similar amount of data. Google's S2 library projects the sphere onto a cube and uses a space-filling curve, giving cells that map to 64-bit integers and handle the poles and the antimeridian correctly. Uber's H3 uses hexagons, whose neighbours are all equidistant, which suits aggregations such as surge pricing per area.

Static places vs moving objects

Places change rarely: store them in a search engine or PostGIS with a geo index, cache popular cell results, and update via change data capture. Moving objects are different: 1 million drivers reporting every 4 seconds is 250 000 location writes per second, each overwriting the last. Keep live positions in memory, partitioned by cell (for example by geohash or H3 cell in a sharded cache), and do not write every ping to a durable database; sample positions to a stream for analytics instead.

Key takeaways

  • Geospatial indexes map 2D positions to cells that ordinary indexes can look up
  • Pick a geohash length that matches the search radius, and always include neighbouring cells
  • Quadtrees adapt cell size to density; S2 and H3 handle the sphere and equal-area aggregation
  • Keep fast-moving locations in memory partitioned by cell; persist samples asynchronously

Sign in to track lessons and earn badges.

Secondary indexes and searchCaching in distributed systems

Challenge checkpoint

Course: 0/24 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 6/10Design a Nearby Places ServiceAnswer radius queries quickly with a geospatial index, neighbouring-cell lookups and cached hot areas.Medium
All 10 challenges →