Geospatial Indexing

Difficulty: Intermediate

Question

What can you do with Redis GEO commands, how are locations stored internally, and how would you use them for a "nearby" feature?

Answer

Redis GEO commands let you store points as longitude/latitude pairs, measure the distance between two points, and find all points within a radius or a bounding box of a location. Under the hood a geo index is just a sorted set, where each member's score is a 52-bit geohash of its coordinates. That makes "find delivery partners within 3 km" a single fast command instead of a full table scan.

The main commands

CommandWhat it does
GEOADD key lon lat memberAdd or update a location. Note: longitude first
GEOPOS key memberGet stored coordinates back
GEODIST key m1 m2 kmDistance between two members, units m, km, mi, ft
GEOSEARCH key FROMLONLAT lon lat BYRADIUS 3 kmFind members within a radius (6.2+)
GEOSEARCH key FROMMEMBER m BYBOX 4 4 kmFind members in a box around a member
GEOSEARCHSTORE dest src ...Store results in another key
GEOHASH key memberStandard 11-character geohash string

GEORADIUS and GEORADIUSBYMEMBER still work but are deprecated since 6.2 in favour of GEOSEARCH. Useful options on GEOSEARCH include ASC (nearest first), COUNT n [ANY], WITHDIST and WITHCOORD.

How it works internally

  1. Coordinates are interleaved into a 52-bit geohash. Points close together usually share a long prefix, so they end up with nearby scores.
  2. The hash is stored as the score of a sorted set member, which is why ZREM removes a location and ZCARD counts them. There is no GEODEL.
  3. A radius search computes the geohash cells that cover the area (the centre cell and its neighbours), runs range queries on the sorted set for each, then filters candidates by exact distance.
  4. Distances use the haversine formula on a spherical Earth model, so results can be off by up to about 0.5%, which is fine for "nearby" features but not for surveying.

Valid latitudes are limited to roughly -85.05 to 85.05 degrees (the Web Mercator range), so polar points cannot be indexed.

Real use cases

  • Food delivery or ride hailing: keep drivers:blr updated with each partner's position every few seconds, then GEOSEARCH within 3 km of a restaurant to assign an order.
  • Store locators: "find the nearest 5 stores to this pincode's centre".
  • Geofencing checks, such as whether a user is within 200 m of a venue for check-in.

Trade-offs and gotchas

  • Longitude first: swapping lat and lon is the most common bug, and Indian coordinates like (77.59, 12.97) for Bengaluru silently become a point near the Arctic if reversed, since (12.97, 77.59) is still a valid pair.
  • Stale positions: members do not expire individually. Remove inactive drivers with ZREM, or keep a separate sorted set of last-update timestamps and clean up periodically.
  • One big key: a single geo key for all of India becomes a hot key. Shard by city or region, such as drivers:mumbai, drivers:pune.
  • Only points: Redis GEO handles points with radius and box queries. For polygons, routes or complex spatial joins, use PostGIS or a search engine with geo support, or the newer geo-shape support in the Redis Query Engine.
How GEOSEARCH finds nearby drivers. Geohash cells narrow candidates; exact distance filters the final list.
  1. GEOSEARCH from restaurant: Centre point and radius, e.g. 3 km.
  2. Compute covering cells: Centre geohash cell plus neighbours.
  3. Sorted set range scans: One score range per cell.
  4. Exact distance filter: Haversine removes points outside 3 km.
  5. Sorted, limited results: ASC, COUNT, WITHDIST applied.

Code examples

Nearby delivery partners in Bengaluru

# Longitude first, then latitude
redis-cli GEOADD drivers:blr 77.6412 12.9719 drv:101
redis-cli GEOADD drivers:blr 77.6101 12.9352 drv:102
redis-cli GEOADD drivers:blr 77.7500 13.0500 drv:103

# Partners within 7 km of a restaurant in Indiranagar, nearest first
redis-cli GEOSEARCH drivers:blr FROMLONLAT 77.6408 12.9784 BYRADIUS 7 km ASC COUNT 10 WITHDIST

# Distance between two partners
redis-cli GEODIST drivers:blr drv:101 drv:102 km

# Remove a partner who went offline (geo keys are sorted sets)
redis-cli ZREM drivers:blr drv:103

GEOADD outputs are omitted. Results are sorted nearest first with distances in km; the last decimals can differ slightly because coordinates are stored as a geohash. drv:103 is about 14 km away, so it does not match.

Key points

  • GEOADD takes longitude before latitude.
  • A geo index is a sorted set with a 52-bit geohash as the score; use ZREM to delete.
  • GEOSEARCH (6.2+) replaces the deprecated GEORADIUS commands.
  • Distances assume a spherical Earth, with up to about 0.5% error.
  • Members do not expire individually; clean up stale locations yourself.
  • Shard large geo sets by city or region to avoid hot keys.

Common follow-up questions

  • How would you keep driver locations fresh and remove inactive drivers?
  • Why does Redis search neighbouring geohash cells and not just the centre cell?
  • When would you choose PostGIS over Redis GEO?
  • How would you scale a nearby search for an entire country?

Interview tips

  • Mention that GEO is built on sorted sets; it explains removal, counting and performance.
  • Call out the longitude-first ordering, it is a favourite gotcha.
  • Frame the answer around a concrete feature like nearest delivery partner assignment.

Concepts covered

GEOADD, GEOSEARCH, Geohash, Sorted Set Encoding, Radius Queries

More Redis & Caching questions