What is Geospatial in Redis: Explanation and Examples
geospatial refers to commands and data structures that store and query geographic locations like latitude and longitude. Redis lets you add, search, and calculate distances between points efficiently using GEOADD, GEORADIUS, and related commands.How It Works
Redis geospatial features work by storing geographic points as longitude and latitude coordinates inside a special sorted set. Each point has a name and a position on Earth.
Think of it like placing pins on a map. Redis keeps these pins organized so you can quickly find all pins near a certain location or calculate the distance between two pins.
Under the hood, Redis uses a geohash technique to encode locations into a format that allows fast searching and sorting by proximity.
Example
This example shows how to add locations and find nearby places using Redis geospatial commands.
GEOADD places 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania" GEORADIUS places 15 37 200 km WITHDIST
When to Use
Use Redis geospatial features when you need to store and query location data quickly. It is great for apps like delivery services, ride-sharing, or finding nearby stores.
For example, a food delivery app can find restaurants within 5 km of a user’s location instantly. Or a social app can show nearby friends on a map.
Key Points
- Redis geospatial stores locations as longitude and latitude in sorted sets.
- Commands like
GEOADD,GEORADIUS, andGEODISThelp add, search, and measure distances. - It uses geohashing internally for fast queries.
- Ideal for location-based apps needing quick proximity searches.
Key Takeaways
GEOADD to add locations and GEORADIUS to find nearby points.