The first version of nearest-driver search is always the same: take every available driver, compute the distance to the rider, sort, take the closest few. It is correct, it is five lines, and it works perfectly during testing.
It stops working because its cost grows with the number of drivers, and it runs on every single ride request. The two curves multiply.
Make the search area small first
The insight that fixes it is that you should never consider a driver who is obviously too far away. Rather than measuring everyone, divide the map into cells, find which cell the rider is in, and only examine drivers in that cell and its neighbours.
You are turning a problem that scales with total drivers into one that scales with local density. That is the whole game.
Straight-line distance is not travel time
A driver three hundred metres away across a river is further, in every sense the rider cares about, than one eight hundred metres away on the same road. Proximity gets you a shortlist of candidates. Ranking that shortlist properly needs routing.
The split is deliberate: cheap filtering to get from thousands to a handful, expensive accurate evaluation on the handful.
The data moves constantly
The complication specific to this problem is that every driver's position changes every few seconds. The index is being written to continuously while being read from continuously.
This shapes the engineering more than the lookup algorithm does:
- Positions are hot, small and ephemeral — store them accordingly rather than in your primary transactional database.
- Update frequency is a tunable, not a constant. More frequent means fresher matching and more load, and the right balance differs by context.
- Stale entries must expire on their own, because a driver whose app was killed will not tell you they are gone.
Correctness at the edges
Two drivers must never be offered the same ride, and a driver who has just accepted must leave the available pool immediately. That is a concurrency problem rather than a geometry one, and it is where the genuinely subtle bugs live.



