skip to content

Geospatial Indexes

You will learn how Redis answers 'what is near me' by geohash-encoding coordinates into a sorted set and querying by radius or box. Interviewers pull this out in ride-sharing and delivery system designs to see if you know an off-the-shelf nearby-search primitive.

part ofRedisoverview, primer and where to startread it →
on this pageshow

questions

5

Redis geospatial commands are often described as a thin layer over another core Redis type. Which type actually backs GEOADD, how is a longitude/latitude pair encoded into it, and what practical consequences does that have for how you operate the key?

level: middleimportance: should knowfreq 30%

basics

~20 s

A sorted set. GEOADD interleaves 26 bits of longitude and 26 bits of latitude into one 52-bit geohash integer and stores it as the member's score. So ZREM, ZSCORE, ZCARD, ZRANGE, DUMP and EXPIRE all work on a geo key, and nearby-search is a set of score-range scans.

open as a page

Redis 6.2 introduced the GEOSEARCH and GEOSEARCHSTORE commands and deprecated the older GEORADIUS family. What do the newer commands add, and why could the original GEORADIUS not be executed on a read-only replica?

level: middleimportance: nice to knowfreq 22%

basics

~20 s

GEOSEARCH unifies GEORADIUS and GEORADIUSBYMEMBER (FROMLONLAT or FROMMEMBER) and adds rectangle search with BYBOX. GEORADIUS had an optional STORE clause, so Redis classified it as a write command and replicas refused it — hence the separate GEORADIUS_RO. GEOSEARCH is read-only; GEOSEARCHSTORE is the writing variant.

open as a page

What accuracy and coverage limits apply to Redis geospatial indexes — how exact are the coordinates returned by GEOPOS and the distances returned by GEODIST, and which coordinates cannot be stored at all?

level: seniorimportance: nice to knowfreq 18%

basics

~20 s

Coordinates are quantised to 26 bits per axis, so GEOPOS comes back within roughly a metre of what you wrote, never bit-identical. GEODIST is a great-circle distance on a sphere — a straight line, no roads, no altitude, with sub-percent error. Latitude is limited to about ±85.05°, so the poles cannot be stored.

open as a page

You are designing a 'vehicles near me' lookup on Redis Cluster for a fleet of two million vehicles that report their position every few seconds. How would you lay out the geospatial keys, and what specifically breaks if you keep every vehicle in a single key?

level: principalimportance: nice to knowfreq 20%

basics

~20 s

One key lives in one hash slot on one shard, so a single global index pins every position write and every query to one core, and dense-area queries scan huge candidate sets. Partition by region (geo:{city}) or geohash-prefix cell, fan out to neighbouring cells client-side, and sweep stale members with ZREM since geo members have no TTL.

open as a page