Software Development

How to use geolocation with Redis 3.2

Recently I was looking for a solution how to tell if couple of Longitudes, Latitudes actually in the same Radius. So in the beginning I was thinking using Elasticsearch to store all my geo-locations and use their mechanism in order to search whether a point is within the requested Radius of other points.

Elastic-search is great product but I wasn’t sure if I wanted to maintain it only for our geolocation service. Our project already using Redis. I found our that on the new Redis version I actually could use all geo-location features.

Redis like in Redis -> Everything goes fast!!

Redis is an amazing key,value data source. Incredibility fast and efficient.

On their new upcoming version (not stable yet) Redis 3.2. a we have new GEO API features.

Redis is orginized with Geo-Sets (backed by sorted sets).

Let’s assume we have one set that is identified by a key, and that holds some members that are associated with geo locations:

GEOADD geoset 8.663877549.5282537 "location1" 8.379628148.9978127 "location2 "8.665351,49.553302 "location3"

Now let’s suppose that I want to check which of my entries is close to a specific geo-point

Let’s assume my input geo-point is:

8.6582361, 49.5285495

And I want to have all my stored locations with-in 10 km Radius. We do it this way:

GEORADIUS key 8.6582361, 49.5285495 10 km

The result will return the key-names(Location1, Location2,..) which actually close by 10 km Radius. Cool eh? how does it work? Redis encode the longtitude, latitude to a digit using technique called geo-hash. They forming a unique 52 bit integer.

So for example to determine if two lat,lon points are close we can geo-hash them first:

  • Tel Aviv (32.0663, 34.7664) -> 1432645594530418
  • Netanya (32.334, 34.8578) -> 1432650217449870

Pay attention to the prefixes. By that we can tell how close the points. The bigger the prefix the closer they are.

Happy Redis!

Reference: How to use geolocation with Redis 3.2 from our JCG partner Idan Fridman at the IdanFridman.com blog.

Idan Fridman

Idan is Software engineer with experience in Server side technologies. Idan is responsible for various infrastructure models in the software industry(Telecommunications, Finance).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Vitalii
Vitalii
8 years ago

Nice!

Back to top button