System Design: Design Uber Ride Sharing
On this page
- The problem: design a ride-sharing service
- Estimating location update traffic
- Tracking driver locations
- Finding nearby drivers: geohash, quadtrees and H3
- Matching riders and drivers without double-booking
- Real-time updates and the trip state machine
- Handling failures and GPS noise
- What a strong ride-sharing design includes
- Check yourself
The problem: design a ride-sharing service
Riders request a ride; the system finds a nearby available driver, the driver accepts, both see each other move on a map, the trip happens, and the rider pays. Hundreds of thousands of active drivers across many cities. Matching should take seconds, and a driver must never be assigned to two riders at once.
Estimating location update traffic
Active drivers 300,000
Location updates every 4 seconds ≈ 75,000 writes per second
Ride requests ~1,000 per second at peakLocation updates dominate: a constant, high-rate stream of tiny writes where only the latest value matters. Ride requests are fewer but need strong correctness.
Tracking driver locations
Drivers' apps send (driver_id, lat, lon, status, timestamp) every few seconds. Writing each to a relational database would be wasteful; nobody needs the location from four seconds ago for matching. Instead:
- An ingestion service keeps the latest location per driver in memory, in Redis or an in-memory index service, sharded by city or region.
- The raw stream also flows into Kafka for trip history, ETA models and analytics (Day 43).
Finding nearby drivers: geohash, quadtrees and H3
"Drivers within 2 km of this point" needs a geospatial index:
- Geohash turns a latitude and longitude into a string where nearby points share a prefix; search the rider's cell and its neighbors.
- Quadtrees split dense areas into smaller cells.
- H3, Uber's hexagonal grid system, gives evenly sized cells and neighbors at equal distances.
Redis supports this directly with geo commands (GEOADD, GEOSEARCH), which is enough for many systems. Rank candidates by estimated time of arrival along roads, not straight-line distance.
Matching riders and drivers without double-booking
Two riders near the same driver may request at the same moment. The assignment must be atomic:
- Offer the trip to the best candidate with a short acceptance window.
- On acceptance, change the driver's status from
availabletoassignedonly if it's still available: a conditional update (UPDATE ... WHERE status = 'available'), or an atomic script in the store holding driver state (Day 10). - If the update fails, someone else got the driver; offer the next candidate.
Shard matching by city so each city's matcher handles its own drivers, which keeps state local and contention low.
City sharding still has hot spots. When a stadium empties, thousands of requests arrive from a few cells within minutes, and every matcher instance competes for the same few drivers, so most conditional updates fail and retry. Batch requests per cell over a second or two and assign drivers to riders in one pass, rather than letting every request race for the nearest driver.
Real-time updates and the trip state machine
- Rider and driver apps keep WebSocket connections (Day 17); trip events (
driver_assigned,driver_arriving,trip_started) and driver locations are pushed through them. A shared pub/sub layer delivers events to whichever server holds each user's connection. - The trip lifecycle is a state machine: requested → assigned → arriving → in progress → completed → paid. Store transitions durably; each step is idempotent so retries from flaky mobile networks are safe (Day 65).
- Payment runs after completion as a saga with the payment service (Day 49): authorize at request time, capture the final fare at completion.
Handling failures and GPS noise
- A location shard fails: drivers in that area reconnect to a replica; matching for that city pauses briefly.
- A driver's phone loses signal mid-trip: the trip continues on the server; the app resumes from the latest trip state when it reconnects.
- Region-level: cities are independent, so an outage in one region doesn't affect others.
- GPS is noisy. A raw position can jump across a river or onto a parallel road. Smooth locations and snap them to the road network before using them for ETAs or fares, and keep the raw stream for disputes.
What a strong ride-sharing design includes
- Location updates treated as a high-rate stream with latest-value storage, not database rows.
- A named geospatial indexing approach and ETA-based ranking.
- Atomic assignment that prevents double-booking.
- Real-time updates over persistent connections.
- A durable, idempotent trip state machine and payment saga.
Check yourself
That's 75,000 small writes per second, of which only the latest per driver matters for matching; an in-memory geospatial index is cheaper and faster, with history streamed elsewhere.
Make assignment a conditional, atomic update: set the driver to assigned only if still available; if it fails, try the next candidate.
Riders and drivers only match within the same area, so a city is a natural boundary. Sharding by city keeps each city's location data and matching contention on its own shard, spreads load across shards, and means a problem in one city's shard doesn't affect others.
Many requests race for the same nearby drivers, so most lose and retry. Batch requests in the area and assign drivers in one pass instead of one race per request.