SD-40 · Back to the write-up
Design a Ride and Courier Matching
Stream driver GPS into an in-memory index by map cell, match requests in 2-second batches on road ETAs, and lock each driver before the offer goes out.
Follow a request
A GPS update
Request and match
Driver accepts
Offer times out
Nearby cars on the map
Phones
Edge
Services
Data
Rider appRequests a ride
Driver appGPS every 4 s
API gatewayAuth + limits
Connection serversOne socket per driver
Trip serviceState machine
MatchingBatches per city
Location streamBy city
Trip DBSharded by trip
ETA serviceRoad graph
Driver locks15 s leases
Geo index16 shards by city
The numbers, at a glance
1 request ≈ 2,000/s125 GPS points ≈ 250,000/s
1,2505K
12,50050K
50,000200K
125,000500K
250,0001M
200 MB of live positions64 GB node
A request, in order
The happy path. On a decline or a 15-second timeout, steps 5 to 8 repeat with the next driver.
Rider
Trip service
Matching
Geo index
Driver locks
Driver
1 POST /trips
2 202 matching
3 open request
4 free drivers in 20 cells
5 10 candidates
6 CAS free → offered, 15 s
7 reserved
8 offer (socket)
9 accept
10 assign if still matching
11 driver + ETA
RequestResponseAsync, does not block
How to pick the driver
The decision this question is really about.
request
closest point on the map
6.1 min pickup
+ Instant and simple.− Ignores roads; first rider wins.
request
10 candidatesETA each
fastest wins
+ Respects rivers and traffic.− Still greedy, one rider at a time.
requests × drivers
ETA tableassign all at once
4.2 min pickup
+ Best for the whole city.− Up to 2 s before the first offer.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
Apps
App server
SQL database
Apps
API gatewayConnection servers
Trip + match service
Trip DBGeo index
Apps
API gatewayConnection servers
Trip + match serviceLocation stream
Trip DBGeo index × 16Driver locks
Apps
API gatewayConnection servers
Trip serviceMatchingLocation stream
Trip DBGeo index × 16Driver locksETA service
Was this walkthrough useful?Your vote tells us what to improve next.