0
0
Redisquery~15 mins

Geo-proximity with sorted sets in Redis - Deep Dive

Choose your learning style9 modes available
Overview - Geo-proximity with sorted sets
What is it?
Geo-proximity with sorted sets in Redis is a way to store and query geographic locations efficiently. It uses a special data structure called a sorted set to keep track of places with their longitude and latitude. This allows you to find nearby locations, measure distances, and sort results by closeness. It's like having a fast map inside your database.
Why it matters
Without geo-proximity features, finding nearby places would be slow and complicated, especially with many locations. This feature solves the problem by making location queries fast and easy, which is crucial for apps like delivery services, ride-sharing, or local search. Without it, users would face delays or inaccurate results, hurting user experience and business.
Where it fits
Before learning this, you should understand basic Redis data types and sorted sets. After mastering geo-proximity, you can explore advanced geospatial queries, combining location with other data filters, or scaling Redis for large datasets.
Mental Model
Core Idea
Geo-proximity with sorted sets stores locations as scores in a sorted set, letting Redis quickly find and sort places by distance.
Think of it like...
Imagine a library where every book is placed on a shelf according to its distance from the entrance. When you want a book close to the door, you just look at the start of the shelf. The sorted set is like that shelf, and the scores are distances from a point.
Sorted Set (ZSET) with Geo Scores:

┌───────────────┐
│  Location A   │ Score: distance from center
├───────────────┤
│  Location B   │ Score: distance from center
├───────────────┤
│  Location C   │ Score: distance from center
└───────────────┘

Query: Find all locations within radius R, sorted by score (distance).
Build-Up - 7 Steps
1
FoundationUnderstanding Redis Sorted Sets
🤔
Concept: Learn what Redis sorted sets are and how they store members with scores.
A sorted set in Redis is a collection of unique members, each with a numeric score. Redis keeps these members sorted by their scores. You can add, remove, and query members by score or rank. For example, you can store player scores in a game leaderboard.
Result
You can retrieve members sorted by their scores efficiently.
Understanding sorted sets is key because geo-proximity uses scores to represent distances, enabling fast sorting and querying.
2
FoundationBasics of Geospatial Coordinates
🤔
Concept: Learn how geographic locations are represented by longitude and latitude.
Every place on Earth can be described by two numbers: longitude (east-west) and latitude (north-south). These coordinates let us pinpoint exact spots on a map. For example, New York City is approximately at longitude -74 and latitude 40.7.
Result
You can represent any location with two numbers.
Knowing coordinates is essential because Redis geo commands use them to calculate distances and proximity.
3
IntermediateStoring Locations with GEOADD Command
🤔
Concept: Learn how to add geographic points to a Redis sorted set using GEOADD.
The GEOADD command adds a location to a sorted set with longitude, latitude, and a name. For example: GEOADD places -74 40.7 "NewYork" stores New York City. Redis encodes these coordinates into a score internally for fast access.
Result
Locations are stored in Redis and ready for geo queries.
Using GEOADD connects geographic data to sorted sets, enabling Redis to manage location data efficiently.
4
IntermediateQuerying Nearby Locations with GEORADIUS
🤔Before reading on: do you think GEORADIUS returns locations sorted by distance or randomly? Commit to your answer.
Concept: Learn how to find all locations within a radius from a point, sorted by distance.
GEORADIUS lets you search for members within a radius from given longitude and latitude. For example, GEORADIUS places -74 40.7 10 km finds all places within 10 kilometers of New York. You can ask Redis to sort results by distance and include the distance in the output.
Result
You get a list of nearby locations sorted from closest to farthest.
Knowing that GEORADIUS sorts results by distance helps you build efficient location-based features like 'find nearby restaurants'.
5
IntermediateMeasuring Distance Between Two Locations
🤔Before reading on: do you think Redis calculates distance using simple math or a special formula? Commit to your answer.
Concept: Learn how to measure the distance between two stored locations using GEODIST.
GEODIST returns the distance between two members in the sorted set. Redis uses the Haversine formula, which calculates the shortest distance over the Earth's surface. For example, GEODIST places "NewYork" "Boston" km returns the distance in kilometers.
Result
You get the accurate distance between two points on Earth.
Understanding Redis uses the Haversine formula explains why distances are accurate for real-world applications.
6
AdvancedUsing GEOSEARCH for Flexible Queries
🤔Before reading on: do you think GEOSEARCH can filter by shape other than radius? Commit to your answer.
Concept: Learn about GEOSEARCH, a newer command that allows searching by radius or bounding box with sorting options.
GEOSEARCH lets you find locations within a radius or a rectangular box. You can specify sorting by distance or by member name. For example, GEOSEARCH places FROMLONLAT -74 40.7 BYRADIUS 10 km ASC returns locations within 10 km sorted closest first.
Result
You can perform more flexible and efficient geo queries.
Knowing GEOSEARCH expands your ability to query locations beyond simple radius searches.
7
ExpertInternal Encoding and Precision Limits
🤔Before reading on: do you think Redis stores exact coordinates or approximations internally? Commit to your answer.
Concept: Learn how Redis encodes geo coordinates into 52-bit integers and the impact on precision.
Redis converts longitude and latitude into a 52-bit integer using a geohash-like encoding. This allows fast sorting and querying but introduces small precision errors (about 1 meter). Understanding this helps when very high precision is required or when comparing results with other systems.
Result
You understand the tradeoff between speed and precision in Redis geo data.
Knowing Redis uses approximate encoding prevents confusion when tiny distance differences appear in queries.
Under the Hood
Redis stores geographic points by encoding longitude and latitude into a single 52-bit integer score using a geohash-like method. This score is used as the member's score in a sorted set. When querying, Redis calculates distances using the Haversine formula and filters members by comparing scores and coordinates efficiently. The sorted set structure allows fast range queries and sorting by distance.
Why designed this way?
This design balances speed and storage efficiency. Using sorted sets leverages Redis's existing fast data structure. Encoding coordinates into scores allows range queries without scanning all members. Alternatives like storing raw coordinates would be slower. The tradeoff is slight precision loss, acceptable for most applications.
┌───────────────┐
│ GEOADD cmd    │
│ (lon, lat, m) │
└──────┬────────┘
       │ Encode lon/lat to 52-bit score
       ▼
┌───────────────┐
│ Sorted Set    │
│ (member,score)│
└──────┬────────┘
       │
       │ Query with GEORADIUS/GEODIST
       ▼
┌───────────────┐
│ Calculate     │
│ distances     │
│ (Haversine)   │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does GEORADIUS return results sorted by distance by default? Commit yes or no.
Common Belief:GEORADIUS returns results in random order unless you sort them manually.
Tap to reveal reality
Reality:GEORADIUS returns results sorted by distance from the center point by default.
Why it matters:Assuming random order leads to extra sorting in application code, causing inefficiency and slower responses.
Quick: Is Redis geo data stored as exact longitude and latitude values? Commit yes or no.
Common Belief:Redis stores exact longitude and latitude values internally without any approximation.
Tap to reveal reality
Reality:Redis encodes coordinates into a 52-bit integer approximation, causing minor precision loss (~1 meter).
Why it matters:Expecting exact precision can cause confusion when distances differ slightly from other systems or calculations.
Quick: Can you use GEOADD to store multiple locations with the same name? Commit yes or no.
Common Belief:You can store multiple locations with the same member name in a geo sorted set.
Tap to reveal reality
Reality:Member names must be unique in a sorted set; adding the same name updates the location.
Why it matters:Trying to store duplicates causes overwriting, leading to data loss or unexpected query results.
Quick: Does GEODIST calculate straight-line distance or driving distance? Commit your answer.
Common Belief:GEODIST calculates driving or walking distance between two points.
Tap to reveal reality
Reality:GEODIST calculates straight-line (great-circle) distance using the Haversine formula, ignoring roads or paths.
Why it matters:Using GEODIST for routing or travel time estimation leads to inaccurate results.
Expert Zone
1
Redis geo commands rely on sorted sets but use a special encoding that differs from normal numeric scores, affecting how range queries work internally.
2
The precision of geo queries depends on the radius size; very small radii may return unexpected results due to encoding limits.
3
Combining geo queries with other Redis data structures requires careful design to maintain performance and data consistency.
When NOT to use
Avoid Redis geo-proximity when you need extremely high precision (sub-meter) or routing-aware distances (like driving routes). Instead, use specialized GIS databases like PostGIS or routing engines like OSRM.
Production Patterns
In production, Redis geo is often used for quick nearby searches combined with caching. It's common to store user locations or points of interest and update them frequently. Developers combine GEOSEARCH with other filters using Lua scripts or Redis modules for complex queries.
Connections
Geohashing
Geo-proximity encoding in Redis builds on geohashing principles.
Understanding geohashing helps grasp how Redis encodes coordinates into sortable scores for fast spatial queries.
Spatial Databases (e.g., PostGIS)
Redis geo-proximity offers a lightweight alternative to full spatial databases.
Knowing spatial databases clarifies Redis geo's tradeoffs between speed and advanced GIS features.
Sorting Algorithms
Redis sorted sets use efficient sorting to keep geo data ordered by distance.
Understanding sorting algorithms explains how Redis maintains fast access to nearest locations.
Common Pitfalls
#1Trying to store multiple locations with the same name expecting them to coexist.
Wrong approach:GEOADD places -74 40.7 "PlaceA" GEOADD places -73 41.0 "PlaceA" # overwrites previous PlaceA
Correct approach:GEOADD places -74 40.7 "PlaceA1" GEOADD places -73 41.0 "PlaceA2" # unique names for each location
Root cause:Misunderstanding that sorted set members must be unique strings; adding the same member updates its score.
#2Assuming GEODIST returns driving distance instead of straight-line distance.
Wrong approach:GEODIST places "NewYork" "Boston" km # expecting driving distance
Correct approach:Use GEODIST for straight-line distance; use external routing services for driving distance.
Root cause:Confusing geographic distance with real-world travel distance.
#3Using GEORADIUS without specifying sorting, expecting unordered results.
Wrong approach:GEORADIUS places -74 40.7 10 km # expecting random order
Correct approach:GEORADIUS places -74 40.7 10 km WITHDIST ASC # results sorted by distance
Root cause:Not knowing GEORADIUS sorts by distance by default and how to control output.
Key Takeaways
Redis geo-proximity uses sorted sets with encoded scores to store and query locations efficiently.
Coordinates are stored approximately using a 52-bit encoding, balancing speed and precision.
Commands like GEOADD, GEORADIUS, GEODIST, and GEOSEARCH let you add locations and find nearby places quickly.
Understanding the difference between straight-line distance and real-world travel distance is crucial for correct use.
Redis geo features are great for fast, simple location queries but have limits compared to full GIS systems.