Courses System design · Part 6 · Case study 7
Case Study: Ride Sharing, Finding Drivers Near You
A rider opens the app in Tehran and asks for a car. Within seconds the app must find drivers nearby, offer the ride to one of them, and show the car coming closer on the map. Two parts make this hard: drivers move all the time, and two riders must never get the same driver.
1. Requirements
Functional:
- Drivers who are online send their location every few seconds.
- A rider asks for a ride; the system finds nearby free drivers and offers the ride to one at a time.
- Once a driver accepts, both see each other on the map until the trip ends.
Non-functional:
- Finding a driver should take a few seconds, even at rush hour.
- A driver is never given two rides at once; a ride never goes to two drivers.
- Losing a few location updates is fine; losing a trip or a payment is not.
2. Numbers
- At peak, 100,000 drivers online in a big city, each sending a location every 4 seconds: 100,000 ÷ 4 = 25,000 updates a second.
- One million trips a day: about 12 requests a second on average, maybe 60 a second at peak.
- One location is about 100 bytes (driver ID, latitude, longitude, time, status): 100,000 drivers × 100 bytes = 10 MB. All current locations fit in the memory of one machine.
The result: the location stream is write-heavy but small, and only the current location matters for matching. Keep it in memory, not as 25,000 database writes a second. Old points for trip history can go to a cheap, write-friendly store later (storage engines).
3. Components
- Location service: receives driver updates and keeps the latest location of each driver in memory.
- Matching service: finds nearby free drivers for a request and offers the ride.
- Trip service: the trip and its states, in a relational database.
- Notifications: offers to drivers, updates to riders (the notification system case study).
4. Deep dive: finding drivers near you
The simple way, “check the distance to every online driver”, means 100,000 distance checks for every request. Too slow at 60 requests a second, and it gets worse as the city grows.
The fix is to cut the map into cells. A common way is the geohash: it turns a latitude and longitude into a short string, and nearby points share a prefix. A geohash of 6 characters is a cell of about 1.2 × 0.6 km; 5 characters is about 5 × 5 km.
- When a driver’s location arrives, store it under its cell:
cell → set of drivers. - For a request, look in the rider’s cell and the 8 cells around it (the rider may be near an edge). That is a few dozen drivers, not 100,000.
- If there are too few, widen the search to the 5-character cells.
Redis does this out of the box (GEOADD, GEOSEARCH). For a whole country, split the cells over several machines, for example by consistent hashing on the cell prefix, so each machine owns some areas.
5. Deep dive: one ride, one driver
The matching service sorts the nearby free drivers by distance (or by road time) and offers the ride to one driver, with a timeout of about 10 seconds. No answer or a “no”: offer it to the next.
The danger is two riders close to each other: both requests see the same free driver, and both offer that driver a ride. This is exactly the last ticket problem, with a driver instead of a ticket. The fix is the same: claim the driver in one atomic step, for example
UPDATE drivers SET status = 'offered', trip = ? WHERE id = ? AND status = 'free'
or the same as a single command in Redis. If no row changed, someone else was faster: move to the next driver. Never “read free, then write offered” in two steps.
Trip states
A trip moves through states: requested → driver offered → accepted → driver arrived → in progress → finished, or cancelled. Each change is checked against the current state, written to the database in one transaction, and published as an event on a queue for billing, notifications and the map.
When things go wrong
- A driver’s app goes silent (a tunnel, a dead battery): after a few missed updates, mark the driver offline so no one is offered to them.
- A driver accepts, but the answer is lost: the app retries with an idempotency key, so the second “accept” does not create a second trip.
- Rush hour after a football match: rate-limit the request button per rider (rate limiter) and let the price rise with demand (surge pricing), so more drivers come online.
6. Trade-offs
- Fresh or cheap: updates every 4 seconds are 25,000 writes a second; every 10 seconds cuts that to 10,000, but the car on the map jumps.
- Nearest or fairest: offering to the nearest driver is fast for the rider; spreading rides between drivers is fairer to them.
- Straight distance or road time: straight distance is cheap; real road time needs a routing service but gives better matches.
Labs used
Last ticket · Consistent hashing · Message queue · Rate limiter · Cache