Redis GEOSEARCH for Nearest Driver Dispatch
Learn Redis GEOSEARCH for nearest driver dispatch: GEOADD courier GPS, radius query with WITHDIST, GEORADIUS vs GEOSEARCH, and a Node.js nearest-drivers API.
Redis GEOSEARCH is the command you use when you need the nearest drivers inside a map radius — without scanning MongoDB on every dispatch. For food delivery and on-demand marketplaces, that query is the heart of courier matching: given a restaurant (or dropoff) coordinate, return available drivers sorted by distance.
This tutorial is only about Redis geo for nearest driver dispatch: GEOADD, GEOSEARCH, and keeping the index limited to available couriers. No batching, sockets, or payments mixed in.
Redis GEOSEARCH · Redis GEOADD · nearest driver Redis · Redis geo radius · Node.js Redis geo
What is Redis GEOSEARCH?
Redis GEOSEARCH queries a Redis geo index (a sorted set of members with lon/lat) and returns members within a circle or box. With ASC, WITHDIST, and COUNT, you get a shortlist of the closest drivers — ideal for dispatch.
Why teams pick Redis for this instead of a full table scan:
- GPS pings write often; Redis handles high-frequency
GEOADDupdates in memory - Radius reads stay O(log N) style lookups instead of loading every driver document
- The same pattern works for “drivers near restaurant” and “drivers near customer”
Courier GPS → GEOADD drivers:geo lng lat <driverId> Restaurant ready → GEOSEARCH … BYRADIUS 5 km ASC WITHDIST COUNT 20 Result → nearest driver ids + distanceKm → offer job
GEOADD: index live driver locations
Before you can run Redis GEOSEARCH, drivers must exist in the geo key. Redis GEOADD stores longitude, latitude, and a member id (your driver ObjectId as a string).
Axis order is always longitude, then latitude (GeoJSON order). Mixing axes breaks every radius query.
Node.js — Redis GEOADD
await redis.geoAdd('drivers:geo', {
longitude: lng,
latitude: lat,
member: String(driverId),
});
redis-cli
GEOADD drivers:geo 9.70 4.05 64f1a2b3c4d5e6f7a8b9c0d1
Skip [0, 0] defaults. They make “nearest driver” results cluster on null-island and destroy dispatch quality.
GEOSEARCH: nearest drivers by radius
This is the money command. Redis GEOSEARCH nearest drivers from a restaurant (or customer) point:
Node.js — Redis GEOSEARCH WITHDIST
const rows = await redis.geoSearchWith(
'drivers:geo',
{ longitude: restaurantLng, latitude: restaurantLat },
{ radius: 5, unit: 'km' },
['WITHDIST'],
{ SORT: 'ASC', COUNT: 20 }
);
// [{ member: 'driverId', distance: '0.42' }, …]
redis-cli — GEOSEARCH nearest drivers
GEOSEARCH drivers:geo FROMLONLAT 9.70 4.05 BYRADIUS 5 km ASC WITHDIST COUNT 20
Map each hit to { driverId, distanceKm }. Redis solved proximity; your marketplace still owns who gets the offer.
GEORADIUS vs GEOSEARCH
Older docs and Stack Overflow answers still say Redis GEORADIUS. Redis 6.2+ recommends GEOSEARCH (and GEOSEARCHSTORE) as the modern replacement. Same job — nearest members in a radius — cleaner syntax and forward-compatible APIs in node-redis v4 (geoSearchWith).
If you maintain legacy code: migrating GEORADIUS → GEOSEARCH … FROMLONLAT … BYRADIUS is usually a straight swap for nearest-driver dispatch.
Online vs offline: keep the geo index honest
A Redis geo key for dispatch must mean available right now. Otherwise GEOSEARCH returns busy or offline couriers.
-
Driver status = available
Run
GEOADDwith the latest GPS fix. -
offline / busy / on_delivery
Run
ZREM drivers:geo <driverId>so Redis GEOSEARCH cannot return them. -
Sync on every status/location write
Hook the driver update path so the geo index never drifts from Mongo.
Presence sync
if (status !== 'available') {
await redis.zRem('drivers:geo', String(driverId));
return;
}
await redis.geoAdd('drivers:geo', {
longitude: lng,
latitude: lat,
member: String(driverId),
});
Treat Redis as optional: if REDIS_URL is missing, return an empty nearest list instead of crashing. Mongo remains the source of truth for the driver profile.
Node.js nearest-driver API shape
Expose Redis GEOSEARCH behind a small authenticated route so ops and demos can verify the index:
HTTP
GET /api/logistics/drivers/nearest?lng=9.70&lat=4.05&radiusKm=5&count=20
{
"engine": "redis-geosearch",
"radiusKm": 5,
"count": 2,
"drivers": [
{ "driverId": "…", "distanceKm": 0.42 },
{ "driverId": "…", "distanceKm": 1.15 }
]
}
When Redis is down or unset: "engine": "unavailable", drivers: []. That keeps white-label installs bootable without Redis while production can enable REDIS_URL for real nearest-driver dispatch.
GEOADD on GPS · ZREM when not available · GEOSEARCH FROMLONLAT … BYRADIUS … ASC WITHDIST COUNT for the shortlist. That is the full nearest-driver Redis geo loop.
FAQ — Redis GEOSEARCH for driver dispatch
Is Redis GEOSEARCH good for food delivery driver matching?
Yes. Food delivery dispatch is a radius + sort problem. Redis GEOSEARCH returns the nearest available drivers around a restaurant or dropoff without scanning every driver row on each order.
Should I still use Redis GEORADIUS?
Prefer GEOSEARCH on Redis 6.2+. GEORADIUS still works on many servers but GEOSEARCH is the documented successor and matches modern Node Redis clients.
Does GEOSEARCH replace my database?
No. Keep driver profiles and orders in MongoDB (or Postgres). Use Redis only as a live geo index for nearest-driver reads.
What radius should I use for nearest drivers?
Start around 3–5 km in dense cities; make it admin-configurable. Cap COUNT (e.g. 20) so you never fan out offers to the entire fleet.
Explore logistics demos on the demo hub and the logistics feature overview.