Design Ride Matching (Uber, Ola, Rapido)
Track millions of moving drivers, find nearby ones with geospatial indexing, match riders reliably and manage the trip lifecycle.
Advanced · about 5 hours · difficulty 4/5
Learn first: Redis in Practice, Event Streaming with Kafka, Sharding and Partitioning
Ride hailing combines high-frequency location writes, geospatial queries and a matching process that must never assign one driver to two riders.
Requirements
- Drivers send location every few seconds; riders see nearby cabs.
- A rider requests a ride; the system finds and offers it to suitable nearby drivers; one accepts.
- Trip lifecycle: requested, driver assigned, arriving, in progress, completed, paid; plus cancellations.
- ETA and pricing (including surge), live tracking for the rider.
Location ingestion
With 10 lakh active drivers sending updates every 4 seconds, that is about 2.5 lakh writes per second. Keep current location in memory (Redis or an in-memory location service), not a disk database; stream updates through Kafka for history, analytics and ETA models.
Geospatial indexing
You need "drivers within 2 km of this point" fast:
- Geohash: encode lat/long into a string; nearby points share prefixes. Query the cell and its 8 neighbours.
- Quadtree: recursively split dense areas; good for uneven density (Bengaluru vs rural areas).
- H3 (Uber's hexagonal grid) or S2 (Google): cells with uniform neighbours, used widely in production.
Redis GEO commands work well for a prototype. Shard the location index by city or region, since matching is local.
Matching
- Find candidate drivers in nearby cells, filter by vehicle type and availability, rank by ETA (road distance, not straight line).
- Offer to the best driver with a short timeout; if declined or timed out, offer to the next (or offer to a few in parallel).
- Make assignment atomic: a driver's status changes from AVAILABLE to ASSIGNED with a conditional update so two riders cannot get the same driver.
A trip service owns the trip state machine; a dispatch service runs matching per city.
Other concerns
Surge pricing from demand/supply ratios per cell, live tracking pushed over WebSockets, payments after the trip, and handling flaky driver connectivity.
Pitfalls
- Writing every location ping to a relational database.
- Straight-line distance for ETA in cities with rivers and one-way roads.
- Race conditions that double-assign drivers.
You are ready to move on when you can explain how a rider's request finds the nearest available driver in under a second and why a geospatial index is needed.
Key concepts
- High-frequency location ingestion
- Geohash
- Quadtree
- H3 and S2 cells
- Driver offer and timeout flow
- Atomic driver assignment
- Trip state machine
- Surge pricing
Mini project
Simulate 10,000 drivers moving around a city map and sending locations to Redis GEO every few seconds. Build a request endpoint that finds the 5 nearest available drivers, offers sequentially with timeouts, and assigns atomically. Report p99 matching time.
Check yourself
Why is a geohash or H3 cell lookup much faster than computing the distance from the rider to every driver, and what edge case do neighbouring cells solve?
Free resources
- H3 hexagonal grid system (Uber)
- Uber engineering blog (Uber)
- Geohash (Wikipedia)
- Redis documentation (geospatial) (Redis)