Location service → Drivers nearby: latest position
Location service → Trip events: position log
API gateway → Dispatch: find a driver
Dispatch → Drivers nearby: who is near?
Dispatch → Routes and ETA: ETA
Dispatch → Pricing: fare
Dispatch → Live updates: offer to driver
API gateway → Trips: trip
Trips → Trips DB: SQL
Trips → Trip events: trip changed
Trip events → Location history: archive
Trips → Payments: charge at the end
Trips → Notifications: driver is here
Notes and decisions
API gateway: Auth, rate limits, routing
Live updates: Holds a connection per driver and rider
Location service: Takes a position every 4 seconds per driver
Drivers nearby: Only the latest position
Dispatch: Offers a ride to the closest drivers, one at a time
Trips: The state of a ride
Pricing: Fare and surge per area
Trips DB: Sharded by city
Why not write every position to Postgres? Millions of writes a minute for data that is stale in 4 seconds. Keep the latest in Redis, the history in the stream.
How do we stop two drivers getting the same ride? Lock the driver in dispatch with a short TTL.
Follow-ups an interviewer asks: surge pricing, a city outage, a driver who goes offline mid-offer.
Inside dispatch
Parts
Ride requestedDomain event
Find drivers in the areaProcess
Anyone free?Decision
Offer to the closest, for 15 secondsProcess
Accepted?Decision
Lock the driver, create the tripProcess
Widen the areaProcess
No drivers: tell the riderStart / end
On the wayStart / end
How they connect
Anyone free? → Offer to the closest, for 15 seconds: yes
Anyone free? → Widen the area: no
Widen the area → No drivers: tell the rider: third try
Accepted? → Lock the driver, create the trip: yes
Accepted? → Find drivers in the area: no, next driver