Redis isn't just a blazing-fast in-memory data store — it also comes packed with geospatial capabilities that are incredibly handy when working with location-based applications. At the core of this is geohashing.
📌 What Is Geohashing?
Geohashing is a method of encoding geographic coordinates (latitude and longitude) into a single string or number. Redis uses a 52-bit representation of this concept to store locations efficiently.
Think of it as compressing a lat/lon point into a compact format that allows for:
- Fast insertion
- Efficient querying of nearby locations
- Sorting by distance
🗺️ How Redis Uses Geohashing
Redis provides the GEOADD
, GEOPOS
, GEODIST
, and GEORADIUS
(deprecated in favor of GEOSEARCH
) commands to handle geospatial data.
Example:
GEOADD places 13.361389 38.115556 "Palermo"
GEOADD places 15.087269 37.502669 "Catania"
Here's what's happening:
- Redis converts each lat/lon pair into a geohash.
- It stores the geohash under a sorted set (zset), using it as the score.
- Location names (like "Palermo") are the values.
+--------------------------+
| Sorted Set (ZSET) |
+--------------------------+
| Score (Geohash) | Member |
+--------------------------+
| 34790932423489 | Rome |
| 34923498230912 | Milan |
| 34982342344933 | Naples |
+--------------------------+
Each location's latitude & longitude is converted into a geohash score.
You can now query nearby locations:
GEOSEARCH places FROMLONLAT 15 37 BYRADIUS 200 km
This will return all locations within 200km of the specified point — fast and sorted by proximity.
+-----------------------------+
| |
| [Center Point] |
| 🧭 |
| / | \ |
| / | \ |
| / | \ |
| [Nearby locations] |
| 📍 📍 |
| |
+-----------------------------+
Using GEOSEARCH BYRADIUS, Redis returns all points within the specified radius, sorted by proximity from the center point.
🧠 Why Use It?
Ideal for "what's near me?" features – Great for location-based apps like food delivery, rideshare, social meetups, and store locators.
In-memory performance – Queries are lightning-fast compared to traditional spatial databases.
Simple, elegant API – Easy-to-use commands like GEOADD, GEOSEARCH, and GEODIST.
Sorted results by distance – Redis returns nearby places ordered by how close they are — no need for post-processing.
Compact storage – Uses 52-bit geohashes internally, making it memory-efficient.
Atomic operations – Redis commands are atomic, which avoids race conditions in high-concurrency systems.
Multi-purpose ZSET integration – Since geohashes are stored in sorted sets, you can combine geospatial queries with scoring, ranking, or expiring keys.
Cluster-friendly – Works well in distributed Redis setups for scalability and high availability.
Built-in distance calculations – No need for custom Haversine functions — Redis does it out-of-the-box in meters, kilometers, miles, or feet.
🧾 Final Thoughts
Redis geohashing is a powerful yet underused feature that provides a fast and efficient way to store and query geospatial data — perfect for building features like "find nearby places" with minimal setup. With simple commands and in-memory speed, it's a great tool to have in your backend toolbox for location-aware applications.
Give it a try and see how easy geospatial can be with Redis! 🌐✨
🚀 Pro tip: Try it out with redis-cli
or libraries like ioredis
for Node.js or redis-py
for Python!
Love how clearly you break down geohashing in Redis. Have you ever combined GEO queries with other ZSET scores, like for ranking nearby locations by rating too?