Hi Team , today we will see a very interesting and useful feature of Redis.The Redis geospatial indexing

What is Geostail indexing?

Redis geospatial indexes let you store coordinates and search for them. This data structure is useful for finding nearby points within a given radius or bounding box.

It’s really useful in applications that deal with large amounts of geographic data, such as mapping applications, geolocation services, and spatial analysis. Geographic indexes allow these applications to quickly query and analyze data based on its location without having to sift through large amounts of data to find relevant information.

For example you can use it for mobile apps or GPS to find specifics points like restaurants nearby and so on, that looks great!

So let’s see how to use it a pick some simple example to show the main commands

About geospatial usage

  • How does it works?

The way the sorted set is populated uses a technique called geohash. The latitude and longitude bits are interleaved to form a unique 52-bit integer. We know that a set of sorted double fractions can represent a 52-bit integer without loss of precision.

  • What Earth model does it use?

The model assumes that the Earth is a sphere since it uses the Haversine formula to calculate distance. This formula is only an approximation when applied to the Earth, which is not a perfect sphere. The introduced errors are not an issue when used, for example, by social networks and similar applications requiring this type of querying. However, in the worst case, the error may be up to 0.5%, so you may want to consider other systems for error-critical applications.

How to add geospatial items?

Redis has several commands related to geospatial indexing (GEO commands) but unlike other commands these commands lack their own data type. These commands actually piggy back on the sorted set datatype. This is achieved by encoding the latitude and longitude into the score of the sorted set using the geohash algorithm.

GEOADD command

Adds the specified geospatial items (longitude, latitude, name) to the specified key. Data is stored into the key as a sorted set, in a way that makes it possible to query the items with the GEOSEARCH command.

  • How it works?

The command takes arguments in the standard format x,y so the longitude must be specified before the latitude. There are limits to the coordinates that can be indexed: areas very near to the poles are not indexable.

  • Limit:

The exact limits, as specified by EPSG:900913 / EPSG:3785 / OSGEO:41001 are the following: Valid longitudes are from -180 to 180 degrees. Valid latitudes are from -85.05112878 to 85.05112878 degrees.The command will report an error when the user attempts to index coordinates outside the specified ranges.

This implies that there will be no GEODEL command . The Geo index structure is just a sorted set so we will use the dedicated commad ZREM 😉

Below an example of a geospatial index , you can see that it is a sorted set TYPE data:

You can use the command type and your goespatial index to have the proof


GEOADD key [NX | XX] [CH] longitude latitude member [longitude
  latitude member ...]

GEOADD FRANCE 4.850000 45.750000 "Lyon" 2.333333 48.866667 "Paris"

As you can see new index were added BUT to show you how to remove it I made a mistake when add the longitude of Lyon so we can use the ZREM command AS on the screenshot BELOW then I added the correct value with GEOADD command.


You can also redo the command and check if the result is updated instead of removing the Index what was done in the screenshot above 😉

you add many entries when you use GEOADD Command

  • Here an example of ZREM command


Return the distance between two members in the geospatial index represented by the sorted set.

For exmaple in our geospatial index named FRANCE we can use the GEODIST command to check distance between Paris and Lyon


GEODIST key member1 member2 [M | KM | FT | MI]

The unit must be one of the following, and defaults to meters:

    m for meters.
    km for kilometers.
    mi for miles.
    ft for feet.


Here for example you can get the distance between 2 cities added in the key FRANCE

GEORADIUS Command (deprecated ,use GEOSEARCH Command instead)

It can be replaced by GEOSEARCH and GEOSEARCHSTORE with the BYRADIUS argument when migrating or writing new code.

Return the members of a sorted set populated with geospatial information using GEOADD, which are within the borders of the area specified with the center location and the maximum distance from the center (the radius).

In other words, it will give you all the points nearby to the central point you have entered.

Can be used for example in mobile app to show for example hostels or free park places nearby.


GEORADIUS key longitude latitude radius <M | KM | FT | MI>

> GEORADIUS FRANCE 1 43 100 km
1) “Tournefeuille”
2) “Toulouse”

This command will display all points within a radius of 100km for the point with longitude 1 and latitude 43

You can also add parameters like the WITHDIST parameter and WITHCOORD to have the distance and the location each points.

> GEORADIUS FRANCE 1.272105 43.576698 100 km withdist withcoord asc
1) 1) "Tournefeuille"
2) "6.0608"
3) 1) "1.3468363881111145"
2) "43.58289924176722252"
2) 1) "Toulouse"
2) "13.2450"
3) 1) "1.43333226442337036"
2) "43.60000100542929147"

ASC and DESC are used to diplay the points by order ( the nearest or the farthest)


I advise you to use this command as it replaces the GEORADIUS command


Return the members of a sorted set populated with geospatial information using GEOADD, which are within the borders of the area specified by a given shape. This command extends the GEORADIUS command, so in addition to searching within circular areas, it supports searching within rectangular areas.


GEOSEARCH key <FROMMEMBER member | FROMLONLAT longitude latitude>   <BYRADIUS radius <M | KM | FT | MI> | BYBOX width height <M | KM |   FT | MI>> [ASC | DESC] [COUNT count [ANY]] [WITHCOORD] [WITHDIST]   [WITHHASH]

If you use the DESC option you will have the further points for longitude 1 and latitude 44 in order:

Use the FROMMEMBER argument to query from a member of the key


Use the FROMLONLAT argument to input the longitude and latitude instead of a member of the group

As you can see on the screenshot below I have checked the exact place on longitude 1 and latitude 44

It is near Toulouse so the command is working as expected , we see that Toulouse and Tournefeuille are the first cities displayed next to this point


Now you see how to use Redis geospatial index , we have seen some examples but there are many ways to use it , I advise you to check on Redis site there is a lot of explanations and use cases with geospatial indexes.

Feel free to visit my other blogs and you can also check out dbi bloggers for even more tips and tricks 🙂