KatInterview

Architecting a Real-Time Car Dispatch Backend

System DesignModel answer
HardModel answerSystem Designsystem-designgeospatial-indexingreal-time

Last updated

Design the backend for an on-demand car service: live driver positions, sub-second nearest-driver matching, a reliable trip lifecycle, and no double bookings at city scale.

Build the Backend for an On-Demand Car Service

You are asked to design the server side of a mobile app that connects people who need a car with nearby drivers. The platform must work for a very large user base and react in real time.

What the system must do

  • Live driver positions: Each driver's phone reports its GPS location every few seconds. The platform must ingest these reports and use them to show riders which drivers are nearby.
  • Dispatch: When a rider asks for a trip, find a suitable close-by available driver and send that driver an offer.
  • Trip lifecycle: Track each trip through its states (requested, accepted, in progress, completed) and make sure transitions are applied reliably.
  • Load profile: The system must cope with a read-heavy workload (riders looking at the map) and a write-heavy workload (continuous location reports) at the same time.

Scale and quality targets

  • Peak load: about 500,000 active drivers and 2 million active riders.
  • Finding a driver for a request should complete in under 500 ms.
  • Once a driver accepts a trip, that assignment must be consistent everywhere. A driver must never end up committed to two trips.

What your answer should cover

  1. API design: the endpoints or channels that riders and drivers use.
  2. Spatial indexing: how you store driver positions and answer "who is near this point?" (for example grid cells, geohashes, quadtrees, or a specialized store).
  3. Data storage: which databases you choose for trips, user profiles, and live location data, and why.
  4. Architecture: the main components and message flow, including load balancers, caching, and publish/subscribe messaging.
  5. Trade-offs: how consistency, availability, and partition tolerance apply to location data versus driver assignment.

Start with back-of-the-envelope numbers and state any assumptions you make.

Hints

Hint 1

Do the arithmetic first: 500K drivers reporting every ~4 seconds is about 125K writes/s, and the whole live map is only on the order of 100 MB. Ask what that implies about where live data should be kept and why you would still shard.

Hint 2

Split the system into two workloads with different needs: a huge, short-lived, loss-tolerant stream of locations, and a small, durable, strongly consistent stream of trips and assignments. Choose storage and consistency separately for each.

Hint 3

To prevent double assignment, think of a temporary reservation on the driver with a timeout, followed by a conditional (versioned or compare-and-set) write that moves the driver to busy and the trip to accepted in one atomic step.

Hint 4

For the spatial index, think about what happens at cell borders: a query must cover neighboring cells, not just the one containing the pickup point. Also consider how a very dense downtown cell would be handled.