Case study: Ride sharing (Uber)

Topic 12 of 15 in Week 7: Classic case studies15 min readAdvancedFree course

Overview

Ride-hailing combines a firehose of location updates with latency-sensitive matching and money-handling trips. The key is to treat these differently: locations are ephemeral and best-effort; matching must be fast and must never double-book a driver; trips and payments must be durable and correct.

Try the exercise first: 45 minutes, then compare.

1. Requirements

Functional

  • Riders see nearby cars, get a fare estimate and request a ride.
  • Drivers go online and offline, share location, and accept or decline offers.
  • The system matches requests to nearby available drivers; both sides see live trip progress.
  • Fares with surge pricing; payment at the end.

Non-functional

  • Matching within a few seconds; location freshness within ~5 seconds.
  • A driver is never assigned two trips; trip and payment data are never lost.
  • Highly available, especially in busy cities at peak.

2. Estimates

Text
location updates : 2M drivers ÷ 4 s = 500,000 / s   (tiny messages: id, lat, lng, heading, ts)
ride requests    : 50,000 / minute ≈ 830 / s
nearby-car views : riders polling the map → tens of thousands / s
location history : 500k/s × ~50 bytes ≈ 25 MB/s ≈ 2 TB / day (for analytics, not the hot path)

3. API

Text
Driver:  WS  {"type": "location", "lat": …, "lng": …, "ts": …}       (every 4 s)
         POST /v1/drivers/me/status {"status": "available"}
         POST /v1/offers/{id}/accept | /decline
Rider:   GET  /v1/estimate?from=…&to=…                     → price, ETA (quote_id)
         POST /v1/rides {"quote_id": …, "pickup": …, "dropoff": …} → ride_id
         WS  trip updates (driver location, status changes)

4. Data model

Text
geo index (Redis, in memory): cell_id → set of available driver IDs; driver_id → last location (TTL ~30 s)
drivers:  driver_id | status (offline, available, offered, on_trip) | vehicle | rating
trips:    trip_id | rider_id | driver_id | state | pickup | dropoff | quote | timestamps

5. High-level design

Ride-sharing design. The driver app sends GPS every 4 seconds to a location service handling about 500 thousand updates a second, which updates a geo index in Redis mapping each cell to its available drivers. The rider app calls the matching service, which queries nearby cells in the geo index, computes ETAs and makes an offer, then hands the trip to the trip service, a state machine backed by a durable trips database. Matching also uses the pricing service for surge by area. If a driver doesn’t reply within about 10 seconds, the offer goes to the next driver. Trip states move from requested to accepted, arrived, in progress and completed.

6. Deep dives

Location ingestion and indexing

Finding nearby drivers with a grid. The map is divided into cells. The rider’s cell and its eight neighbours form a 3 by 3 search area; drivers inside it are candidates, while drivers outside the nine cells are ignored. Drivers send GPS every few seconds, each point maps to a cell ID using a geohash or H3, the index maps each cell to driver IDs in Redis, a query checks the rider’s cell plus eight neighbours, and candidates are ranked by road ETA.

  • Drivers stream updates over a persistent connection to location servers (sharded by city or by driver ID).
  • Each update maps the coordinates to a cell (geohash prefix or Uber’s H3 hexagons) and moves the driver between cell sets if needed. Redis GEO commands or an in-memory index per city work well.
  • Entries carry a short TTL; drivers who stop sending disappear automatically.
  • Updates are also published to a stream for trip tracking, analytics and ETA models; they’re not written to a database synchronously.

Matching

  1. Take the rider’s pickup point, look up its cell and neighbours (expanding the ring if too few drivers).
  2. Filter available drivers (vehicle type, rating) and compute road ETA with a routing service, not straight-line distance.
  3. Offer the trip to the best driver. Change the driver’s state from available to offered with a conditional update (only if still available), so two concurrent matches can’t grab the same driver.
  4. If the driver declines or doesn’t answer within ~10 seconds, release them and offer to the next candidate.

Matching per city can be handled by a dedicated matching service instance (or partition) per area, avoiding cross-region coordination. Batch matching (optimising several requests at once every second or two) improves overall pickup times in busy areas.

Trip state

Trips are a state machine: requested → accepted → arrived → in progress → completed (or cancelled). Each transition is a conditional write in a strongly consistent database and emits an event (to Kafka) for notifications, billing, receipts and analytics. During the trip, the rider’s app receives the driver’s location over a WebSocket.

Pricing and surge

A stream job counts open requests and available drivers per cell over short windows and computes a surge multiplier per area. The fare quote given at request time is stored with an ID and honoured, so price changes mid-request don’t surprise the rider.

Payments

At completion, the trip service emits an event; the payment service charges the stored payment method with an idempotency key (the trip ID). See the payment system case study.

7. Trade-offs and bottlenecks

  • 500k location writes per second: keep them in memory, shard by city, avoid durable writes on the hot path.
  • Location freshness vs battery and bandwidth: update more often only during active trips.
  • Strong consistency where it matters (driver assignment, trips, payments), best effort elsewhere.

8. Follow-up questions

  • A huge event ends and 50,000 people request rides at once? Queue requests, batch matching, surge pricing to attract drivers, rate-limit retries.
  • Region outage? Cities are independent partitions; fail over a city’s services to another region, rebuilding the in-memory index from fresh driver updates within seconds.
  • Shared rides? Matching becomes a routing optimisation over multiple riders and detours.

Recap

Ride-sharing streams driver locations into an in-memory geospatial index keyed by cells, finds candidates in the rider’s cell and its neighbours, ranks them by road ETA and offers trips one driver at a time with atomic state changes to avoid double assignment. Trips are durable state machines emitting events; surge pricing comes from stream processing of supply and demand per area; and the system partitions naturally by city.

Exercise

Design it yourself (45 minutes)

Set a 45-minute timer and design a ride-hailing service for a large country: riders request rides and see nearby cars; drivers send their location every 4 seconds; the system matches a rider to a nearby available driver within seconds; trips are tracked live; prices include surge. Assume 2 million active drivers at peak and 50,000 ride requests per minute at peak. Use the framework, then compare.

Text
1. Clarifying questions:
2. Functional requirements:
3. Non-functional requirements:
4. Estimates:
5. API:
6. Data model:
7. High-level design:
8. Deep dives (location index, matching, trip state, surge):
9. Trade-offs and bottlenecks:

Summary

  • Driver locations are a high-volume stream where only the latest value matters: keep them in an in-memory geospatial index.
  • Index locations by cell (geohash or H3) and search the rider's cell plus neighbours, then rank candidates by road ETA.
  • Match carefully: offer to one driver at a time and change driver state atomically so a driver is never assigned twice.
  • Trips are a durable state machine in a strongly consistent store; locations are ephemeral.
  • Partition naturally by city or region; compute surge pricing from supply and demand per area with a stream job.