Mastering Redis GEO: Store and Query Geolocation Data Efficiently
This article explains Redis' GEO feature, covering its data structures, commands like GEOADD, GEOPOS, GEODIST, GEORADIUS, and GEORADIUSBYMEMBER, and demonstrates how to store, retrieve, and calculate distances for location‑based services with practical code examples and visual illustrations.
1 Introduction
In the first article we introduced Redis' basic data structures, which are designed for different use cases:
Dynamic string (REDIS_STRING): integer (REDIS_ENCODING_INT), raw string (REDIS_ENCODING_RAW)
Double‑ended list (REDIS_ENCODING_LINKEDLIST)
Compressed list (REDIS_ENCODING_ZIPLIST)
Skip list (REDIS_ENCODING_SKIPLIST)
Hash table (REDIS_HASH)
Integer set (REDIS_ENCODING_INTSET)
Beyond these common types, Redis also provides less‑used structures such as BitMap, Geo, and HyperLogLog, each solving specific statistical problems. This article focuses on the Geo type, exploring its capabilities for coordinate storage, distance calculation, and map‑based applications.
2 Understanding Location‑Based Services (LBS)
Location‑Based Services (LBS) use a user's geographic data to deliver services and are widely used in map applications (e.g., Baidu, AMap) and e‑commerce platforms (e.g., Meituan, Ele.me). Typical scenarios include:
Calculating a user's precise geographic coordinates.
Counting other points of interest within a certain radius and computing distances.
Sorting locations by proximity.
These scenarios are familiar and appear in many everyday applications.
3 Geo Capabilities in Redis
Redis introduced GEO commands in version 3.2 to store and manipulate geographic information. There are six core commands:
GEOADD
GEOPOS
GEODIST
GEORADIUS
GEORADIUSBYMEMBER
GETHASH
3.1 GEOADD – Adding Longitude and Latitude
The command GEOADD key longitude latitude member [longitude latitude member ...] stores a set of coordinates and their associated member names in a sorted set.
<code>GEOADD key longitude latitude member [longitude latitude member ...]</code>Example:
<code>redis> GEOADD food:location 115.775632 39.483256 "Northeast Dumpling House" 114.081569 39.692756 "Lanzhou Noodles"
(integer) 2</code>3.2 GEOPOS – Retrieve Coordinates
Given a key and member name(s), GEOPOS returns the stored longitude and latitude.
<code>GEOPOS key member [member ...]</code>Example:
<code>redis> GEOPOS food:location Northeast Dumpling House Lanzhou Noodles NonExisting
"115.775632 39.483256"
"114.081569 39.692756"
</code>3.3 GEODIST – Distance Between Two Locations
GEODIST key member1 member2 [unit] returns the distance between two members. The optional unit defaults to meters and can be m , km , mi , or ft .
m – meters
km – kilometers
mi – miles
ft – feet
If either member does not exist, the result is null. Example calculating the distance between the two restaurants yields about 6.1 km:
<code>redis> GEODIST food:location Northeast Dumpling House Lanzhou Noodles
"6184.15156"</code>3.4 GEORADIUS – Find Locations Within a Radius
Use GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [ASC|DESC] [COUNT count] to search for members within a given distance from a coordinate.
key longitude latitude : base point and key.
radius : search radius.
Unit: m , km , ft , mi .
WITHCOORD , WITHDIST , WITHHASH : optional return formats.
ASC|DESC : order by distance.
COUNT : limit number of results.
Example: find the nearest food places within 10 km, ordered from near to far, returning distance, and limiting to 100 results.
<code>redis> GEORADIUS food:location 115.791331 39.5120003 10 km WITHDIST ASC COUNT 100
"Northeast Dumpling House" 3.3421
"Lanzhou Noodles" 9.4571
</code>3.5 GEORADIUSBYMEMBER – Search by Member Name
This command is similar to GEORADIUS but uses an existing member as the center point.
<code>redis> GEORADIUSBYMEMBER food:location "Lanzhou Noodles" 100 km WITHDIST
"Northeast Dumpling House" 6.09127
"Lanzhou Noodles" 0
</code>3.6 ZREM – Remove Closed Stores
<code>redis> ZREM food:location "Lanzhou Noodles"
(integer) 1
</code>4 Summary
GEO leverages Redis Sorted Sets and GeoHash encoding to map latitude/longitude to sorted‑set scores, enabling efficient 2‑D range queries and encoding.
Typical applications include precise user positioning, radius‑based proximity searches with distance calculations, and sorting results by closeness.
Real‑world map data can be massive; partitioning by city or district improves storage and retrieval performance.
Architecture & Thinking
🍭 Frontline tech director and chief architect at top-tier companies 🥝 Years of deep experience in internet, e‑commerce, social, and finance sectors 🌾 Committed to publishing high‑quality articles covering core technologies of leading internet firms, application architecture, and AI breakthroughs.
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.