Vishnu's Blog

Vishnu's Blog

Find Nearby places using Redis Geospatial search

Explore nearby places using Redis Geospatial search

Find Nearby places using Redis Geospatial search

Subscribe to my newsletter and never miss my upcoming articles

In your day-to-day life cycle, I am pretty much sure you would have definitely come across a situation where you want to know nearby hotels, grocery shops, hospitals etc.. from your location. In this article, I will explain about implementing similar usecase to fetch nearby places from your location using Redis Geospatial search capabilities.

There are many other alternative ways you can implement this Geospatial search using

  • MongoDB
  • Elasticsearch
  • Solr

In this article, i will explain about implementing the same using Redis. If your data size is small and decent enough to manage in Redis cache , i.e in memory then i would say this is a good fit for your use case. Also, Redis Geo query capabilities are very basic as of now. In other scenarios you can explore one of the above from MongoDB, Elasticsearch, Solr etc..

How Does It Work ?

Redis uses the sorted set to store the Geo points added. The way the sorted set is populated is using a technique called Geohash. Latitude and Longitude bits are interleaved to form a unique 52-bit integer. A sorted set double score can represent a 52-bit integer without losing precision.

This format allows for bounding box and radius querying by checking the 1+8 areas needed to cover the whole shape and discarding elements outside it. The areas are checked by calculating the range of the box covered, removing enough bits from the less significant part of the sorted set score, and computing the score range to query in the sorted set for each area.

Insert Records

I will be using GEOADD command to insert data into Redis.

Below is the Sytax of GEOADD command.

GEOADD key longitude latitude member

GEOADD Sicily 13.361389 38.115556 "Palermo"
GEOADD Sicily 15.087269 37.502669 "Catania"

I added 2 places "Palermo" and "Catania" under same key Sicily.

Get Nearby Places

Let's find out which is nearest place from given Latitude and Longitude using GEORADIUS command.

Assume my current location details are 15 & 37 (Longitude and Latitude respectively)

a) Get all places within 100 kms from my location :

GEORADIUS Sicily 15 37 100 km WITHDIST

Result :

127.0.0.1:6379> GEORADIUS Sicily 15 37 100 km WITHDIST
1) 1) "Catania"
   2) "56.4413"

b) Get all places within 200 kms from my location :

GEORADIUS Sicily 15 37 200 km WITHDIST

Result :

127.0.0.1:6379> GEORADIUS Sicily 15 37 200 km WITHDIST
1) 1) "Palermo"
   2) "190.4424"
2) 1) "Catania"
   2) "56.4413"

c) Get all places within 200 kms from my location in ASC order :

Sort the results by distance, i.e Get nearest places first and farthest places next.

GEORADIUS Sicily 15 37 200 km WITHDIST ASC

Result :

127.0.0.1:6379> GEORADIUS Sicily 15 37 200 km WITHDIST ASC
1) 1) "Catania"
   2) "56.4413"
2) 1) "Palermo"
   2) "190.4424"

d) Get all places within 200 kms from my location in ASC order with location details :

GEORADIUS Sicily 15 37 200 km WITHDIST WITHCOORD ASC

Result :

127.0.0.1:6379> GEORADIUS Sicily 15 37 200 km WITHDIST WITHCOORD ASC
1) 1) "Catania"
   2) "56.4413"
   3) 1) "15.08726745843887329"
      2) "37.50266842333162032"
2) 1) "Palermo"
   2) "190.4424"
   3) 1) "13.36138933897018433"
      2) "38.11555639549629859"

Thank you for reading

Hope you find these resources useful. If you like what you read and want to see more about system design, microservices, and other technology-related stuff... You can follow me on


Buy Me A Coffee

Interested in reading more such articles from vishnu chilamakuru?

Support the author by donating an amount of your choice.

Recent sponsors
#redis#tutorial#programming-blogs#developer
 
Share this