Archprimer
ArchitectureAdvanced 8 min read

Food Delivery

Design a three-sided food delivery platform: customer ordering, restaurant order management, geospatial courier matching, order lifecycle, and live tracking.

geospatialmatchingorder-lifecycletrackingthree-sided

By Ratish Jain · Updated

Diagram with 6 components: Customer, Order API, Order Svc, Matching Svc, Geo Index, Notify/Track. Connections: Customer → Order API (1 · place); Order API → Order Svc (2 · create); Order Svc → Matching Svc (3 · dispatch); Matching Svc → Geo Index (4 · nearby); Order Svc → Notify/Track (5 · updates).
ClientGatewayServiceDatabase
Order flows from customer to restaurant, then a courier is matched geospatially.

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

  1. 1Customer places an order; the order service creates it and authorizes payment.
  2. 2The restaurant is notified and accepts, moving state to preparing.
  3. 3The matching service queries the geo index for nearby available couriers.
  4. 4A courier is offered and accepts; the order is assigned.
  5. 5Courier location pings stream in; the tracking service updates ETA live.
  6. 6On delivery, state moves to completed and payment is captured; all parties are notified.

Flow Diagram

Diagram with 6 components: Customer, Order API, Match Engine, Geo Index, Courier App, Tracking. Connections: Customer → Order API (1 · order); Order API → Match Engine (2 · match); Match Engine → Geo Index (3 · nearby); Match Engine → Courier App (4 · assign); Courier App → Tracking (5 · location).
ClientServiceDatabaseExternal
An order matches a nearby courier via the geo index, then streams location.

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

Diagram with 5 components: Order API, Order DB, Kafka Ingest, Geo Consumer, Geo Index. Connections: Order API → Order DB (1 · persist); Order API → Kafka Ingest (2 · location); Kafka Ingest → Geo Consumer (3 · stream); Geo Consumer → Geo Index (4 · upsert).
ServiceDatabaseQueue
Orders persist to the DB; courier pings flow through Kafka into the geo index.

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

Pros
  • 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.
Cons
  • 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

How do you find nearby couriers efficiently?

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.

How do you keep the order lifecycle consistent?

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.

How do you handle the location-ping firehose?

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.

60-Second Revision
  • →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.