Design a location-based ride-hailing service
A ride-hailing service matches riders to nearby drivers in real time. The core problem is geospatial: efficiently indexing millions of moving driver locations and answering "nearest available drivers" in milliseconds, then orchestrating the trip lifecycle.
Geo & Location, 11 components, about 45 min
1. Scope
In scope
- • Driver location ingestion
- • Match rider to nearby driver
- • Trip lifecycle
- • ETA + surge pricing
Out of scope
- • Payments deep-dive
- • Maps/routing engine internals
- • Driver onboarding
2. Functional requirements
Must have
- • Drivers report location frequently
- • Match a ride request to the best nearby driver
- • Track trip state
- • Compute ETA
Nice to have
- • Surge pricing
- • Pooling/shared rides
- • Driver heatmaps
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • 5M active drivers at peak
- • Each pings every 4 s
- • 1M ride requests/hour peak
- • World partitioned into geo cells
Derived numbers
5. API design
Driver location ping → upsert into geo index
Request a ride → matcher finds drivers
Trip state + driver ETA
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
Geospatial indexing
Divide the world into cells (geohash prefix or Google S2, ~5 km). Store each driver in their cell's Redis set keyed by position. To find nearby drivers, query the rider's cell plus its 8 neighbors — bounding the search to a small area instead of scanning all drivers. Dense cities use finer cells / quadtrees that split when too crowded.
Handling 1M+ location writes/sec
Driver pings are a firehose. Ingest them into the in-memory geo index (cheap upserts) and optionally stream to Kafka for analytics. Locations are eventual — a slightly stale position is fine for matching.
Matching
On a request, the matcher pulls candidate drivers from the relevant cells, filters by availability, and ranks by ETA (via maps) and rating. It offers the ride to the top candidate; on decline/timeout it moves to the next. The trip transitions requested → matched → in-progress → complete as a persisted state machine.
Surge pricing
A streaming job (Flink) continuously computes supply vs demand per cell. When demand outstrips supply, it raises a surge multiplier for that cell, which the matcher/pricing applies. This is eventual and localized.
9. Bottlenecks
- Location write firehose → in-memory geo index, shard by cell
- Hot cells (downtown) → finer cell granularity
- Match latency → bounded neighbor search, precomputed ETAs
10. Trade-offs
Geo index
Choose: Hexagonal cells (H3) or geohash cells in memory, with drivers bucketed by cell and updated every few seconds.
Instead of: Quadtree: adapts to density, but rebalancing under constant movement is costly.
Fixed cells make updates O(1) and neighbor search a fixed ring of cells.
Location storage
Choose: Latest location in memory (Redis or the matching service); history streamed to Kafka for analytics.
Instead of: Write every ping to a database: ~1M writes/s for data that is stale in 4 s.
Only the latest location matters for matching.
Matching strategy
Choose: Batch matching every ~500 ms per area to optimize total ETA across riders and drivers, short enough to keep the under-1 s match target.
Instead of: Greedy nearest driver per request: faster per request, worse globally.
A small delay buys better matches when demand is dense.
11. Failure modes
Matching service for a city fails
No rides there.
Mitigation: Shard by city or region with replicas; drivers re-report location so state rebuilds in seconds.
Driver gets two simultaneous offers
Double booking.
Mitigation: Offer locks: a driver is reserved with a short TTL while one offer is outstanding.
Phone GPS jitter
Wrong ETAs and pickup spots.
Mitigation: Map matching (snap to roads) and Kalman filtering on the server.
12. What interviewers probe
Answer each one yourself before opening it.
How many location updates per second?Show a strong answer
5M drivers every 4 s ≈ 1.25M/s. Partition by region so each matching node handles its area.
How do you find drivers within 2 km?Show a strong answer
Find the rider's cell, take k rings of neighbors covering 2 km, collect drivers, then filter by road-network ETA, not straight-line distance.
How is the trip state machine stored?Show a strong answer
A strongly consistent store (requested → accepted → arrived → in_progress → completed) with idempotent transitions.
How does surge pricing work technically?Show a strong answer
Per-cell supply and demand counts from the stream, a multiplier computed every minute, locked into the rider's quote.
Sign in to track this guide in your curriculum progress.