0
0
Redisquery~10 mins

Geo-proximity with sorted sets in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Geo-proximity with sorted sets
Add locations with GEOADD
Store locations in sorted set with geo info
Use GEORADIUS or GEOSEARCH to find nearby locations
Sort results by distance
Return locations with distances
Locations are added to a sorted set with geo info, then queried by radius to find and sort nearby places.
Execution Sample
Redis
GEOADD places 13.361389 38.115556 "Palermo"
GEOADD places 15.087269 37.502669 "Catania"
GEORADIUS places 15 37 200 km WITHDIST ASC
Add two places and find all within 200 km of point (15,37), sorted by distance.
Execution Table
StepCommandActionResultNotes
1GEOADD places 13.361389 38.115556 "Palermo"Add Palermo locationAdded 1Palermo stored in 'places' sorted set
2GEOADD places 15.087269 37.502669 "Catania"Add Catania locationAdded 1Catania stored in 'places' sorted set
3GEORADIUS places 15 37 200 km WITHDIST ASCFind locations within 200 km of (15,37)[["Catania", 56.4413], ["Palermo", 190.4424]]Results sorted by distance ascending
4EndNo more commandsQuery completeExecution stops after query
💡 All commands executed; GEORADIUS returns sorted nearby locations within radius.
Variable Tracker
VariableStartAfter 1After 2After 3Final
placesempty[Palermo][Palermo, Catania][Palermo, Catania][Palermo, Catania]
query_resultnonenonenone[[Catania, 56.4413], [Palermo, 190.4424]][[Catania, 56.4413], [Palermo, 190.4424]]
Key Moments - 2 Insights
Why does GEORADIUS return Catania before Palermo even though Palermo was added first?
GEORADIUS sorts results by distance when ASC is specified, so Catania is closer to (15,37) than Palermo, shown in execution_table row 3.
What does the WITHDIST option do in the GEORADIUS command?
WITHDIST makes GEORADIUS return the distance of each location from the center point, as seen in the result distances in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 3, what is the distance from (15,37) to Palermo?
A15 km
B56.4413 km
C190.4424 km
D37 km
💡 Hint
Check the distances listed in the GEORADIUS result in execution_table row 3.
At which step is the second location added to the sorted set?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the commands in execution_table rows 1 and 2.
If the radius in GEORADIUS was changed to 50 km, what would happen to the query_result?
ABoth Palermo and Catania would be returned
BNo locations would be returned
COnly Palermo would be returned
DOnly Catania would be returned
💡 Hint
Refer to the distances in execution_table row 3 and compare with 50 km radius.
Concept Snapshot
GEOADD key longitude latitude member - adds geo location to sorted set
GEORADIUS key longitude latitude radius m|km WITHDIST ASC|DESC - finds nearby locations sorted by distance
WITHDIST returns distance with each location
Results sorted by distance if ASC or DESC specified
Use for geo proximity queries in Redis
Full Transcript
This visual execution shows how Redis stores geo locations in a sorted set using GEOADD. Two locations, Palermo and Catania, are added with their longitude and latitude. Then GEORADIUS queries locations within 200 km of point (15,37), returning Catania and Palermo sorted by distance ascending. The WITHDIST option includes the distance in the results. The execution table tracks each command and result, and the variable tracker shows how the 'places' set and query results change. Key moments clarify why sorting happens by distance and what WITHDIST does. The quiz tests understanding of distances, command steps, and radius effects. This helps beginners see step-by-step how geo proximity queries work with Redis sorted sets.