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_idPK,rider_id,driver_id,status,origin,destination,fare,created_at) - driver_locations (
driver_idPK,lat,lng,updated_at) - trip_events (
event_idPK,trip_id,type,ts)
APIs
- Request ride:
POST /api/tripswith body{ origin, destination } - Driver update location:
PUT /api/drivers/me/locationwith body{ lat, lng } - Trip status:
PUT /api/trips/:id/statuswith body{ status }
Hot Path
- Rider request → price/ETA → dispatch offer to nearest drivers
- Accept → start trip; update location stream → ETA updates
Matching Flow
- Ingest rider request; compute price and ETA
- Find nearest eligible drivers; dispatch offers with timeouts
- 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