Matching riders with nearby drivers
A ride-hailing startup matches by scanning all drivers in a city. Design real-time matching that scales to many cities.
To design ride matching, keep the latest driver locations in memory partitioned by area, with a geospatial index so a search touches a few cells instead of every driver. Model ride offers as a state machine with a lease per driver, and partition by city for scale and failure isolation.
The system today: A ride-hailing startup runs in one city. Drivers send their location every few seconds and the server stores the latest in a table. To match a rider it scans every driver, computes distances, and picks the closest available one.
What has to change: Grow to 100 cities and 500,000 drivers sending a location every four seconds. Design the location pipeline and the matching: find nearby available drivers in under a second, avoid offering the same driver to two riders, and keep working if a server or a whole region fails.
Questions to ask first
- How accurate must locations be, and how stale can one get before we ignore it?
- What does a good match mean: nearest, soonest pickup, or fair across drivers?
- How do we handle a driver who declines or does not answer?
- Is the system partitioned by city, or do users cross regions?
Where the interviewer digs in
- A geospatial index (geohash, quadtree or similar) to find nearby drivers without scanning all
- The write rate of location updates and where the latest location lives
- Offering a ride to a driver exactly once, with timeouts and the next candidate
What a strong answer covers
- Computes the load: about 125,000 location updates a second, which rules out writing each to a relational table.
- Keeps latest locations in memory or a fast store partitioned by area, with a geospatial index so a query touches a few cells, and archives history separately.
- Models matching as a state machine (offered, accepted, declined, expired) with a lock or lease per driver so two riders cannot get the same driver.
- Partitions by city or region for scale and failure isolation, and explains failover and what stale data costs.
Common mistakes
- Writing every location update to a durable relational database.
- Offering the same driver to several riders at once and sorting it out afterwards.
- Ignoring edge cases at cell borders when searching nearby.