Redis Geospatial
Redis Geospatial commands store places as longitude and latitude pairs. They answer location questions such as "Which stores sit within 5 kilometers of me?" or "How far apart are two cities?" Delivery apps, ride services, and store locators rely on this feature.
The Map Pin Analogy
Picture a paper map covered with pins. Each pin carries a name and a position. A geospatial key works like that map. You drop pins with names, and Redis measures distances and finds the pins near any point you choose.
North
▲
┌──────────┼─────────────────────┐
│ │ ● Noida │
│ ● Delhi│ │
│ │ │
│ ● Gurgaon │
└──────────┼─────────────────────┘
▼
South
How Redis Stores Locations
Redis converts each coordinate pair into a single number and saves it in a Sorted Set. Every Sorted Set command still works on a geospatial key. You can list members with ZRANGE or delete one with ZREM.
GEOADD – Adding Places
Give longitude first and latitude second. Many beginners swap them by mistake.
127.0.0.1:6379> GEOADD cities 77.2090 28.6139 "Delhi" 77.3910 28.5355 "Noida" 77.0266 28.4595 "Gurgaon" (integer) 3
Valid longitudes run from -180 to 180. Valid latitudes run from about -85.05 to 85.05.
GEOPOS – Reading Coordinates
127.0.0.1:6379> GEOPOS cities "Delhi" 1) 1) "77.20899999141693115" 2) "28.61389934116891693"
The tiny differences from your input come from the conversion to a single number.
GEODIST – Measuring Distance
127.0.0.1:6379> GEODIST cities "Delhi" "Noida" km "19.8"
Choose the unit with m, km, mi, or ft. The sample distance above is approximate. Redis returns a more precise decimal value.
GEOSEARCH – Finding Nearby Places
GEOSEARCH looks around a center point and returns members inside a radius or a box.
127.0.0.1:6379> GEOSEARCH cities FROMMEMBER "Delhi" BYRADIUS 22 km ASC WITHDIST 1) 1) "Delhi" 2) "0.0000" 2) 1) "Noida" 2) "19.8"
Search circle: radius 22 km around Delhi
┌───────────────────────┐
│ ╭───────────╮ │
│ ╱ ● Noida ╲ │
│ │ ● Delhi │ │ ● Gurgaon (about 25 km) stays outside
│ ╲ ╱ │
│ ╰───────────╯ │
└───────────────────────┘
Use FROMLONLAT in place of FROMMEMBER to search around raw coordinates, such as a phone's current position. Add COUNT 5 to limit the result list.
Older Commands
GEORADIUS and GEORADIUSBYMEMBER still run in current versions. Redis marks them as deprecated, so new projects should use GEOSEARCH.
Practical Example – Nearest Pharmacy Finder
Setup: GEOADD pharmacy 77.2167 28.6448 "Shop A" 77.2295 28.6129 "Shop B" User opens the app at longitude 77.2190, latitude 28.6300: GEOSEARCH pharmacy FROMLONLAT 77.2190 28.6300 BYRADIUS 3 km ASC WITHDIST COUNT 3 App shows the three closest shops, nearest first, with distances.
One command replaces a heavy database query with map math.
Removing Places
127.0.0.1:6379> ZREM cities "Gurgaon" (integer) 1
No GEODEL command exists, so ZREM does the job.
Accuracy and Limits
Redis computes distances with a spherical model of the Earth. The result stays within about 0.5 percent of the true distance, which suits delivery zones, store finders, and nearby-friend features. Surveying and aviation need exact geodesic math, so those fields should look elsewhere.
Geospatial keys grow like any Sorted Set. A key with millions of places still answers searches quickly, yet very large keys slow deletion and replication. Split data by region, such as pharmacy:north and pharmacy:south, when the list becomes huge.
Key Points
- GEOADD stores places as longitude then latitude.
- GEODIST returns the distance between two places.
- GEOSEARCH finds places inside a radius or box.
- Redis keeps geospatial data in a Sorted Set, so ZREM and ZRANGE work on it.
