Real time location tracking like Uber using Redis
Let’s say we need to create an App like Uber which saves drivers location on a regular interval. If someone request a driver, App will display nearest to farthest drivers available to serve the user.
Redis inherently support geospatial data structures which will help us in making such App. In this Post I’ll show Redis commands that can help achieve this. One can use Client Libraries to implement in their Apps.
Assume we have 9 drivers and we are saving their locations for Karachi, Pakistan. Let’s add hypothetical locations for all 9 drivers in key “karachi” in Redis
GEOADD karachi 24.886819 67.075467 driver1 GEOADD karachi 24.878233 67.049457 driver2 GEOADD karachi 24.875711 67.047008 driver3 GEOADD karachi 24.879434 67.036419 driver4 GEOADD karachi 24.870967 67.036948 driver5 GEOADD karachi 24.870907 67.052568 driver6 GEOADD karachi 24.882015 67.049126 driver7 GEOADD karachi 24.887119 67.043831 driver8 GEOADD karachi 24.880334 67.036154 driver9
Now App user who is in location 24.871147 67.032050 wants a driver to server him, then below Redis command can get list of 5 drivers in Sorted order within 10 Km radius along with Distance (in Km) and Geo Coordinates
GEORADIUS karachi 24.871147 67.032050 10 km WITHCOORD WITHDIST COUNT 5
Result would be something like this
1) 1) "driver5" 2) "0.5448" 3) 1) "24.87096816301345825" 2) "67.0369478790157558" 2) 1) "driver4" 2) "0.6045" 3) 1) "24.87943321466445923" 2) "67.03641812229345476" 3) 1) "driver9" 2) "0.6061" 3) 1) "24.88033443689346313" 2) "67.03615451129287806" 4) 1) "driver8" 2) "1.4825" 3) 1) "24.88712042570114136" 2) "67.04383218168452174" 5) 1) "driver3" 2) "1.6755" 3) 1) "24.87571030855178833" 2) "67.04700818729716616"
Whenever there is any update for location of driver, just execute below for a driver
GEOADD karachi 24.887119 67.043831 driver1
Redis made it easy and saved us from lot of data processing using this built-in data structure.
Nice artical, as per my requirement.