Design Uber: location, matching, and trip service
Uber
Guided ModeStep 5 β Matching Service
Request Flow
Geo query + score
API forwards ride request to the Matching Service. It issues GEORADIUS to Cache for 20 drivers within 3 km, then ranks by: ETA (60%) + acceptance rate (30%) + rating (10%).
Atomic lock + dispatch
Before dispatching, the Matching Service acquires a Redis lock: SET driver:lock:<id> 1 NX EX 30. This atomically prevents two concurrent requests from dispatching the same driver. Then push notification via WebSocket.
Retry / expand radius
On driver decline or 8s timeout: release lock, try the next-ranked candidate. After 3 candidates at 3 km radius, expand to precision-5 (~5 km). This handles low-supply areas gracefully.
Why We Need This
Without a distributed lock, two riders matching simultaneously could both select the same nearest driver. Redis SETNX makes the lock atomic β the second request automatically fails the lock check and tries the next candidate.
Key Insight
The matching lock is the critical safety mechanism. Redis SETNX (SET if Not eXists) makes acquiring it atomic β no race conditions possible between concurrent matching workers.
Step 5 of 8
