Food Delivery
Design a three-sided food delivery platform: customer ordering, restaurant order management, geospatial courier matching, order lifecycle, and live tracking.
By Ratish Jain · Updated
Problem
A food delivery platform coordinates three parties: customers who order, restaurants that prepare, and couriers who deliver. The system must manage a multi-stage order lifecycle (placed → accepted → preparing → picked-up → delivered), match each order to a nearby available courier using geospatial queries, and stream live location to the customer.
The hard problems are real-time courier matching over a moving fleet, keeping the order state machine consistent across three actors and payment, and delivering timely status updates — all while demand and courier supply shift by neighborhood and time of day.
Functional Requirements
- Browse restaurants/menus and place an order with payment.
- Restaurant accepts and updates preparation status.
- Match orders to nearby available couriers.
- Track courier location live during delivery.
- Notify all parties on each lifecycle transition.
Non-Functional Requirements
- Low-latency geospatial matching (nearby couriers in <1s).
- Consistent order state across customer, restaurant, courier, payment.
- High availability during peak meal-time surges.
- Real-time location updates with acceptable freshness.
- Scalable per-city/region with geographic partitioning.
Capacity Estimation
- Orders/day
- 10M (~120/sec avg, 2K/sec peak)
- Active couriers
- 500K online at peak
- Location pings
- 1 per 4s/courier ≈ 125K/sec
- Restaurants
- 5M listed, ~1M active daily
- Geo index
- sharded by city/region
- Order state store
- 10M orders/day, months retained
APIs
- POST /orders
- placeOrder(customerId, restaurantId, items[], payment) -> {orderId}
- PUT /orders/{id}/status
- updateStatus(orderId, status) -> {ok}
- POST /couriers/location
- ping(courierId, lat, lng) -> {ack}
- GET /orders/{id}/track
- track(orderId) -> {courierLoc, eta, status}
Components
- Order service: owns the order state machine and coordinates payment.
- Matching service: finds and assigns nearby available couriers.
- Geo index: geohash/quadtree store of live courier locations for proximity queries.
- Location ingest: high-throughput pipeline receiving courier GPS pings.
- Tracking service: streams courier position and ETA to customers.
- Notification service: pushes lifecycle updates to all three parties.
- Restaurant service: menu, availability, and prep-status management.
Request Flow
- 1Customer places an order; the order service creates it and authorizes payment.
- 2The restaurant is notified and accepts, moving state to preparing.
- 3The matching service queries the geo index for nearby available couriers.
- 4A courier is offered and accepts; the order is assigned.
- 5Courier location pings stream in; the tracking service updates ETA live.
- 6On delivery, state moves to completed and payment is captured; all parties are notified.
Flow Diagram
Data / Storage Design
The order record is the state-machine source of truth in a strongly consistent store, partitioned by orderId (or city). Because an order spans payment, restaurant, and courier steps, transitions are coordinated as a saga with compensations (e.g., refund if no courier is found).
Courier locations are ephemeral and high-write, so they live in an in-memory geo index using geohash or a quadtree, keyed by region — supporting *find couriers within radius R* efficiently. Pings flow through a streaming pipeline (Kafka) rather than hammering the DB. Restaurant/menu data is read-heavy and cached.
Data Model
Scaling Strategy
The platform is naturally geo-partitioned: a city's orders, couriers, and matching are largely independent, so shard services by region/city to localize load and keep geo queries small. Matching runs per-region against that region's live geo index.
Location ingest scales as a stream: pings are partitioned by courier/region and consumed by workers that update the in-memory index. Order writes scale by partitioning on order/city, while restaurant reads scale via caching and replicas. Peak meal-time surges are handled by autoscaling matching and ingest workers.
Bottlenecks & Failure Scenarios
- Supply-demand imbalance — no couriers available in a hot zone.
- Location-ping firehose overwhelming ingest or the geo index.
- Order state inconsistency across payment/restaurant/courier steps.
- Matching latency during surges causing long customer waits.
- Stale courier locations producing bad ETAs and mismatches.
Design Tradeoffs
- Geo-partitioning localizes load and keeps proximity queries cheap.
- In-memory geo index gives fast radius matching.
- Saga-based lifecycle coordinates three parties with clean compensations.
- Geo index freshness vs write cost of frequent pings.
- Saga complexity versus a single transaction across services.
- Cross-region orders and rebalancing add edge-case handling.
Common Interview Questions
Store live courier locations in a geospatial index using geohash or a quadtree keyed by region. A proximity query maps the order's location to a cell and scans that cell plus neighbors, returning available couriers within radius R without scanning the whole fleet.
Model it as a state machine coordinated by a saga: each step (payment auth, restaurant accept, courier assign) has a compensating action. If a later step fails — e.g., no courier found — earlier steps are undone (refund/void), avoiding a stuck or inconsistent order.
Ingest pings through a streaming pipeline (Kafka) partitioned by region, and update an in-memory geo index rather than writing each ping to a durable DB. This decouples the high write rate from matching reads and keeps location data fresh.
- →Three-sided platform: customers, restaurants, couriers, plus payment.
- →Order is a state machine coordinated as a saga with compensations.
- →Courier matching uses a geohash/quadtree geo index for radius queries.
- →Location pings stream through Kafka into an in-memory index, not the DB.
- →Geo-partition by city to localize load and scale matching per region.
Related
Everything on Archprimer is connected — follow the graph.