Case study: Ride sharing (Uber)
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
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
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
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 | timestamps5. High-level design
6. Deep dives
Location ingestion and indexing
- 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
- Take the rider’s pickup point, look up its cell and neighbours (expanding the ring if too few drivers).
- Filter available drivers (vehicle type, rating) and compute road ETA with a routing service, not straight-line distance.
- Offer the trip to the best driver. Change the driver’s state from
availabletoofferedwith a conditional update (only if still available), so two concurrent matches can’t grab the same driver. - 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.
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.
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:A strong answer covers:
- Estimates: 2M drivers / 4 s = 500k location updates/s; ~830 ride requests/s at peak.
- Location service ingests updates (via WebSocket/UDP-ish streaming) and updates an
in-memory geospatial index (Redis GEO / H3 or geohash cell → driver set) with expiry;
only the latest position matters; history goes to a stream for analytics.
- Matching: rider location → cell + neighbours → candidate available drivers → rank by
ETA (routing service) → offer to one driver at a time with timeout; lock the driver
(conditional state change) to avoid double assignment.
- Trip service: durable state machine in a strongly consistent DB (requested, accepted,
arrived, in_progress, completed, cancelled); events to Kafka.
- Live tracking: rider app receives driver location via WebSocket/push.
- Pricing: surge per cell from supply/demand ratio computed by a stream job; fare quote
locked at request time.
- Sharding by city/region; multi-region for availability.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.