{}DSA Atlas

Design a Ride-Sharing Service (Uber / Lyft)

Real-time matching of riders and drivers — high-frequency location ingestion, in-memory geospatial indexing (geohash/H3), the dispatch/matching service and offer flow, trip state machine, ETA and routing, surge pricing from supply/demand per cell, payments, and consistency for "one driver, one trip".

Advanced5 interview questions

1. Requirements

Functional

  • Rider requests a ride (pickup, destination, product type) and sees fare estimate.
  • Match to a nearby driver; driver accepts/declines.
  • Real-time tracking of the driver's location for the rider; trip lifecycle.
  • Fare calculation, payment, ratings.

Non-functional

  • 20M trips/day; 5M drivers online at peak.
  • Matching within a few seconds; location updates visible within ~1–2 s.
  • Strong consistency for assignments (one driver ↔ one trip); availability for everything else.
  • Global, but each city operates largely independently.

2. Estimation

QuantityValue
Driver location updates5M / 4 s ≈ 1.25M writes/s
Ride requests20M/day ≈ 230/s avg, ~2,000/s peak (concentrated per city)
Rider tracking reads~1M active trips × 1 update/2 s ≈ 500k pushes/s
Location history1.25M/s × 50 B ≈ 5 TB/day

3. API

HTTP
POST /v1/fare-estimates   {"pickup": {...}, "dropoff": {...}, "product": "uberx"}  → {"fare_token": "…", "amount": 1840, "surge": 1.3, "expires_in": 300}
POST /v1/rides            {"fare_token": "…"}   Idempotency-Key: …   → 201 {"ride_id": "r_1", "status": "matching"}
GET  /v1/rides/{id}       → status, driver, vehicle, ETA
WS   /v1/stream           driver: location pings, offers; rider: driver position, status changes
POST /v1/offers/{id}/accept

4. High-level design

driver app rider app location gateway(WebSocket) Kafka geo index (H3)in-memory, sharded surge (Flink →Redis per cell) location history ride API(gateway) dispatchcandidates · ETA · offer routing / ETAengine trip servicestate machine (DB) pricing payments offer → driver
Location is a firehose into memory; dispatch reads it; trips are the durable source of truth.

5. Deep dives

Geo index

Each update: compute H3 cell (resolution ~8, ≈ 0.7 km² hexagons), move the driver between cell sets if the cell changed, store latest position. Query: k-ring around the pickup cell (ring 1, then ring 2 …) until enough available drivers. See geospatial indexing.

Python
def candidates(pickup, product, want=15, max_ring=4):
    origin = h3.latlng_to_cell(pickup.lat, pickup.lng, 8)
    found = []
    for k in range(max_ring + 1):
        for cell in h3.grid_ring(origin, k):
            found += [d for d in geo_index.drivers_in(cell)
                      if d.status == "available" and d.product_ok(product)
                      and d.updated_at > now() - 10]                   # ignore stale pings
        if len(found) >= want:
            break
    etas = routing.eta_many([d.position for d in found], pickup)   # road-network ETA
    return sorted(zip(etas, found), key=lambda t: t[0])[:want]

Trip state machine

requested matching assigned arrived in progress completed cancelled reachable from requested / matching / assigned / arrived (with fees per policy)
Every transition is a conditional, versioned write; events go out via the outbox.

Real-time tracking for riders

Driver pings flow through Kafka → a trip-tracking consumer → pushed to the rider's connection (WebSocket gateway lookup as in chat). Interpolate on the client between pings for smooth movement; snap to roads.

ETA

A routing engine (contraction hierarchies over the road graph, with live traffic weights derived from GPS traces) computes ETAs; ML models correct the residual error. Cache common origin/destination cell pairs.

Payments

Authorise the estimated fare at request time (hold), capture the final fare after the trip; handle failures asynchronously; driver payouts batched. See payment system.

6. Trade-offs

DecisionChoice
Location storagelatest in memory (TTL), history async to Cassandra/TSDB
Partitioningby city/region → independent failure domains (cells)
Matchinggreedy nearest-ETA vs batched global optimisation
Consistencystrong for assignment/trip state; eventual for tracking, surge
IndexH3 hexagons (uniform neighbours) vs geohash (simpler)

Interview questions

0 / 5 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.