GEORADIUS(3valkey)
π GEORADIUS π
okay bestie, so this is a Valkey command β straight out of the Valkey Command Manual (3valkey), section 3. it's the one where you ask "hey, what's near this spot?" and the server actually tells you. let's get into it πΊοΈβ¨
β¨ NAME β¨
georadius β queries a geospatial index for members within a distance from a coordinate, optionally stores the result. π«
πΈ SYNOPSIS πΈ
GEORADIUS key longitude latitude radius <M | KM | FT | MI> [WITHCOORD]
[WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC | DESC] [STORE key |
STOREDIST key]
π¦ DESCRIPTION π¦
So basically: this returns the members of a sorted set that's been stuffed full of geospatial info using GEOADD, and it only gives you back the ones sitting inside the area you defined β the center location, plus a max distance from that center (that's the radius, bestie π―).
heads up: this manual page also covers theGEORADIUS_ROandGEORADIUSBYMEMBER_ROvariants β scroll down to the read-only section, it's right there π
The classic glow-up use case here is grabbing geospatial items near a specific point, no farther than a given amount of meters (or whatever unit you vibe with). Like, literally: suggesting nearby places to mobile users in your app. main character location services energy fr fr π
The radius goes in one of these units:
mfor meters.kmfor kilometers.mifor miles.ftfor feet.
The command can also hand you extra info if you ask nicely, with these options:
WITHDIST: also return the distance of the returned items from the specified center. and bestie, that distance comes back in the same unit you passed as the radius argument. no sneaky conversions πWITHCOORD: also return thelongitude,latitudecoordinates of the matching items.WITHHASH: also return the raw geohash-encoded sorted set score of the item, as a 52 bit unsigned integer. ngl this one is only useful for low level hacks or debugging β for the general user it's otherwise of little interest. iykyk π¬
Default behavior? unsorted items. yes, chaos. but you can invoke two different sorting methods with these:
ASC: sort returned items from the nearest to the farthest, relative to the center.DESC: sort returned items from the farthest to the nearest, relative to the center.
By default, all the matching items come back. want to cap it? use the COUNT <count> option to limit results to the first N matching items. and when you toss in ANY, the command returns as soon as it's found enough matches β so the results may not be the ones closest to your specified point. but on the other hand, the effort the server puts in is significantly lower. when ANY is not provided, the command does an effort proportional to the number of items matching the specified area and sorts them, so querying very large areas with a very small COUNT option may be slow even if just a few results come back. lowkey a trap, watch out πͺ€
By default the command returns the items to the client. but you can store the results instead, with one of these:
STORE: store the items in a sorted set populated with their geospatial information.STOREDIST: store the items in a sorted set populated with their distance from the center as a floating point number, in the same unit specified in the radius.
π Read-only variants π
Okay so here's the tea: since GEORADIUS and GEORADIUSBYMEMBER have a STORE and STOREDIST option, they're technically flagged as writing commands in the Valkey command table. because of that, read-only replicas will flag them, and Valkey Cluster replicas will redirect them to the primary instance even if the connection is in read-only mode (peep the READONLY command of Valkey Cluster).
So two read-only variants got added. they're exactly like the original commands but they refuse the STORE and STOREDIST options. their names? GEORADIUS_RO and GEORADIUSBYMEMBER_RO, and they can safely be used in replicas. slay π«Ά
π Alternative π
GEOSEARCH and GEOSEARCHSTORE with the BYRADIUS argument. just saying π
π REPLY π
One of the following:
- If no
WITH*option is specified, avalkey-protocol(7)Array reply of matched member names. - If
WITHCOORD,WITHDIST, orWITHHASHoptions are specified, the command returns avalkey-protocol(7)Array reply of arrays, where each sub-array represents a single item:- The distance from the center as a floating point number, in the same unit specified in the radius.
- The Geohash integer.
- The coordinates as a two items x,y array (longitude,latitude).
For example, the command GEORADIUS Sicily 15 37 200 km WITHCOORD WITHDIST will return each item in the following way:
["Palermo","190.4424",["13.361389338970184","38.115556395496299"]]
β COMPLEXITY β
O(N+log(M)) where N is the number of elements inside the bounding box of the circular area delimited by center and radius and M is the number of items inside the index. math is giving iconic π₯
π ACL CATEGORIES π
@geo @slow @write
π HISTORY π
- Available since:
3.2.0 - Changed in
6.2.0: Added theANYoption forCOUNT. - Changed in
7.0.0: Added support for uppercase unit names.
π§ EXAMPLES π§
127.0.0.1:6379> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
127.0.0.1:6379> GEORADIUS Sicily 15 37 200 km WITHDIST
1) 1) "Palermo"
2) "190.4424"
2) 1) "Catania"
2) "56.4413"
127.0.0.1:6379> GEORADIUS Sicily 15 37 200 km WITHCOORD
1) 1) "Palermo"
2) 1) "13.36138933897018433"
2) "38.11555639549629859"
2) 1) "Catania"
2) 1) "15.08726745843887329"
2) "37.50266842333162032"
127.0.0.1:6379> GEORADIUS Sicily 15 37 200 km WITHDIST WITHCOORD
1) 1) "Palermo"
2) "190.4424"
3) 1) "13.36138933897018433"
2) "38.11555639549629859"
2) 1) "Catania"
2) "56.4413"
3) 1) "15.08726745843887329"
2) "37.50266842333162032"
π SEE ALSO π
geoadd(3valkey), geodist(3valkey), geohash(3valkey), geopos(3valkey), georadiusbymember(3valkey), georadiusbymember_ro(3valkey), georadius_ro(3valkey), geosearch(3valkey), geosearchstore(3valkey)
and that's the whole page bestie!! π this one's from Valkey version 9.0.0, dated 2025-10-21 β go query some coordinates, you absolute icon πβ¨
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms