hardDesignConceptMidSeniorStaff
You need every driver within two kilometres of a moving rider, updated every four seconds. What index makes that cheap?
A geospatial index for nearby drivers reduces latitude and longitude to ordered cells such as geohash, S2 or H3. Query the rider's cell plus neighbours, keep the hot current-position index in memory because positions churn, and filter candidates by true distance afterwards.
6 min readUpdated 2026-08-15
Answer
Short answer
Use a geospatial cell index such as geohash, S2 or H3 for nearby-driver search. Store each driver's current cell in an in-memory index, query the rider's cell plus neighbouring cells to avoid boundary misses, then run a real distance calculation on the candidate set. The index narrows the search cheaply; it is not the final distance answer.
Was this answer helpful?
Likely follow-ups
- Cell size is fixed at design time but driver density varies by a factor of a thousand between a city centre and a motorway. What do you do?
- Where do you keep the driver's location history for billing disputes, and why is it a different store from the one serving searches?
- The rider asks for drivers within two kilometres but you want them ranked by driving time. What extra system does that need, and how many candidates do you send it?
- How would you answer the reverse query, "which riders are inside this driver's ten-minute reach", without inverting the whole index?
Related questions
- A table has forty million rows, thirty-five million of them soft-deleted, and every query for active rows has got slower. Would a partial index help, and what would you have to be careful about?hardAlso on indexing5 min
- Three queries hit the same table with different filters. What composite indexes do you build, and how do you order the columns?hardAlso on indexing6 min
- A query filters on two columns and sorts on a third. What index do you create, and what does it cost you?hardAlso on indexing6 min
- This table has fourteen indexes and writes have got slower. How do you work out which ones to drop?hardAlso on indexing6 min