Lesson 11 of 14
โฎPrev Nextโฏ

MSD-101 ยท Deep dives: hard problems, solved

Matching the nearest driver

โฑ 12 min ๐ŸŽฎ Hands-on โ˜ Azure

11of 14

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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

    ๐ŸŽ“ Education-only simulation. Nothing is created in Azure and no subscription is needed.

    1. 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.
    2. The design ideaSearch fast, rank by road, reserve atomically, offer briefly.
    3. 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.

    StepToolWhy
    FindRedis GEOSEARCHMilliseconds, fresh positions
    RankAzure Maps route matrixReal drive time
    ReserveRedis SET NX EX 15One driver, one rider
    OfferWeb PubSub, 15 s timeoutFast, then move on
    Widen1 โ†’ 3 โ†’ 10 kmQuiet areas still get cars

    โ˜ Simulate on Azure๐Ÿงฉ Patterns

    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.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Two matchers choose the same driver. What prevents a double assignment?
    2. 2. Why rank by Azure Maps drive time?
    3. 3. A driver ignores the offer. What should happen?
    4. 4. No drivers within 1 km at 3 am. What should matching do?
    5. 5. Why must every lock have an expiry?

    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