Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How ride-hailing apps find nearby drivers: location updates, geospatial indexing with geohash and H3 hexagons, matching, ETAs and surge pricing.
Quick answer: Drivers' phones send their GPS position to the server every few seconds. The server stores each position in a geospatial index, which divides the map into cells so that "who is near this point?" becomes a quick lookup of a few cells instead of a scan of every driver. When you request a ride, the system finds candidate drivers in the cells around you, estimates how long each would take to reach you by road, picks the best match, and sends an offer to that driver. If they decline or time out, it moves on to the next.
This article explains the general design of ride-hailing systems. Uber has published a good deal about its approach, including its open-source H3 grid; the full details of its current dispatch system are not public.
The naive solution is to compute the distance from the rider to every online driver and sort. With hundreds of thousands of drivers moving constantly and thousands of requests per second, that is far too slow.
Ordinary database indexes do not help much either. A B-tree index works on one dimension. You could index latitude, but "latitude between X and Y" still returns a band stretching around the whole planet. Location is two-dimensional, and it needs a different kind of index.
Each driver's app sends its position every few seconds over a persistent connection; see how WebSockets work. That is a very large volume of small writes, with two useful properties:
So current positions are typically kept in a fast in-memory store, updated in place, and partitioned by region. A separate stream of the same updates flows through a message queue into long-term storage for trip records, analytics and machine learning.
The trick is to cut the map into cells and give each cell an ID. Then "nearby" means "in the same or neighbouring cells", and cell IDs can be indexed like any other key.
A geohash divides the world into a grid recursively and encodes each square as a short string. Longer strings mean smaller squares, and squares that share a prefix are close together.
A quadtree splits a square into four, and splits busy squares again. Dense city centres get many small cells and empty countryside gets a few large ones, so each cell holds a similar number of points.
Google's S2 library projects the Earth onto a cube and uses a space-filling curve to number cells. Uber developed H3, a hierarchical grid of hexagons, described in its engineering blog and at h3geo.org.
Why hexagons? In a square grid, a cell's eight neighbours are at two different distances: edge neighbours are closer than corner neighbours. A hexagon's six neighbours are all the same distance from its centre. That makes "everything within k rings of this cell" a clean approximation of a circle, and makes smoothing and movement analysis more uniform.
| Geohash | Quadtree | H3 | |
|---|---|---|---|
| Cell shape | Rectangle | Square | Hexagon |
| Adapts to density | No | Yes | Fixed resolutions |
| Neighbour distances | Uneven | Uneven | Uniform |
| Typical use | Simple proximity search | In-memory spatial indexes | Ride-hailing, supply and demand analysis |
With a cell index, a nearby search is:
The index is essentially a map from cell ID to the set of drivers in it. In-memory stores handle this well; Redis, for example, has built-in geospatial commands. See why Redis is fast.
When a driver moves from one cell to another, they are removed from the old set and added to the new one.
The closest driver in a straight line is often not the best choice.
Once a driver is chosen:
A trip is a state machine: requested, matched, driver arriving, in progress, completed or cancelled. Two guarantees are essential and require careful concurrency control: one driver must never be assigned two riders at once, and one request must never be assigned to two drivers. See how database transactions handle concurrent users.
The same grid is used to balance supply and demand. For each cell, the system compares open requests with available drivers. Where demand outstrips supply, prices rise. The intent is to nudge some riders to wait and attract more drivers to the area. Hexagonal cells make it natural to smooth these values across neighbouring cells so prices do not jump sharply at a boundary.
It indexes drivers by map cell. A search only looks at drivers in the rider's cell and its neighbours, not at every driver in the city.
A short string that encodes a rectangular area of the map. Longer strings are smaller areas, and nearby places usually share a prefix.
All six neighbours of a hexagon are the same distance away, which makes radius searches, smoothing and movement analysis more uniform than with squares.
Matching uses estimated travel time, driver availability and overall efficiency across several requests, not straight-line distance alone.
Ride matching is a geospatial search problem wrapped in a real-time system. Dividing the map into indexed cells makes "who is nearby" cheap, road-based ETAs make the choice sensible, and batching makes it efficient for everyone. The same pattern underlies food delivery, bike sharing and any other "find the nearest" service.