Mastering Redis GEO: Add Locations, Radius Searches, and Distance Calculations
Redis 3.2 introduces a concise GEO API that lets you store geographic points, perform radius queries, retrieve distances, and manage location data using commands like GEOADD, GEORADIUS, GEORADIUSBYMEMBER, GEODIST, and ZRANGE, while highlighting its underlying sorted‑set storage model.
Redis has just released version 3.2 , officially supporting geographic GEO APIs. These commands are ideal for simple location‑based services such as finding nearby merchants or calculating distances between users and stores.
Usage Example
GEOADD locations -0.12455 51.5007 "Big Ben" -0.12520 51.50115 "Westminster Station" -0.11358 51.50482 "BFI IMAX"The command returns (integer) 3, indicating three location entries were added. Each entry consists of latitude, longitude, and a name.
Radius Search
To find merchants within 500 meters of a user at -0.11759 51.50574: GEORADIUS locations -0.11759 51.50574 500 m Result: "BFI IMAX".
To also return distances and sort by proximity: GEORADIUS locations -0.11759 51.50574 1 km WITHDIST ASC Result includes members with their distances, e.g., "BFI IMAX" – 0.2960 km, "Westminster Station" – 0.7335 km, "Big Ben" – 0.7392 km.
Member‑Based Search
Find locations near a specific member (e.g., "Westminster Station") within 100 m:
GEORADIUSBYMEMBER locations "Westminster Station" 100 m WITHDISTReturns the member itself with distance 0.0000 and nearby "Big Ben" at 67.3659 m.
Distance Between Members
GEODIST locations "Westminster Station" "BFI IMAX"Result: "902.1221" meters.
Listing and Removing Members
All stored members can be listed with: ZRANGE locations 0 -1 Result: "Big Ben", "Westminster Station", "BFI IMAX".
Redis stores GEO data in a sorted set (zset); each name becomes a member, and latitude/longitude are converted to a geohash used as the score.
To delete a location: ZREM locations "Big Ben" Result: (integer) 1.
While Redis 3.2 offers a simple and efficient GIS solution for "what's nearby" queries, more comprehensive GIS requirements are better served by PostgreSQL with PostGIS.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
