DEV Community

Tan Yong He
Tan Yong He

Posted on

🌍 Redis Geohashing: Storing and Querying Location Data with Ease

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"
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

You can now query nearby locations:

GEOSEARCH places FROMLONLAT 15 37 BYRADIUS 200 km
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

🧠 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!

Top comments (3)

Collapse
Β 
nevodavid profile image
Nevo David β€’

insane how quick redis can pull this off imo - you ever feel torn picking between geohashing in redis vs a full spatial db long-term?

Collapse
Β 
tanyonghe profile image
Tan Yong He β€’

Totally agree β€” Redis is ridiculously fast for geospatial lookups, especially with GEOSEARCH. It's hard not to appreciate that speed when you're prototyping or need real-time responsiveness.

That said, I do feel the tension when thinking long-term. Redis geohashing is awesome for radius queries, but once you need more complex spatial relationships or accurate geometry handling, options like PostGIS really start to shine.

My take: if the use case requires fast nearby-object lookups (e.g. building a ride-hailing app), Redis is a solid choice. But if you're dealing with deeper spatial logic or long-term persistence (e.g. urban planning or GIS analysis), PostGIS is likely the better tool.

Curious to hear what others are doing!

Collapse
Β 
dotallio profile image
Dotallio β€’

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?