Design the order API for a food delivery app

Difficulty: Advanced

Question

Design the API for placing and tracking a food delivery order (think Swiggy or Zomato): endpoints, order states, idempotency and real-time updates.

Answer

For a design question like this I would first list actors and flows. Actors: customer app, restaurant app, delivery partner app, and internal systems for payment and dispatch. Core flows: browse a menu, build a cart, place an order with payment, restaurant accepts, food is prepared, partner picks up and delivers, customer tracks live, and cancellation or refund. The order is a long-lived entity with a lifecycle, so I would model it explicitly as a state machine.

Order states: created, payment_pending, confirmed (paid), accepted_by_restaurant, preparing, ready_for_pickup, picked_up, delivered, plus cancelled and refunded terminal branches. Allowed transitions are enforced on the server, for example you cannot cancel after picked_up, and delivered is terminal. Invalid transitions return 409 Conflict. Store a status history (event log) with timestamps and actors, which helps support and analytics.

Endpoints. Menus: GET /v1/restaurants/{id}/menu (cacheable). Quote: POST /v1/orders/quotes with cart items and address returns totals, taxes, delivery fee and ETA, valid for a short window, so the price the user sees is the price they pay. Place order: POST /v1/orders with a quote_id, address_id, payment method and an Idempotency-Key header. It returns 201 with the order in payment_pending, or 202 if payment confirmation is asynchronous. Retrieve: GET /v1/orders/{id}. List: GET /v1/orders?status=active&limit=20 with cursor pagination. Cancel: POST /v1/orders/{id}/cancel with a reason; it returns 409 if it's no longer cancellable. Tracking: GET /v1/orders/{id}/tracking for the snapshot. Rating: POST /v1/orders/{id}/reviews. For restaurants: POST /v1/orders/{id}/accept and /reject (or PATCH the status with validation). For partners: PATCH assignment and pickup or delivery confirmations, with role-specific authorization.

Idempotency and duplicates matter most: people tap Place Order twice on flaky mobile networks. The Idempotency-Key, unique per checkout attempt, makes the POST safe to retry, returning the same order. Combine with payment idempotency downstream so the customer is never charged twice. Money is stored in paise as integers. Prices are snapshotted into the order line items at placement time so later menu edits don't alter history.

Payment integration: create a payment intent with the gateway, and treat the gateway webhook (payment.captured or failed) as the source of truth to move the order from payment_pending to confirmed. If payment isn't completed in, say, 10 minutes, a timer cancels the order. Never trust the client's claim that payment succeeded.

Real-time tracking: polling GET works as a baseline but is wasteful. For live location and status, use Server-Sent Events (GET /v1/orders/{id}/events with text/event-stream), WebSockets, or push notifications through FCM and APNs. The delivery partner app sends GPS pings every few seconds to a separate high-throughput ingestion path (not through the main orders service), which feeds a location store and fans out to subscribed customers. The tracking API returns ETA, partner name and last location.

Concurrency and consistency: two actors may modify the order at once, e.g. the customer cancels while the restaurant accepts. Use optimistic concurrency with a version column and compare-and-set transitions in SQL, so exactly one wins. Emit domain events (order.confirmed, order.picked_up) via an outbox to Kafka so dispatch, notifications, analytics and billing react asynchronously and independently.

Other considerations: authorization so customers only see their orders and restaurants only theirs; rate limits on order placement; geofencing and serviceability checks before the quote; and graceful degradation when the restaurant's system is slow, using timeouts with auto-reject and re-assign.

Code examples

Placing an order safely

POST /v1/orders HTTP/1.1
Authorization: Bearer <token>
Idempotency-Key: c2a1f0d4-7e5b-4b1a-8d90-2f3c4b5a6d7e
Content-Type: application/json

{ "quote_id": "qt_88", "address_id": "adr_5", "payment_method": "upi" }

HTTP/1.1 201 Created
Location: /v1/orders/ord_9001
{
  "id": "ord_9001",
  "status": "payment_pending",
  "total": 48500,
  "currency": "INR",
  "items": [ { "name": "Paneer Tikka", "qty": 1, "unit_price": 24000 } ],
  "eta_minutes": 35
}

The idempotency key protects against double taps; prices are snapshotted in paise.

State transition with compare-and-set

const ALLOWED = { created:['payment_pending','cancelled'], payment_pending:['confirmed','cancelled'], confirmed:['accepted','cancelled'], accepted:['preparing','cancelled'], preparing:['ready'], ready:['picked_up'], picked_up:['delivered'] };

async function transition(orderId, from, to) {
  if (!ALLOWED[from]?.includes(to)) throw Object.assign(new Error('Invalid transition'), { status: 409 });
  const { rowCount } = await db.query(
    'UPDATE orders SET status=$1, version=version+1 WHERE id=$2 AND status=$3', [to, orderId, from]);
  if (rowCount === 0) throw Object.assign(new Error('Order changed'), { status: 409 });
}

Only legal transitions pass, and the WHERE status clause ensures exactly one concurrent actor wins.

Key points

Common follow-up questions

Interview tips

Concepts covered

order lifecycle, state machine, idempotency, real-time tracking

More API Design questions