Redis Geospatial
Redis can store locations (longitude, latitude) and answer questions like "which stores are within 5 km of me?" or "how far is Hanoi from Ho Chi Minh City?". Geospatial indexes are built on sorted sets: each location is a member whose score is a geohash, a number that encodes the position so that nearby places get nearby scores.
GEOADD and GEOPOS
GEOADD key longitude latitude member [...] adds places. Note the order: longitude first, then latitude (x, then y), which is the opposite of how most maps write coordinates.
GEOADD cities 105.8542 21.0285 hanoi 106.6297 10.8231 hcmc 108.2022 16.0544 danang 100.5018 13.7563 bangkok
GEOPOS cities hanoi danang nowhere
TYPE cities
ZCARD citiesredis> GEOADD cities 105.8542 21.0285 hanoi 106.6297 10.8231 hcmc 108.2022 16.0544 danang 100.5018 13.7563 bangkok
(integer) 4
redis> GEOPOS cities hanoi danang nowhere
1) 1) "105.85420221090317"
2) "21.028499185602875"
2) 1) "108.2022026181221"
2) "16.05440040333351"
3) (nil)
redis> TYPE cities
zset
redis> ZCARD cities
(integer) 4
GEOPOS returns the coordinates back, slightly different from the input in the last digits: the geohash stores positions with about 0.6 mm precision, not the exact floats you sent. TYPE shows that the key is an ordinary sorted set.
Distances
GEODIST key member1 member2 [m|km|mi|ft] returns the distance along the Earth's surface (Redis models the Earth as a sphere, so expect an error of up to about 0.5 %):
GEOADD cities 105.8542 21.0285 hanoi 106.6297 10.8231 hcmc 108.2022 16.0544 danang
GEODIST cities hanoi hcmc km
GEODIST cities hanoi danang km
GEODIST cities danang hcmc mi
GEODIST cities hanoi atlantis kmredis> GEOADD cities 105.8542 21.0285 hanoi 106.6297 10.8231 hcmc 108.2022 16.0544 danang
(integer) 3
redis> GEODIST cities hanoi hcmc km
"1138.1252"
redis> GEODIST cities hanoi danang km
"606.0870"
redis> GEODIST cities danang hcmc mi
"376.6740"
redis> GEODIST cities hanoi atlantis km
(nil)
Searching around a point
GEOSEARCH finds members inside a circle (BYRADIUS) or a rectangle (BYBOX), centred either on a member (FROMMEMBER) or on coordinates (FROMLONLAT). Options add the distance (WITHDIST), the coordinates (WITHCOORD), sort by distance (ASC/DESC) and limit the results (COUNT).
GEOADD shops 105.8500 21.0300 old-quarter 105.8412 21.0245 hoan-kiem 105.8000 21.0400 west-lake 105.9000 20.9800 long-bien 105.7800 21.0050 my-dinh
GEOSEARCH shops FROMLONLAT 105.8450 21.0280 BYRADIUS 2 km ASC WITHDIST
GEOSEARCH shops FROMMEMBER hoan-kiem BYRADIUS 7 km ASC COUNT 2
GEOSEARCH shops FROMLONLAT 105.8450 21.0280 BYBOX 15 10 km ASCredis> GEOADD shops 105.8500 21.0300 old-quarter 105.8412 21.0245 hoan-kiem 105.8000 21.0400 west-lake 105.9000 20.9800 long-bien 105.7800 21.0050 my-dinh
(integer) 5
redis> GEOSEARCH shops FROMLONLAT 105.8450 21.0280 BYRADIUS 2 km ASC WITHDIST
1) 1) "hoan-kiem"
2) "0.5544"
2) 1) "old-quarter"
2) "0.5649"
redis> GEOSEARCH shops FROMMEMBER hoan-kiem BYRADIUS 7 km ASC COUNT 2
1) "hoan-kiem"
2) "old-quarter"
redis> GEOSEARCH shops FROMLONLAT 105.8450 21.0280 BYBOX 15 10 km ASC
1) "hoan-kiem"
2) "old-quarter"
3) "west-lake"
4) "my-dinh"
The search from hoan-kiem includes hoan-kiem itself, at distance 0. The box search (15 km wide, 10 km high) also catches west-lake and my-dinh but not long-bien, which lies outside the box to the south-east.
This is the core of every "find the nearest" feature: a delivery app looks up the closest couriers, a store finder lists shops near the user, a ride-share app finds nearby drivers.
Storing search results
GEOSEARCHSTORE saves the result of a search into another key, either as a geo set or, with STOREDIST, as a sorted set whose scores are the distances:
GEOADD shops 105.8500 21.0300 old-quarter 105.8412 21.0245 hoan-kiem 105.8000 21.0400 west-lake
GEOSEARCHSTORE nearby shops FROMLONLAT 105.8450 21.0280 BYRADIUS 10 km ASC STOREDIST
ZRANGE nearby 0 -1 WITHSCORESredis> GEOADD shops 105.8500 21.0300 old-quarter 105.8412 21.0245 hoan-kiem 105.8000 21.0400 west-lake
(integer) 3
redis> GEOSEARCHSTORE nearby shops FROMLONLAT 105.8450 21.0280 BYRADIUS 10 km ASC STOREDIST
(integer) 3
redis> ZRANGE nearby 0 -1 WITHSCORES
1) "hoan-kiem"
2) "0.5543807890702301"
3) "old-quarter"
4) "0.5649169408566737"
5) "west-lake"
6) "4.858565968493712"
It's still a sorted set
Because a geo index is a sorted set, the sorted-set commands work on it: ZREM removes a place, ZCARD counts places, and ZRANGE lists them in geohash order. GEOHASH returns the standard 11-character geohash string of a member, which other systems understand too:
GEOADD cities 105.8542 21.0285 hanoi 106.6297 10.8231 hcmc
GEOHASH cities hanoi hcmc
ZREM cities hcmc
ZRANGE cities 0 -1redis> GEOADD cities 105.8542 21.0285 hanoi 106.6297 10.8231 hcmc
(integer) 2
redis> GEOHASH cities hanoi hcmc
1) "w7er8u0evs0"
2) "w3gvd6m3hh0"
redis> ZREM cities hcmc
(integer) 1
redis> ZRANGE cities 0 -1
1) "hanoi"
Tip: For moving objects (drivers, couriers) re-run
GEOADDwith the new coordinates: it updates the member's position in place. Combine it with a separate key per object that has a short TTL, so objects that stop reporting drop out.
Limits
- Valid longitudes are −180 to 180 and latitudes about −85.05 to 85.05 (the poles cannot be indexed).
- Distances are on a perfect sphere, which is fine for "nearby" queries but not for surveying.
- For polygons, complex shapes or combining location with text filters, use the Redis Query Engine (last lesson) or a GIS database such as PostGIS.
Test yourself
In which order does GEOADD take coordinates?
- latitude, longitude
- longitude, latitude
- either order
- x, y, z
Which data type stores a Redis geospatial index?
- Hash
- List
- Sorted set
- String
Which command finds the members within 3 km of a given coordinate?
- GEODIST key 3 km
- GEOSEARCH key FROMLONLAT lon lat BYRADIUS 3 km
- GEOPOS key 3 km
- ZRANGE key 0 3