← Back to System Design, page 3

system-design-0051 · Hard senior

Design a ride-hailing service like Uber or Ola that matches riders with nearby drivers in a few seconds for 20 million rides per day.

Drivers stream their location every few seconds into an in-memory geospatial index partitioned by city and cell; a matching service queries nearby cells, ranks candidate drivers by ETA and offers the trip to one driver at a time with a short timeout. Trips are a state machine stored durably. The hard part is the write rate of location updates (about 250,000 per second for 1 million online drivers), not the ride requests (about 230 per second on average).

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

mermaid
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" --> GW

Matching

  1. The ride API creates a trip in SEARCHING.
  2. 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.
  3. It ranks the top candidates by road ETA, not straight-line distance, because a river or highway can make a close driver far away.
  4. 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 available to offered with a conditional write; only one trip can win.
  5. On accept, the trip moves to DRIVER_ASSIGNED in 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