Skip to content
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

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
1rider → gateway · POST /tripsThe rider taps Request, with an Idempotency-Key so a double tap makes one trip.1 / 8

The numbers, at a glance

For every ride request……drivers send about 125 GPS points
1 request ≈ 2,000/s125 GPS points ≈ 250,000/s
Location updates per secondOne point every 4 s per online driver
The live index is tiny200 MB for 1M drivers on a 64 GB node
We shard by city for write rate and blast radius, not for memory.

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.

Nearest by distance
Baseline
request
closest point on the map
6.1 min pickup
+ Instant and simple.− Ignores roads; first rider wins.
Nearest by road ETA
Better
request
10 candidatesETA each
fastest wins
+ Respects rivers and traffic.− Still greedy, one rider at a time.
Batch every 2 s
Our pick
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.

Stage 1 · ≈ 5,000 driversMake it work
Apps
App server
SQL database
One service, positions in a SQL table, drivers poll for offers.
Stage 2 · ≈ 200,000 driversIndex the map
Apps
API gatewayConnection servers
Trip + match service
Trip DBGeo index
Positions in memory by cell; one socket per driver.
Stage 3 · ≈ 1M driversSplit the firehose
Apps
API gatewayConnection servers
Trip + match serviceLocation stream
Trip DBGeo index × 16Driver locks
A stream for GPS, 16 index shards by city, a lease per offer.
Stage 4 · ≈ 1M drivers, better pickupsMatch well
Apps
API gatewayConnection servers
Trip serviceMatchingLocation stream
Trip DBGeo index × 16Driver locksETA service
Batch requests every 2 s per city and assign on road ETAs.
Was this walkthrough useful?Your vote tells us what to improve next.