Implementing Nearby Search with Redis GEO in a NestJS Backend
This tutorial demonstrates how to use Redis' GEO data structure together with Docker, RedisInsight, and a NestJS backend to store location data, perform distance calculations and radius searches, and finally display nearby points on a web map using AMap and Axios.
The article explains that many location‑based features such as ride‑hailing, shared power banks, hotels, and nearby people are implemented using Redis GEO, which stores geographic coordinates and supports distance and radius queries.
First, Docker Desktop is used to run a Redis container, exposing port 6379 and mounting a data volume. RedisInsight is then installed to visualize keys.
Next, a NestJS project is created ( npm i -g @nestjs/cli && nest new nearby-search ) and the redis client library is added. A RedisModule provides a REDIS_CLIENT provider that connects to the local Redis instance, and a RedisService wraps geoAdd , geoPos , geoList , and geoSearch methods using the Redis client.
The AppController injects RedisService and defines routes:
GET /addPos?name=...&longitude=...&latitude=... – adds a point.
GET /allPos – lists all points.
GET /pos?name=... – retrieves a point's coordinates.
GET /nearbySearch?longitude=...&latitude=...&radius=... – searches points within a radius (km).
After adding several sample locations (e.g., Tiananmen, Cultural Palace), the distance between points is verified with GEODIST , and radius queries demonstrate how the search radius affects results.
For the frontend, a static public/index.html is served, loading the AMap JavaScript SDK. A map is initialized centered on a sample coordinate, and markers and circles are added to illustrate points.
Axios is then used to call the /nearbySearch API, retrieve nearby points, and dynamically create AMap markers for each result, drawing a circle representing the search radius.
The final result shows a working end‑to‑end system where a NestJS backend stores location data in Redis GEO, provides APIs for adding and querying points, and a web page visualizes nearby points on a map.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.