// STEP 1 — FIND NEARBY DRIVERS
$
GEORADIUS
drivers:geo:blr
77.5946 12.9716 5
km
ASC COUNT 10
WITHCOORD WITHDIST
GEORADIUS fetches all members of the geo set
drivers:geo:blr within
5km of the restaurant at coordinates (77.5946, 12.9716). Results are sorted
ASC (nearest first) and limited to
10.
WITHCOORD returns actual lat/lng,
WITHDIST returns distance.
1) "driver:D4" 0.82 km [77.5812 12.9680]
2) "driver:D2" 1.24 km [77.5900 12.9820]
3) "driver:D7" 2.17 km [77.5760 12.9550]
4) "driver:D1" 3.90 km [77.6100 12.9440]
// STEP 2 — CHECK AVAILABILITY
$
SMEMBERS
drivers:available:blr
SMEMBERS returns all members of the Redis Set tracking
available drivers in Bengaluru. We intersect this with GEORADIUS results — only drivers who are both
nearby AND
available are candidates.
1) "driver:D1"
2) "driver:D2"
3) "driver:D4"
4) "driver:D9"
// STEP 3 — ACQUIRE LOCK (atomic)
$
SET
agent:D4:lock
"order:ORD_2847"
NX EX 30
SET ... NX EX 30 is atomic.
NX = only set if Not eXists — if another request already locked this driver, this returns nil (no lock).
EX 30 = auto-expire in 30 seconds (driver window). This is a distributed mutex using Redis as the coordinator.
OK → lock acquired, driver reserved for 30s
// STEP 4 — RELEASE ON REJECTION
$
DEL
agent:D4:lock
If driver
D4 rejects (or times out), we call DEL to immediately release the lock. This allows the system to retry with the next-ranked driver without waiting for the 30s TTL to expire. The available set is
not modified until acceptance.
(integer) 1 → 1 key deleted, lock released
// STEP 5 — REMOVE FROM AVAILABLE SET
$
SREM
drivers:available:blr
driver:D4
On
acceptance, we remove the driver from the available set. Any concurrent GEORADIUS+SMEMBERS query will no longer see this driver as a candidate. Combined with the lock, this ensures no double-assignment is possible.
(integer) 1 → driver removed from available pool