System design case study C09 · Geo · State machine

Design a Ride-Hailing Service (Uber/Ola-style)

Match riders with nearby drivers in seconds, track the trip live, and never give one driver to two riders.

What you'll be able to explain
  • Ingest hundreds of thousands of location updates per second
  • Find nearby drivers with a geo index
  • Dispatch without double-assigning drivers
  • Model a trip as a state machine
  • Partition by geography for scale and resilience

Scope

Functional

  • Drivers share location while online. Riders request a ride with pickup/drop.
  • Match a nearby available driver; driver accepts/declines.
  • Live tracking during pickup and trip; fare estimate and final fare.

Non-functional

  • Match within ~5 s (p95). Location freshness ~5 s.
  • A driver is never assigned two trips at once; a trip never has two drivers.
  • Highly available per city; one city's issue mustn't affect others.

Out of scope: payments (Case Study C12 of the book), ratings, pooling.

Size

  • 2M online drivers at peak, update every 4 s → 500k location writes/s.
  • 20M rides/day → ~230/s average, ~1,500/s peak requests.
  • Location history: 500k/s × 50 B ≈ 2 TB/day (to cold storage).

APIs and data

Driver:  PUT  /v1/drivers/me/location  {lat,lng,heading,ts}   (or a persistent stream)
Rider:   POST /v1/rides  {pickup, drop, product}  → {ride_id, status: MATCHING}
Driver:  POST /v1/offers/{offer_id}/accept
Both:    WS   /v1/rides/{id}/live      (driver position, ETA, status)
  • Driver state (in memory, per city shard): location, cell, status (AVAILABLE / OFFERED / ON_TRIP), vehicle type.
  • Trips (sharded SQL by city_id/ride_id): state, rider, driver, fares, timestamps.

The architecture

CLIENTDriver appCLIENTRider appSERVICELocation gatewayQUEUE / LOGKafkakey = driver_idCACHEGeo indexH3 cell → driversSERVICEDispatchper citySERVICETrip servicestate machineDATABASETrips DBEXTERNALRouting / ETAlocationrequest ridefind drivernearby availableETAsoffer
City-sharded dispatch: location updates feed an in-memory geo index that matching queries.

The 30-second summary

Say it in the interview

"Drivers stream locations through gateways into Kafka; per-city dispatchers keep an in-memory H3 index of available drivers. A ride request queries nearby cells, ranks by routing ETA, and offers to one driver at a time using a conditional status change with a timeout, so a driver can't be double-assigned. Trips are a validated state machine with events driving payments and notifications. Everything is partitioned by city for scale and blast-radius control."

Follow-up questions the interviewer may ask

  1. How do you prevent two riders getting the same driver?
  2. How does surge pricing work?
  3. How do you compute ETAs?
  4. What happens if the matching service in a city fails?

Model answers to each of these are in the book.