Home » Software Development » How to use geolocation with Redis 3.2

About Idan Fridman

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).

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.
(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Vitalii Recent comment authors

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

  Subscribe  
newest oldest most voted
Notify of
Vitalii
Guest
Vitalii

Nice!