Quick revision
The short answer
Drivers get double offers and double trips because two dispatch workers read the same free driver from the location index and both act on it; making the offer an atomic state change on the driver, FREE to OFFERED with the trip id, through a conditional update or compare-and-set, ensures only one wins. Offers get a short timeout backed by an expiring reservation, and on decline or timeout dispatch moves straight to the next ranked candidate. Candidates are ranked by ETA from a routing engine rather than straight-line distance, peak matching runs in short batches solved as an assignment, and dispatch is partitioned by city or cell so one dispatcher owns each area.
What to remember
In this answer, we saw that double offers and double trips come from a check-then-act race between dispatch workers, fixed by making the offer an atomic, expiring reservation on the driver's state. Short offer timeouts and moving down a ranked list stop declines from stalling riders, ranking by ETA from a routing engine replaces misleading straight-line distance, batch matching at peak lowers total wait time, and partitioning dispatch by city or cell gives each area one owner, removing cross-worker races.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Drivers get double offers and double trips because two dispatch workers read the same free driver from the location index and both act on it; making the offer an atomic state change on the driver, FREE to OFFERED with the trip id, through a conditional update or compare-and-set, ensures only one wins. Offers get a short timeout backed by an expiring reservation, and on decline or timeout dispatch moves straight to the next ranked candidate. Candidates are ranked by ETA from a routing engine rather than straight-line distance, peak matching runs in short batches solved as an assignment, and dispatch is partitioned by city or cell so one dispatcher owns each area.
Why this question is asked
Dispatch is the heart of a ride-hailing service, and its bugs are visible to everyone: drivers juggling two offers, riders watching the spinner for 40 seconds, and a car sent across a river because it looked close on a map. Interviewers ask this question because it combines a classic race condition with timeouts and a real optimisation problem. In this answer, we will find the race behind the double offers, fix it with atomic reservations, stop declines from stalling riders, switch ranking from distance to travel time, match in batches at peak, and partition dispatch so races cannot happen in the first place.
1A Quick Recap of the Current Dispatch
Today each ride request is handled by whichever dispatch worker picks it up. The worker queries the location index for the nearest drivers marked free, picks the closest one, and sends that driver an offer. If the driver declines, the worker searches again. At evening peak, many requests arrive per second in the same busy areas, handled by many workers in parallel.
2Finding the Race Behind Double Offers
Two riders request trips 100 metres apart within the same second. Worker A and worker B both query the index and both see driver d_17 as the nearest free driver. Both send an offer. The driver receives two offers within 300 ms. If the driver accepts both, or accepts one while the other is auto-accepted, or the second worker marks the driver busy without checking, the driver ends up on two trips.
This is a check-then-act race: each worker checked that the driver was free, then acted, but the check and the act were not one atomic step, so another worker acted in between. The location index is especially prone to this, because it is updated asynchronously and its free flag may be seconds old.
8 messages in order. Play them, or step through them one at a time.
Both workers checked, then acted; nothing made the check and the act one step.
3Fix 1: Make the Offer an Atomic Reservation
The fix is to turn sending an offer into an atomic change of the driver's dispatch state, from FREE to OFFERED with the trip id, that only one worker can win. It can be a conditional update in a database or a compare-and-set in Redis. Whoever wins sends the offer; whoever loses moves on to its next candidate.
Notice the expiry. The reservation lasts 15 seconds. If the driver accepts, the state moves to ASSIGNED; if they decline or do not answer, the reservation expires and the driver is FREE again. A crashed worker cannot leave a driver stuck, because the expiry releases them anyway.
4Fix 2: Do Not Let Declines Stall the Rider
Riders waited 40 seconds because each decline sent the worker back to the beginning: a new search, a new pick, a new offer, with no time limit on how long the driver could think. Instead, each offer has a short timeout, 10 to 15 seconds, enforced by the expiring reservation. The worker keeps the ranked list of candidates from the first search; on decline or timeout it releases the driver and offers the next candidate on the list immediately, only searching again if the list runs out or becomes stale.
Some services go further and offer the trip to the next candidate while the first is still thinking, or send offers to two drivers with first acceptance winning. That cuts wait time at the cost of more declined offers, and it only works if the assignment itself is atomic, as in fix 1.
5Fix 3: Rank by Travel Time, Not Distance
Straight-line distance is a poor guide in a city. A driver 800 metres away across a river may need 12 minutes to reach the pickup by the nearest bridge, while one 2 kilometres away on the same road arrives in 4. Ranking by distance sends the wrong car, which raises wait time and makes drivers decline.
So candidates are ranked by ETA, the estimated time of arrival, computed by a routing engine over the road graph with current traffic, such as an in-house engine or an open-source one like OSRM, Valhalla or GraphHopper. Because routing is more expensive than a distance check, we first take the nearest 10 to 20 drivers by distance as a shortlist, then compute ETAs only for them.
| Driver | Straight-line distance | ETA by road | Ranked first by |
|---|---|---|---|
| d_17 | 800 m, across the river | 12 min | Distance |
| d_42 | 2.0 km, same side | 4 min | ETA |
6Fix 4: Batch Matching at Peak
Greedy matching, giving each rider the best driver at the moment of request, works when drivers are plentiful. At peak it makes poor choices: the first rider takes the driver who was the only good option for the second rider. Collecting requests for one to two seconds and solving an assignment across all of them, for example with the Hungarian algorithm or a min-cost flow on a matrix of ETAs, minimises total wait time. The cost is up to two seconds of extra delay per request, which riders do not notice, against minutes saved overall.
7Fix 5: Partition Dispatch by Area
The deepest fix is to stop many workers from competing for the same drivers at all. Dispatch is partitioned by city, or by geographic cells within large cities, and each partition has one owner process that sees all requests and drivers in its area. Batching then happens naturally, the owner can keep driver state in memory, and cross-worker races disappear. The owner writes its decisions to a durable log, such as Kafka or a database, so a replacement can recover the state if it fails. Drivers near a cell border can be visible to both neighbours, with the atomic reservation from fix 1 still guarding the edge cases.
8Follow-up questions
What happens to an outstanding offer when the dispatcher for that city restarts?
The offer is recorded durably with its expiry, in the reservation store or the dispatcher's log. The new dispatcher reads the outstanding offers on start. If the driver's acceptance arrives during the restart, it goes to the trip service, which applies it with a conditional update only if the offer is still valid. If no answer arrives before the expiry, the reservation lapses, the driver becomes free, and the new dispatcher moves the trip to the next candidate. Nothing is lost and no driver is left stuck.
Why not lock the driver row with SELECT FOR UPDATE during the whole offer?
Because the offer lasts up to 15 seconds and depends on a human tapping a button. Holding a database lock that long blocks other work and ties up connections. A short atomic state change with an expiry gives the same exclusivity without holding a lock.
How do you keep ETA calls affordable?
Shortlist by distance first, compute ETAs only for the top candidates, and cache ETAs between grid cells for a short time bucket, since traffic changes slowly over minutes.
9Conclusion
In this answer, we saw that double offers and double trips come from a check-then-act race between dispatch workers, fixed by making the offer an atomic, expiring reservation on the driver's state. Short offer timeouts and moving down a ranked list stop declines from stalling riders, ranking by ETA from a routing engine replaces misleading straight-line distance, batch matching at peak lowers total wait time, and partitioning dispatch by city or cell gives each area one owner, removing cross-worker races.
Open with
The double offers are a check-then-act race: two dispatch workers query the location index, both see the same driver as free and both send an offer. I would make the offer an atomic state change on the driver, FREE to OFFERED with the trip id, using a conditional update or a compare-and-set like SET NX with an expiry in Redis, so only one worker wins and the other moves to its next candidate.
Then, if they want more
The reservation expires after 10 to 15 seconds, so a decline, a timeout or a crashed worker releases the driver automatically, and on decline we go straight to the next ranked candidate rather than searching again, which removes the 40-second waits.
For ranking, I would use ETA from a routing engine over the road graph, not straight-line distance, because a driver 800 metres away across a river can be 12 minutes out; to keep it affordable, shortlist the nearest few by distance and compute ETAs for those.
At peak, match in batches of one to two seconds and solve an assignment across all riders and drivers, which lowers total wait time compared with greedy picks. Finally, partition dispatch by city or cell so one dispatcher owns each area, which removes cross-worker races and lets state live in memory with a durable log for recovery.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How would you redesign dispatch so a driver is never assigned two trips, declined offers...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/3c4a53619c/