MSD-101 ยท Deep dives: hard problems, solved
Matching the nearest driver
โฑ 12 min ๐ฎ Hands-on โ Azure โ Completed
โก The 30-second answer
Matching finds free drivers near the pickup with a geo search, ranks them by drive time (not straight-line distance), reserves the best one atomically so no other rider can take them, and offers the trip with a short timeout before moving to the next driver. If nobody is near, the search radius expands.
- Geo search in milliseconds
- Rank by drive time
- Atomic reservation
- Timeouts and expanding radius
Try it
Play with the design before you read about it. Everything runs on this page.
No Azure subscription needed. Read the requirement, see the architecture, build it in a mock portal, send a booking through it, then break it.
โ Simulate on Azure: Matching the nearest driver
Priya is at a hotel by the river. Three free drivers are close in a straight line, but one is across the water. Two other riders nearby are booking at the same second.
It must
- Find free drivers near the pickup
- Rank them by real drive time
- Offer the trip to one driver at a time
- Never give one driver to two riders
With these numbers
- Match time
- p95 < 10 s
- Search
- < 5 ms
- Offer timeout
- 15 s
- Double assignment
- 0
Each box is one Azure service. Tap a box (or Tab to it) to see its job and why it is there.
Create each resource. The defaults are a good design; change a setting and read why it matters. Some choices decide what breaks later.
Home โบ Create a resource โบ Azure Managed Redis
Create Azure Managed Redis
โ Silent drivers must leave the index on their own.
Deployment in progressโฆ
Microsoft.ManagedRedis ยท resource group rg-matching
Home โบ Create a resource โบ Azure Container Apps
Create Container App: matching-service
โ The closest driver in a straight line can be 15 minutes away by road.
โ Only one matcher can hold a driver at a time.
โ Cities need small radii; suburbs at 3 am need bigger ones.
โ Long timeouts make riders wait for drivers who are not looking.
Deployment in progressโฆ
Microsoft.ContainerApps ยท resource group rg-matching
Everything is deployed. Your architecture is lit up below. Go to Run to send a request through it.
Request: Priya books from the riverside hotel
โ ๏ธ Azure Maps route matrix calls are billed per transaction. Use small test matrices.
- Load 1,000 fake drivers into Redis with GEOADD and a 30 s expiry.
- Search with GEOSEARCH, then call the Azure Maps route matrix for the top 10.
- Reserve the winner with SET key value NX EX 15.
- Delete the resource group when done.
* Illustrative prices to compare designs, not a quote.
What breaks, and the fix
| Failure | User sees | Why | Fix |
|---|---|---|---|
| ๐ Closest driver is across the river | A 14-minute wait for a 'nearby' car | Straight-line ranking | Rank by drive time from Azure Maps |
| โ๏ธ Two riders matched at the same moment | Driver assigned, then taken away | Two matchers chose the same driver | Atomic reservation (Redis SET NX with expiry) |
| ๐ Driver ignores the offer | Long waits | Long offer timeout | 15 s offers, then the next driver |
| ๐ Quiet suburb at 3 am | 'No cars' when cars exist further away | Fixed small radius | Expanding radius |
| ๐ Driver apps killed by the OS | Timeouts on dead phones | No expiry | 30 s expiry on positions |
๐ Education-only simulation. Nothing is created in Azure and no subscription is needed.
Stream vs command
Live data can lose a point. Commands must happen exactly once. Give them different roads.
GPS points ride the WebSocket every 4 s. One lost? The next one replaces it.
Idempotency key
Send the same ID on every retry. The server returns the first result instead of doing it again.
Book with key k7. The trip is savedโฆ but the reply is lost in the lift.
Outbox + backoff
Save the command on the phone first, then send it. Wait longer after every failure.
The tap is saved to the local outbox. Even if the app dies now, it is not lost.
Geo index + expiry
Keep only the latest position per driver in memory, and let it expire after 30 s.
Every ping overwrites the driverโs last position and resets a 30 s timer.
Pub/sub fan-out
Publish one event to a group. Everyone in the group gets it, however many they are.
The trip service publishes once: โcar movedโ.
Push to wake, then re-sync
A push only says โsomething changedโ. The app then asks the server for the truth.
The app is in the background, so its socket is closed. The server sends a push.
- The problemTwo riders at the same hotel book at the same second. Both phones say โDriver found: Aliโ. Ali accepts once. One rider gets a car; the other gets a cancellation two minutes later.
- The design ideaSearch fast, rank by road, reserve atomically, offer briefly.
- In real lifeLarge ride platforms also batch requests for a second or two and solve the assignment for many riders at once. Batch matching improves total wait time at the cost of a tiny delay.
Start with a question
Two riders at the same hotel book at the same second. Both phones say โDriver found: Aliโ. Ali accepts once. One rider gets a car; the other gets a cancellation two minutes later.
Matching is a race. The design must decide who wins, every time.
Explain it like I'm 10
At a sports day, two teachers both point at the same fast runner. The runner can only run for one team. The first teacher to put a sticker on the runner gets them; the other picks someone else.
The technical explanation
Find. Free drivers live in an Azure Managed Redis geo index (latest point, 30 s expiry). GEOSEARCH returns about 10 candidates within 3 km in a few milliseconds. In a quiet area the radius grows 1 โ 3 โ 10 km.
Rank. Ask Azure Maps for a drive-time matrix from the candidates to the pickup. A driver 900 m away across a river can be 14 minutes away by road.
Reserve. Before offering, reserve the winner with SET driver:{id}:lock {tripId} NX EX 15. Only one matcher can set it; the other moves to the next candidate. The lock expires by itself.
Offer. Send the offer over the driverโs WebSocket (Azure Web PubSub) with a 15 s timeout. Decline or silence โ release the lock and offer the next driver. The trip service, not matching, saves the final assignment.
| Step | Tool | Why |
|---|---|---|
| Find | Redis GEOSEARCH | Milliseconds, fresh positions |
| Rank | Azure Maps route matrix | Real drive time |
| Reserve | Redis SET NX EX 15 | One driver, one rider |
| Offer | Web PubSub, 15 s timeout | Fast, then move on |
| Widen | 1 โ 3 โ 10 km | Quiet areas still get cars |
Real-world scenario
Large ride platforms also batch requests for a second or two and solve the assignment for many riders at once. Batch matching improves total wait time at the cost of a tiny delay.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
Next: The trip state machine โQuestions people ask
How do ride apps decide which driver gets a trip?
They search free drivers near the pickup, rank them by estimated drive time (often also rating and acceptance rate), reserve the best one so no other rider can take them, and offer the trip with a short timeout.
Should matching run on the phone?
No. Only the server sees every driver and every rider, so only the server can resolve races fairly.
Is the nearest driver always the best match?
No. The nearest driver in a straight line can be across a river or facing the wrong way on a one-way street. Rank by drive time, and many teams also weigh acceptance rate and how long the driver has waited.
Interview questions
How this topic comes up in system design interviews. Answer out loud first, then open the model answer.
intermediate Design driver matching for one city.
Redis geo index of free drivers (expiry 30 s) โ GEOSEARCH 3 km โ Azure Maps route matrix โ reserve with SET NX EX 15 โ offer over WebSocket with a 15 s timeout โ next driver on decline; widen radius when empty; trip service records the assignment.
advanced When would you batch matching?
At high demand: collect requests for 1โ2 seconds, then assign many riders to many drivers at once to minimise total wait. Trade-off: a small added delay for better global results.
Quick revision
Remember
- Geo search in milliseconds
- Rank by drive time
- Atomic reservation
- Timeouts and expanding radius
- Keep reservation locks short and always set an expiry. A lock without an expiry turns one crashed matcher into a driver nobody can book.
One line to remember
โSearch fast, rank by road, reserve atomically, offer briefly.โ
Official documentation
- Azure Maps routing best practices (Microsoft Learn)
- Azure Managed Redis (Microsoft Learn)