Design the location and matching part of a ride-sharing app.
System designSenior
In shortDrivers send location updates every few seconds to a location service that keeps only the latest positions in memory, indexed by a geospatial grid (geohash, S2 or H3 cells). Matching queries nearby cells for available drivers, ranks them by ETA, and offers the ride with timeouts, while trips are stored durably elsewhere.
Model answer
Estimates: 1 million active drivers updating every 4 seconds is ~250,000 writes per second, mostly overwrites of the latest position. That data is ephemeral; trips and payments are durable and much lower volume.
Location service: drivers send updates over a persistent connection (WebSocket or gRPC stream) to gateway nodes. The service keeps the latest position per driver in memory (or Redis with geo commands), indexed by cells: geohash prefixes, Google S2 or Uber’s H3 hexagons. Each cell maps to the set of available drivers in it. Shard by city or region; a driver moving between cells is a remove and an add.
Matching: for a ride request, look up the rider’s cell and neighboring rings until enough candidates are found, filter by availability and vehicle type, rank by ETA from a routing service (not straight-line distance), and offer the ride to the best driver with a short timeout, then the next. Assignment must be atomic: a conditional update (available → reserved) or a per-driver lock ensures one driver gets one ride.
Trip service: durable trip records, state machine (requested → matched → arrived → in progress → completed), pricing, and payment handoff; location samples during a trip are batched to storage for receipts and disputes.
Mobile concerns: adaptive update frequency (less often when idle), batching and compression, handling dropped connections by resuming the stream, and showing the rider smoothed driver positions.
What they're looking for
Write-heavy ephemeral location data vs durable trip data, spatial indexing and cell sizes, sharding by region, the matching flow with offers and timeouts, consistency for driver assignment, mobile constraints (battery, connectivity), and scale estimates.
Common mistakes
- Writing every location update to a relational database.
- Scanning all drivers to find nearby ones.
- Letting two riders be matched to the same driver.