When a rider taps "Request", a dispatch system has a few seconds to answer three questions: which drivers are nearby, who gets the offer, and what happens when two of them accept at the same moment.
This post builds a small but realistic dispatch loop with Redis for driver locations and PostgreSQL for trip state. It is not a production system, but every piece maps to something real ride-hailing apps have to get right.
1. Keep a live index of available drivers
Every online driver app sends its position every few seconds. We only need the latest point per driver for matching, so a Redis geo set per vehicle type is enough:
import redis , time r = redis . Redis ( )
def update_location ( driver_id : str , vehicle_type : str , lon : float , lat : float , available : bool ) : key = f "drivers:{vehicle_type}" if available : r . geoadd ( key , ( lon , lat , driver_id ) ) r . hset ( "driver:last_seen" , driver_id , int ( time . time ( ) ) ) else : r .…