Ride Sharing

Designing a service like Uber/Lyft: matching, dispatch, pricing, and tracking.

Gateway + matching + pricing + trips + location + events

Requirements

  • Request → match → accept → pickup → dropoff → complete
  • Real-time driver locations; surge pricing
  • ETAs, cancellations, and reassignments

High-Level Design

  • Matching on proximity, driver supply, and constraints
  • Trip lifecycle managed by state machine; idempotent updates
  • Location updates via streaming ingestion; geo-indexed store

Capacity & Sizing

  • Driver location updates/sec and ingest throughput
  • Trip TPS at peak, surge factors by region
  • Geo index size and query QPS

Key Components

  • Matching service, Dispatch queue, Driver pool
  • Trip service with state machine
  • Location service and geo-index (R-tree/Geohash)

Data Model

Trips with events and driver locations

  • trips (trip_id PK, rider_id, driver_id, status, origin, destination, fare, created_at)
  • driver_locations (driver_id PK, lat, lng, updated_at)
  • trip_events (event_id PK, trip_id, type, ts)

APIs

  • Request ride: POST /api/trips with body { origin, destination }
  • Driver update location: PUT /api/drivers/me/location with body { lat, lng }
  • Trip status: PUT /api/trips/:id/status with body { status }

Hot Path

  1. Rider request → price/ETA → dispatch offer to nearest drivers
  2. Accept → start trip; update location stream → ETA updates

Matching Flow

  1. Ingest rider request; compute price and ETA
  2. Find nearest eligible drivers; dispatch offers with timeouts
  3. Driver accepts; confirm and start trip; reassign on timeout

Scaling

  • Partition geographies into cells; maintain driver pools per cell
  • Geo-index for proximity queries; stream processing for locations
  • Backpressure on dispatch queue with prioritization

Caching & TTL

  • Short TTL cache for driver locations by cell
  • Cache static map tiles and traffic segments

Failure Modes & Mitigations

  • Location gaps → extrapolation and last-known fallbacks
  • Driver scarcity → surge pricing and queueing
  • Provider outages → degrade to simpler matching rules

Observability

  • Match success rate, time-to-assign, cancel/reassign rate
  • Driver online counts, location ingest lag