Slides for getting started with the Geo API with Redis for the RedisConf 2019. Associated with a Project, Nailinda, which is open-source on github. Contributing is welcomed!
https://github.com/Podiihq/nailinda
2. PRESENTED BY
1 Introduction
Self Introduction & Introduction to GeoCoding.
2 Geo API
Breaking down the GEO API and seeing it in action live on
production, with source code to understand how
implementation is carried out
3 RediSearch & Q & A
Very brief introduction to RediSearch then Q & A comes up
next.
Agenda:
3. PRESENTED BY
Name: Linda Achieng’
Organization: Podii
Location: -0.107626, 34.768505
Role: Software Developer, specializing in Ruby
and Elixir
Introduction
7. PRESENTED BY
Geocoding is all about coordinates, the x and y axis.
Converting an address like Lutheran Church Kisumu, Kenya to -0.107626,
34.768505.
Location has become key in data collection, accurate location being the key
word, and accurate location goes back to the definition of Geocoding.
Users want answers to questions like “Who is near me?”, “what is near me?”,
“how far am I from X?”
How can we answer the users such questions, in ms?
8. PRESENTED BY
Users want answers
● “Who is near me?”
● “what is near me?”
● “how far am I from X?
13. PRESENTED BY
● Redis is an in-memory data structure store that's commonly used as a
database, a cache, and a message broker.
● Data structures in Redis are like Lego building blocks, helping developers
achieve specific functionalities with minimal complexity and at an amazing
speed .
● Redis also minimizes network overhead and latency because operations are
executed extremely efficiently in memory, right next to where the data is
stored.
● We’ll get to see geospatial functions that use two-dimensional indices built
from latitude and longitude data, and how we can use other Redis APIs to get
the most out of Redis to make our work easier, and most importantly, make
our applications fast.
15. PRESENTED BY
Write to the DB
Save the location by calling the GEOADD API
It takes in the longitude, latitude and member.
Updating the DB uses the same api.
Whether writing or updating the db, if everything goes well, response will
always be
{:ok, val}
17. PRESENTED BY
Read from the DB Read from the DB
You can use hashes instead of
coordinates.
GEOHASH returns a string, a
unique, that is user friendly and
browser(url) friendly.
The longer the string, the more
accurate it is.
We can query coordinates of one place,
a member, in your table.
The output is something like this:
19. PRESENTED BY
Read from DB
You can also use other APIs that
Redis has to query from DB.
Like ZRANGE – gives you list of
members within the range you
specify.
If you query a non-existent table (sad
path)
ZRANGE gives you a list of the
locations, but you can decide to get
Only the number of locations in your
database.
The ZCARD is a handy command when
you need that.
20. PRESENTED BY
Important Points to Note
Redis supports data structures such as strings, hashes, lists, sets, sorted sets
with range queries, bitmaps, hyperloglogs and geospatial indexes with radius
queries.
Geospatial indexing uses sorted sets which adds an ordered view to members,
sorted by scores.
When we add our coordinates (longitudes and latitudes), they are hashed together
to make a numeric geohash and the label is saved in a sorted set with the
geohash as a score in a sorted set (ZSET).
This how scores are derived:
● If A and B are two elements with a different score, then A > B if A.score is >
B.score.
● If A and B have exactly the same score, then A > B if the A string is
lexicographically greater than the B string. A and B strings can't be equal since
sorted sets only have unique elements.
21. PRESENTED BY
Queries for Searching the DB
GEORADIUS
Searching within a circle given by its center and its radius and returns the
members that fall inside it.
We specify the distance we want to query with, like “Which hospitals are 500m
away from me?”
We can also ensure that the response comes with the distance, or
coordinates or hash.
22. PRESENTED BY
GEORADIUSBYMEMBER
Works exactly like GEORADIUS with additional information, it accepts one of
the indexed members as the circle’s center.
The difference between this and GEORADIUS is that here you specify a
member, with the latter you use coordinates of where your searching.
See:
24. PRESENTED BY
Points to Note
Results are obtained in less than
10ms- for little amount of data.
If you want want great performance
(speed), keep the radius as small as
possible.
The more points that the circle
covers, the slower the query.
27. PRESENTED BY
Deleting from Database
You don’t need a location from your db anymore, then ZREM will work for
you.
Response shows you the number of locations deleted.
28. PRESENTED BY
RediSearch
Search engine that utilizes both its own datatype and the inbuilt Redis data
types.
Best features:
● Auto-Complete and suggestions/ hints
● Simplicity and ease of use – define an index, add data then search.
29. PRESENTED BY
Resources
Data Structures
- https://redislabs.com/why-redis/
- https://redis.io/topics/data-types-intro
Geo API
- https://redislabs.com/redis-best-practices/indexing-patterns/geospatial/
-