Full answer
Requirements
Functional:
- A rider requests a ride from a pickup point and sees an estimated fare and arrival time.
- The system finds a nearby available driver, offers the trip, and the driver accepts or declines.
- Both sides see each other's live location until the trip ends; the trip is priced and paid.
Non-functional:
- Matching completes in a few seconds; location on the map lags by no more than a few seconds.
- A driver is never assigned two trips at once; a trip is never lost once accepted.
- Each city keeps working if another region has problems.
Capacity estimates
Assumptions: 20M rides per day, 1M drivers online at peak, one location update every 4 seconds, 100 bytes per update, 2 KB per stored trip record.
- Ride requests: 20M / 86,400 = about 230 per second on average; a few thousand per second at city rush hours.
- Location updates: 1M / 4 = 250,000 per second, about 25 MB per second of incoming data.
- Trip storage: 20M x 2 KB = 40 GB per day, about 14.6 TB per year before replication.
- Current driver positions: 1M x 100 bytes = 100 MB, which fits in memory many times over.
Location updates dominate, so they must not go to a relational database one row at a time.
API
POST /v1/rides {"pickup": {"lat": 12.97, "lng": 77.59}, "dropoff": {...}, "product": "economy"}
-> 202 {"ride_id": "r_81f", "status": "SEARCHING"}
GET /v1/rides/{ride_id} -> {"status": "DRIVER_ASSIGNED", "driver": {...}, "eta_s": 240}
WS /v1/drivers/stream driver sends {"lat", "lng", "heading", "ts"} every 4 s; server pushes trip offers
POST /v1/offers/{offer_id}/accept
Data model
driver_location(in memory, per city shard): driver_id, cell_id, lat, lng, status (available, on_trip, offline), updated_at.trips(durable, partitioned by city then trip_id): trip_id, rider_id, driver_id, status, pickup, dropoff, fare, timestamps.location_history(append-only, for disputes and route replay): written in batches to a log or column store.
High-level design
flowchart LR
D[Driver app] -- "WebSocket, every 4 s" --> GW[Connection gateway]
GW --> LOC[Location service]
LOC --> GEO[(Geo index per city)]
LOC --> K[(Location log)]
R[Rider app] --> API[Ride API]
API --> M[Matching service]
M --> GEO
M --> ETA[ETA service]
M --> T[(Trips DB)]
M -- "offer" --> GWMatching
- The ride API creates a trip in
SEARCHING. - The matching service looks up the pickup's cell and its neighbours (a geohash or H3 cell of about 1 km) and gets available drivers.
- It ranks the top candidates by road ETA, not straight-line distance, because a river or highway can make a close driver far away.
- It offers the trip to the best driver with a 10 to 15 second timeout. To prevent double booking, it sets the driver's status from
availabletoofferedwith a conditional write; only one trip can win. - On accept, the trip moves to
DRIVER_ASSIGNEDin the durable store. On decline or timeout, the next driver gets the offer.
Partitioning by city keeps each match local: a match in Mumbai never needs data from Delhi.
Bottlenecks and trade-offs
- Location write rate. Updates go to memory first and to a log in batches. Losing a few seconds of positions is acceptable; losing a trip is not, so the two have different durability.
- Hot cells. An airport or stadium can hold thousands of drivers. Smaller cells or a cap on candidates per query keep lookups cheap.
- Offer one driver at a time or several? One at a time is fair and avoids races but adds seconds per decline. Offering to two or three with "first accept wins" is faster but wastes driver attention.
- Connection gateways hold long-lived WebSockets; a gateway crash means drivers reconnect, so gateways stay stateless apart from the socket and route by driver_id.
- Surge pricing reads supply and demand per cell from the same index and runs as a separate service so pricing bugs cannot block matching.
By level
Fresher: List the rider and driver flows, sketch the APIs and explain that drivers send their location often. A fresher should be able to explain why nearby drivers are found by grid cell instead of scanning all drivers.
Mid: Do the update-rate math, design the geo index and the matching flow with timeouts, and show how a conditional write stops double booking. Partitioning by city should come up naturally.
Senior: Cover hot cells, ETA-based ranking, connection gateway failure, durability trade-offs between positions and trips, and surge pricing as an isolated service. Expect a view on single versus parallel offers and on regional failure.
Key points
- Location updates (250,000 per second) dominate, not ride requests
- Keep current positions in an in-memory geo index partitioned by city
- Rank by road ETA, not straight-line distance
- Conditional write on driver status prevents double assignment
- Trips are durable state machines; positions can tolerate small loss
Common mistakes
- Writing every location update as a row in a relational database
- Matching by straight-line distance only
- Offering one driver to two riders at the same time
- Treating positions and trips with the same durability requirements
Follow-ups
- How would you implement surge pricing per area?
- How do you handle a driver who loses network mid-trip?
- How would you support shared rides with several pickups?
Tips
- Separate the high-rate location path from the low-rate trip path early
- Show the conditional write that prevents double booking
- Mention city-level partitioning
Evaluation rubric
- Correct capacity math showing location updates dominate
- Sensible geo index and matching flow
- Prevents double assignment
- Discusses durability differences and hot cells