Low-Level Design: Fleet Management System — Vehicle Tracking, Driver Assignment, and Route Optimization

Core Entities

Vehicle: vehicle_id, license_plate, type (SEDAN, VAN, TRUCK), capacity_kg, fuel_type, status (AVAILABLE, ON_TRIP, MAINTENANCE, OFFLINE), current_location (lat, lng), odometer_km, last_service_date. Driver: driver_id, name, license_number, phone, status (AVAILABLE, ON_TRIP, OFFLINE), current_vehicle_id, rating, total_trips. Trip: trip_id, vehicle_id, driver_id, origin, destination, status (SCHEDULED, IN_PROGRESS, COMPLETED, CANCELLED), scheduled_at, started_at, completed_at, distance_km, cargo_weight_kg. GPSEvent: event_id, vehicle_id, timestamp, lat, lng, speed_kmh, heading. MaintenanceRecord: record_id, vehicle_id, type, description, cost, performed_at, next_service_at.

Real-Time Vehicle Tracking

GPS devices in vehicles send location events every 5-30 seconds. Ingestion: events stream via MQTT (lightweight IoT protocol) or WebSocket to an ingestion service. The ingestion service publishes to Kafka (partitioned by vehicle_id for ordering). A stream processor consumes and: (1) updates the vehicle’s current location in Redis (HSET vehicle:{id} lat {lat} lng {lng} timestamp {ts} speed {speed}). (2) Appends to the GPS time-series database (InfluxDB or TimescaleDB) for trip history and analytics. The operations dashboard reads current locations from Redis for real-time display — fast O(1) reads. Historical trip replay reads from the time-series DB. Redis expiry: if a vehicle sends no update for 5 minutes, mark it OFFLINE.

Driver and Vehicle Assignment

When a trip is requested: find the best available vehicle and driver. Vehicle matching criteria: type matches cargo requirements, weight capacity >= cargo_weight, fuel level sufficient for the trip distance. Driver matching: AVAILABLE status, assigned to a vehicle of the right type, highest rating, lowest current trip count (for load balancing). Geospatial matching: among candidates, prioritize the closest vehicle to the pickup location. Use a geospatial index (PostGIS or Redis GEOSEARCH) to find vehicles within a radius. GEOSEARCH fleet_vehicles FROMLONLAT {lng} {lat} BYRADIUS 20 km ASC COUNT 10 gives the 10 nearest vehicles sorted by distance. Score candidates by: proximity weight * (1/distance) + rating weight * rating + availability weight * (1 if AVAILABLE else 0).

Route Optimization

Single trip routing: use a routing engine (OSRM, Valhalla) with the road network graph. Input: origin + destination + vehicle type (trucks have height/weight restrictions). Output: optimal route with turn-by-turn directions, estimated distance and duration. Multi-stop optimization (vehicle routing problem, VRP): assign N deliveries to M vehicles to minimize total distance. This is NP-hard. Practical approach: nearest-neighbor heuristic for initial assignment, then 2-opt local search improvement. For real-time re-routing: monitor traffic data; if a delay is detected on the current route, re-calculate and push updated directions to the driver’s device via WebSocket.

Maintenance Scheduling

Proactive maintenance based on: odometer (every 10,000 km), time (every 6 months), engine hours, or fault codes from the vehicle’s OBD-II interface. On each GPS event update, check if odometer_km >= last_service_km + 10000. If yes: create a MaintenanceRecord with status=SCHEDULED, notify the fleet manager, and set vehicle status=MAINTENANCE_DUE. A vehicle on MAINTENANCE_DUE can still run trips but is flagged. When maintenance is completed: update performed_at, cost, next_service_at, reset the odometer threshold, set status=AVAILABLE. Predictive maintenance: stream engine telemetry (temperature, RPM, fault codes) to an ML model that predicts component failures before they occur.

Analytics and Reporting

Key metrics: fleet utilization (% of time vehicles are on trips vs idle), average trip duration and distance by vehicle type, fuel efficiency (km/liter) by vehicle, driver performance (on-time rate, safety score from speed/braking telemetry), maintenance cost per vehicle. Aggregate GPS speed data to detect harsh braking (speed drops > 20 km/h in 1 second) and speeding (speed > road limit). Score drivers on safety daily. Reports: daily fleet summary emailed to fleet managers, monthly cost-per-km by vehicle for financial planning, anomaly alerts (vehicle exceeds geofence, driver inactive for > 30 minutes during a trip).

Asked at: Uber Interview Guide

Asked at: Lyft Interview Guide

Asked at: DoorDash Interview Guide

Asked at: Airbnb Interview Guide

Scroll to Top