Backend
Designing Matching and Dispatch Services
How availability, geospatial matching, assignment leases, and timeout recovery interact in production dispatch — including Redis GEO reads and PostgreSQL transactional guarantees. Applies to marketplaces, logistics, and mobility.
SystoBase Engineering · · 16 min
Key takeaways
- Dispatch quality is bounded by availability accuracy before geospatial cleverness.
- Industry pattern: nearest-driver queries only; SystoBase recommendation: nearest eligible driver with atomic lease acquisition.
- Redis GEO supports fast nearby reads; PostgreSQL holds assignment truth and audit history.
- Every offer needs a timeout, a decline path, and a measurable assignment funnel.
- PostGIS helps validate service areas and analyze match quality — not every hot-path read.
- Monitor ghost assignments: offered drivers who were already busy or stale.
Availability comes first
Matching and dispatch services begin with a trustworthy online set. If provider status lags reality — still online after kill-switch, still free while on an active job — matching algorithms cannot compensate. Availability is a state machine synchronized across mobile clients, API, and hot location stores. Ride-sharing and logistics fleets make this vivid; marketplaces face the same availability correctness problem.
Industry pattern: infer availability from last heartbeat alone. SystoBase recommendation: combine explicit provider intent (online/offline/busy), job linkage, and heartbeat TTL. A provider on an active assignment should be excluded atomically, not eventually.
Failure mode: stale availability produces offers to providers who cannot accept, eroding customer trust and inflating cancel rates. Measure the delta between “marked busy” and “last dispatch offer”.
The matching pipeline
Matching transforms a request into an ordered candidate set: capability, payment method, service area, distance, ETA estimate, and product rules (batching, scheduled jobs, priority tiers). Each filter should be explainable in support tooling.
Geospatial search typically starts from the request location. Redis GEO commands like GEORADIUS return nearby provider IDs sorted by distance — fast enough for iterative expansion if the first radius is empty. Keep search radii bounded; unbounded scans do not scale with supply density.
Industry pattern: single batch query, first free driver wins. SystoBase recommendation: iterative radius expansion with a cap on candidates evaluated per request, logging why drivers were skipped (busy, wrong vehicle, low rating threshold).
Assignment leases
Assignment is the concurrency-critical step. Two concurrent requests must not bind the same provider. Implement leases: upon offer, mark the provider as reserved with expiry; acceptance converts the lease to an active job; timeout or decline releases the lease.
PostgreSQL row-level locks or optimistic versioning on availability rows work when matching is centralized. Compare-and-set on a version column in a short transaction is often enough before you need distributed locks.
Industry pattern: retry on unique constraint violation without surfacing rider-facing errors. SystoBase recommendation: structured retry with jitter inside dispatch service, plus rider-visible “searching” state rather than false “driver assigned”.
Redis GEO and PostgreSQL together
Redis holds ephemeral online positions updated every few seconds from mobile location streams. PostgreSQL holds jobs, provider profiles, compliance records, and assignment audit trails. Neither replaces the other.
Matching reads Redis for proximity, then validates eligibility against PostgreSQL — documents, wallet status, active job flags. This two-step pattern adds milliseconds but prevents illegal matches.
When Redis and PostgreSQL disagree (driver shown nearby but marked offline in DB), prefer PostgreSQL for assignment and treat Redis as hint. Reconciliation jobs can flag systematic drift.
Timeouts and recovery
Providers miss offers. Networks drop. Apps background. Every offer needs TTL; expired offers return the provider to the pool and trigger the next candidate or customer notification.
Celery tasks often handle timeout enforcement so the API path stays fast — but those tasks must run on job-critical queues isolated from bulk work. A delayed timeout task leaves providers ghost-reserved.
Recovery paths: re-offer next driver, widen radius, convert to scheduled retry, or cancel with transparent rider messaging. Each path should emit metrics; silent failure is how operations teams burn out.
PostGIS for spatial analytics
PostGIS excels at service area polygons, geofences, heatmaps of unfulfilled demand, and offline analysis of match quality. Use ST_DWithin with GiST indexes for polygon containment checks during request creation.
Industry pattern: hard-code city bounding boxes in application config. SystoBase recommendation: store operational geographies in PostGIS, version them, and validate pickup/dropoff (or start/end locations) against active polygons at request time.
Analytics on historical trips — route deviation, average pickup distance by zone — informs dispatch tuning without touching hot-path Redis reads.
Operational metrics
Track funnel metrics: request → candidates found → offer sent → accepted → arrived → started. Drop-offs between stages localize bugs faster than aggregate cancel rate.
Alert on rising time-to-first-offer, increasing offers per completed job, and lease timeouts exceeding baseline. These often precede user-visible outages.
Dispatch is not finished when it works in staging with ten drivers. Load-test concurrent assignment in the same metro cell; races appear only under overlap.