Introduction to the

GEO is a location module added to Redis after version 3.2, which means you can use Redis to implement nearby location functions

Compute with a database

The general method is to limit the number of elements through the rectangular area, and then calculate the full distance of the elements in the area and then sort. This can significantly reduce the amount of computation.

select id from positions where x0-r < x < x0+r and y0-r < y < y0+r
Copy the code

In order to increase performance, the above SQL needs to add a bi-directional composite index in latitude and longitude coordinates. However, database query performance is limited, and in high concurrency situations, this may not be a good solution.

GEO algorithm

  • The common geographical distance sorting algorithm in the industry isGeoHashAlgorithm, also used by RedisGeoHashAlgorithm.
  • GeoHashAlgorithm willLatitude and longitude data are mapped to one-dimensional integersIn this way, all elements will be mounted on a line, and the distance between the two dimensional coordinates mapped to the one dimensional points will be very close. When we want to calculate “nearby people”, we first map the target position to this line, and then obtain nearby points on this one-dimensional line.
  • It sees the entire earth as a two-dimensional surface, divided into a series of square squares, like a go board. All map element coordinates will be placed on a single square. The smaller the square, the more accurate the coordinates. These squares are then integer coded, and the closer they are, the closer they are.
  • After encoding, the coordinates of each map element will become an integer through which the coordinates of the element can be restored. The longer the integer, the smaller the loss of the restored coordinate value.
  • GeoHashThe algorithm will continue to do this integer oncebase32coding(0-9,a-z minus a, I, L, O)It becomes a string.
  • In Redis, latitude and longitude are encoded as 52-bit integerszsetInside,zsetvalueIs an element ofkeyscoreGeoHashThe 52-bit integer value of.
  • When using Redis for Geo queries, keep in mind that its internal structure is really just onezset(skiplist). throughzsetscoreSort to get the other elements near the coordinates (it’s a little more complicated, but that’s enough to understand), by puttingscoreThe original coordinates of the elements are obtained by restoring them to coordinate values.

Redis GEO instruction

1. Increase geoadd
geoadd key longitude latitude member [longitude latitude member ...]
Copy the code
127.0.0.1:6379> geoadd beijing 116.403856 39.924043 gugong
(integer) 1
127.0.0.1:6379> geoadd beijing 116.343620 39.947633 dongwuyuan
(integer) 1
127.0.0.1:6379> geoadd beijing 116.328643 39.900272 xizhan 116.415324 39.931231 meishuguan 116.416852 39.887607 tiantan
(integer) 3
Copy the code

Delete zrem from zset

2. Distance geodist
geodist key member1 member2 [unit]
Copy the code
127.0.0.1:6379> Geodist Beijing Gugong Dongwuyuan KM "6.9402" 127.0.0.1:6379> Geodist Beijing Gugong Dongwuyuan # Default unit M "5768.5737" 127.0.0.1:6379> Geodist Beijing XIZhan XIZhan "0.0000"Copy the code

The units of distance can be m, km, ML, and ft, representing meters, kilometers, miles, and feet, respectively.

3. Geopos position
geopos key member [member ...]
Copy the code
Geopos Beijing gugong 1) 1) "116.4038559794426" 2) "39.92404192186725" 127.0.0.1:6379> Geopos Beijing Tiantan XIZhan 1) 1) "116.41685396432877" 2) "39.887607839922914" 2) 1) "116.32864147424698" 2) "39.900271306834973"Copy the code
4. The hash value geohash
geohash key member [member ...]
Copy the code
127.0.0.1:6379> Geohash Beijing Gugong 1)"Copy the code

Latitude and longitude string encoding is base32 encoding, which can be directly by http://geohash.org/wx4g0gfwqk0 to find the latitude and longitude

5. Nearby location Georadiusbymember
  1. The queryireaderThe scope of20Maximum within km3The elements are aligned by distance, which does not exclude itselfdesc
127.0.0.1:6379> georadiusbymember company ireader 20 km count 3 asc
1) "ireader"
2) "juejin"
3) "meituan"
Copy the code
  1. Three optional parameterswithcoord withdist withhashUsed to carry additional parameters,withdistVery useful, it can be used to show distance
georadiusbymember key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DES]
Copy the code
127.0.0.1:6379> Georadiusbymember Beijing Gugong 5 km withcoord withdist Withhash count 3 ASC 1) 1) "gugong" 2) "0.0000" 3) (INTEGER) 4069885568932443 4) 1) "116.4038559794426" 2) "39.92404192186725" 2) 1) "meishuguan" 2) "1.2634" 3) (INTEGER) 4069885710390435 4) 1) "116.415325519028" 2) "39.93123039107514" 3) 1) "tiantan" 2) "4.2014" 3) (INTEGER) 4) 1) "116.41685396432877" 2)"Copy the code
  1. Queries nearby elements based on coordinate values
 georadius key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DES]
Copy the code
127.0.0.1:6379> Georadius Beijing 116.383882 39.922061 5 km withcoord withdist WithHash Count 3 ASC 1) 1) "gugong" 2) "1.7180" 3) (INTEGER) 4069885568932443 4) 1) "116.4038559794426" 2) "39.92404192186725" 2) 1) "meishuguan" 2) "2.8693" 3) (INTEGER) 4069885710390435 4) 1) "116.415325519028" 2) "39.93123039107514" 3) 1) "dongwuyuan" 2) "4.4588" 3) (INTEGER) 4069879836419688 4) 1) "116.34361892938614" 2) "39.94763257169722Copy the code

Matters needing attention

In practical applications there could be millions and millions of pieces of data, and we know that Redis Geo will put them all in a Zset set. In the cluster environment of Redis, the set may be migrated from one node to another. If the data of a single key is too large, the migration of the cluster will be greatly affected. In the cluster environment, the data amount corresponding to a single key should not exceed 1M, otherwise the cluster migration will lag. The online services are affected.

Therefore, it is recommended that Geo data be deployed in a separate Redis instance instead of a clustered environment.

If the amount of data is over 100 million or even larger, it is necessary to split Geo data by country, by province, by city, and even by district in populous megacities. This can significantly reduce the size of a single Zset collection.

Reference source

Redis Deep Adventure Core Principle and Application practice _ Qian Wenpin