KatInterview

Resilient Dispatch and Trip Lifecycle at Global Scale

System DesignModel answer
HardModel answerSystem Designsystem-designgeospatialdistributed-systems

Last updated

Design a real-time ride-hailing backend that ingests driver pings, matches riders without double-booking, and runs an idempotent, fault-tolerant trip state machine.

Design the dispatch core of a worldwide ride-hailing service. The system must continuously absorb driver position updates, search and rank nearby available drivers when a rider requests a ride, prevent two riders from claiming the same driver (or one rider from being claimed twice), and manage the trip through its full lifecycle: REQUESTED, MATCHED, ARRIVED, IN_PROGRESS, COMPLETED, CANCELLED.

Functional requirements

  1. Location ingestion — Drivers report positions periodically; the platform keeps a spatial index that reflects where each active driver is.
  2. Candidate search and matching — On a ride request, find available drivers within a dynamic radius (roughly 3–5 km) and offer the trip to candidates sequentially or in batches.
  3. Conflict-free assignment — A driver may hold at most one outstanding offer/trip, and a ride request may be claimed by at most one driver.
  4. Trip state machine — Transitions must be idempotent, auditable, and safe under duplicate or out-of-order network packets.
  5. Rejection and timeout — If the targeted driver declines or does not answer within roughly 15 seconds, the ride moves on to the next-best candidate.

Non-functional requirements

  • Scale — about 1,000,000 concurrently active drivers reporting every 4 seconds (≈250,000 location writes/sec), with peaks of 5,000 new ride requests/sec.
  • Latency — candidate discovery plus the first dispatch offer within 1 second at p99.
  • Consistency — strict consistency for driver assignment and trip transitions; eventual consistency is fine for the map/discovery index.
  • Fault tolerance — regional network interruptions and node crashes must not lose active trip state.

What the answer should cover

  • Endpoint design for location updates, ride requests, driver accept/decline, and trip transitions.
  • Storage and indexing choices for trips and geospatial data (H3/S2/Redis GEO or equivalent).
  • Matching-engine architecture with concurrency control that avoids double dispatch or split brain.
  • The trip lifecycle machine, ordering guarantees, idempotency, and fare tracking.
  • Failure handling: driver app disconnects, driver crashes mid-trip, and stale location data.

Assume the scope is the dispatch core — pricing/payment execution and detailed ETA modelling are out of scope, but a fare snapshot must be captured for auditing.

Hints

Hint 1

Split the system into a fast, lossy location plane and a small, strictly consistent dispatch/trip plane; the geo index should only nominate candidates, never authorize an assignment.

Hint 2

Use a short-lived conditional lease plus a fencing token on the driver row instead of holding a distributed lock across a network call to the driver's phone.

Hint 3

Give every trip a monotonic version and an append-only event log with unique constraints on (trip_id, version) and (trip_id, idempotency_key), so retries and reordered packets become no-ops.