2
Dispatch-Engine
Jonathan Miller edited this page 2026-06-27 19:47:07 +00:00

Dispatch Engine

The dispatch engine matches ride requests with available drivers using proximity-based search with geographic optimization.

Algorithm Overview

  1. Bounding Box Pre-filter -- SQL WHERE clause limits candidate drivers to a geographic rectangle, using index-friendly comparisons on lat/lng columns
  2. Haversine Distance -- Precise great-circle distance calculation for candidates within the bounding box
  3. Sort & Offer -- Drivers sorted by distance (nearest first), ride offered sequentially

Bounding Box Optimization

Instead of calculating haversine distance for every driver in the database, the dispatch engine first applies a bounding box filter:

$latDelta = $radiusKm / 111.0;
$lngDelta = $radiusKm / (111.0 * cos(deg2rad($pickupLat)));

$query->where($db->quoteName('s.current_lat') . ' BETWEEN :latMin AND :latMax')
      ->where($db->quoteName('s.current_lng') . ' BETWEEN :lngMin AND :lngMax');

This reduces the candidate set from potentially thousands of drivers to only those within the approximate radius, making the haversine calculation feasible even with large driver fleets.

Configuration

Parameter Source Default
Max Dispatch Radius Plugin params 10 km
Dispatch Timeout Plugin params 60 seconds
Max Dispatch Attempts Plugin params 5

Dispatch Record

Each offer creates a dispatch record tracking:

  • Driver offered
  • Attempt number
  • Distance to pickup
  • ETA estimate
  • Response (accepted/rejected/expired)
  • Timestamps (offered, responded)

Re-dispatch Flow

When a driver rejects or times out:

  1. Dispatch record updated to rejected or expired
  2. Next nearest driver selected (excluding already-offered drivers)
  3. New dispatch record created with incremented attempt number
  4. Process repeats until a driver accepts or max attempts reached

No Driver Available

If all dispatch attempts are exhausted:

  1. Ride status set to no_driver
  2. Rider notified
  3. Rider may retry (creates fresh dispatch cycle)

Race Condition Prevention

Driver acceptance uses FOR UPDATE locking to prevent a driver from being assigned to multiple rides simultaneously. When two rides try to claim the same driver, only one will succeed.