Design a Food-Aggregation Service like DoorDash
Author: System Design Expert | Date: 2026-08-28 Scope: End-to-end design of a food delivery aggregation platform connecting customers, restaurants, and delivery drivers at scale.
Table of Contents
- Requirements Clarification
- Back-of-Envelope Calculations
- System Interface Design
- High-Level Architecture
- Core Component Design
- Data Model
- Key Design Decisions
- Scalability Strategy
- Curveball Scenarios
- Failure Modes and Mitigations
1. Requirements Clarification
1.1 Functional Requirements
| ID | Requirement | Priority |
|---|---|---|
| FR-01 | Customer App: Browse restaurants, search/filter, view menus, place orders, track delivery in real-time, rate/review, manage addresses | P0 |
| FR-02 | Restaurant Portal: Manage menu (items, prices, availability), receive/accept/reject orders, update order status, view analytics | P0 |
| FR-03 | Dasher (Driver) App: Go online/offline, accept/decline delivery offers, navigate to restaurant and customer, update delivery status, earnings dashboard | P0 |
| FR-04 | Order Routing: Route order to restaurant, assign delivery driver, handle order lifecycle | P0 |
| FR-05 | Real-Time Tracking: Track order status (preparing → picked up → on the way → delivered) with live map | P0 |
| FR-06 | Menu Synchronization: Keep restaurant menus in sync with restaurant’s own systems; handle price/item changes | P0 |
| FR-07 | Payment Processing: Multiple payment methods, tips, delivery fees, service fees, restaurant settlement | P0 |
| FR-08 | Search & Discovery: Full-text search, filters (cuisine, price, rating, distance), recommendations | P1 |
| FR-09 | Multi-Tenant Architecture: Support multiple restaurant brands, each with independent menu, pricing, and branding | P1 |
| FR-10 | Promotions & Coupons: Discount codes, first-order free delivery, loyalty programs, restaurant-sponsored promos | P1 |
| FR-11 | Batch Orders: Group nearby orders for efficient dasher routing (multi-stop deliveries) | P2 |
| FR-12 | Scheduled Orders: Schedule delivery for future time slots | P2 |
| FR-13 | Admin Dashboard: Platform analytics, dispute resolution, restaurant onboarding, compliance | P1 |
1.2 Non-Functional Requirements
| ID | Requirement | Target |
|---|---|---|
| NFR-01 | Latency: Menu browse < 300ms (p95); Order placement < 1s (p95); Search < 200ms (p95) | Critical |
| NFR-02 | Availability: 99.95% uptime for customer-facing services | Critical |
| NFR-03 | Consistency: Menu prices eventually consistent (< 5 min lag); Order state strongly consistent | Critical |
| NFR-04 | Scalability: 5M DAU, 500K orders/day, 100K active restaurants, 200K active dashers | Critical |
| NFR-05 | Multi-Tenant Isolation: Restaurant data fully isolated; one restaurant’s outage doesn’t affect others | High |
| NFR-06 | Menu Freshness: Menu changes reflected within 5 minutes | High |
| NFR-07 | Security: PCI-DSS compliance for payments; PII encryption; GDPR/CCPA compliance | Critical |
| NFR-08 | Durability: Zero data loss for orders and payments (RPO = 0) | Critical |
2. Back-of-Envelope Calculations
2.1 Assumption Sets
| Parameter | Conservative (A) | Base (B) | Aggressive (C) |
|---|---|---|---|
| DAU (customers) | 2M | 5M | 20M |
| Orders per customer/day | 0.3 | 0.5 | 0.8 |
| Total orders/day | 600K | 2.5M | 16M |
| Peak hour ratio (lunch+dinner) | 35% of daily | 35% of daily | 35% of daily |
| Peak orders/hour | 210K | 875K | 5.6M |
| Peak QPS (order placement) | ~58 | ~243 | ~1,555 |
| Active restaurants | 50K | 100K | 500K |
| Active dashers | 100K | 200K | 1M |
| Menu browse QPS | 5,000 | 20,000 | 100,000 |
| Search QPS | 2,000 | 8,000 | 40,000 |
Note: Food delivery has two distinct peaks (lunch 11:30-13:30, dinner 17:30-19:30) unlike ride-hailing which has evening/weekend peaks.
2.2 Storage Estimates
Order Records
- Average order record: ~5 KB (items, addresses, payment, tracking events)
- Daily: 2.5M × 5 KB = 12.5 GB/day
- Annual: 12.5 GB × 365 = ~4.6 TB/year
- 3-year retention: ~13.8 TB
Menu Data
- Average restaurant: 80 items × 500 bytes/item = 40 KB/restaurant
- 100K restaurants × 40 KB = ~4 GB (fits in cache)
- Menu images: 100K × 80 items × 50 KB = ~400 GB (CDN + object storage)
- Menu update rate: ~500 updates/hour (price changes, item availability)
User Profiles & Addresses
- 50M registered users × 3 KB = ~150 GB
- Saved addresses: 50M × 3 × 500 B = ~75 MB
Tracking Events
- Active orders: 50K concurrent × 10 events/order × 200 B = ~100 MB (hot)
- Historical tracking: 2.5M orders/day × 10 events × 200 B = ~5 GB/day
Database Sizing Summary
| Data Type | Daily Write | Daily Read | Storage (3yr) |
|---|---|---|---|
| Orders | 2.5M | 10M | 13.8 TB |
| Menu items | 500K updates | 50M reads | 4 GB (hot cache) |
| Menu images | 10K uploads | 20M reads | 400 GB (CDN) |
| User profiles | 50K | 20M | 150 GB |
| Payments | 2.5M | 5M | 5 TB |
| Reviews | 2.5M | 10M | 5 GB |
| Tracking events | 25M | 50M | Ephemeral (7-day) |
2.3 Bandwidth Estimates
| Traffic Type | Calculation | Throughput |
|---|---|---|
| Menu browsing | 20,000 QPS × 50 KB (with images ref) | ~1 GB/s |
| Search queries | 8,000 QPS × 2 KB | ~16 MB/s |
| Order placement | 243 QPS × 5 KB | ~1.2 MB/s |
| Order tracking | 50K concurrent × 1 Hz × 500 B | ~25 MB/s |
| Dasher location | 200K × 0.5 Hz × 200 B | ~20 MB/s |
| Total estimated | ~1.06 GB/s |
Key insight: Menu browsing dominates bandwidth. Heavy use of CDN and image optimization is critical.
2.4 Infrastructure Sizing (Base Case)
| Component | Instances | CPU | Memory | Notes |
|---|---|---|---|---|
| API Gateway | 30 | 4C | 8 GB | Load balanced |
| Restaurant/Menu Service | 20 | 4C | 16 GB | Read-heavy |
| Order Service | 25 | 4C | 8 GB | Write-heavy at peak |
| Search Service | 15 | 8C | 16 GB | Elasticsearch cluster |
| Dispatch Service | 20 | 8C | 16 GB | CPU-bound (matching) |
| Tracking Service | 15 | 4C | 8 GB | WebSocket-heavy |
| Payment Service | 15 | 4C | 8 GB | Strong consistency |
| Recommendation Service | 10 | 8C | 32 GB | ML inference |
| Redis Cluster (Menu Cache) | 40 nodes | 4C | 32 GB | 8 shards, 5 replicas |
| Kafka Brokers | 15 | 8C | 32 GB | 3 replicas |
| PostgreSQL (Orders, Payments) | 5 | 16C | 64 GB | Primary + 4 replicas |
| Elasticsearch | 12 | 8C | 32 GB | 3 master, 9 data |
| Cassandra (Tracking, History) | 10 | 8C | 32 GB | 3 replicas |
3. System Interface Design
3.1 API Definitions
Customer APIs
GET /api/v1/restaurants — Browse nearby restaurants
GET /api/v1/restaurants/{id} — Restaurant detail
GET /api/v1/restaurants/{id}/menu — Full menu
POST /api/v1/search — Search restaurants/items
POST /api/v1/orders — Place order
GET /api/v1/orders/{id} — Order status
GET /api/v1/orders/{id}/tracking — Real-time tracking
GET /api/v1/orders/history — Order history
POST /api/v1/orders/{id}/cancel — Cancel order
POST /api/v1/orders/{id}/tip — Add/modify tip
GET /api/v1/promotions — Available promotions
POST /api/v1/payments/charge — Charge payment
Restaurant APIs
GET /api/v1/partner/menu — Get current menu
POST /api/v1/partner/menu/items — Add/update menu item
PUT /api/v1/partner/menu/items/{id} — Update item
DELETE /api/v1/partner/menu/items/{id} — Remove item
PUT /api/v1/partner/menu/items/{id}/availability — Toggle availability
GET /api/v1/partner/orders — Incoming orders
POST /api/v1/partner/orders/{id}/accept — Accept order
POST /api/v1/partner/orders/{id}/reject — Reject order
POST /api/v1/partner/orders/{id}/ready — Mark ready for pickup
GET /api/v1/partner/analytics — Sales analytics
Dasher (Driver) APIs
POST /api/v1/dasher/status — Go online/offline
POST /api/v1/dasher/location — Update GPS location
GET /api/v1/dasher/offers — Available delivery offers
POST /api/v1/dasher/offers/{id}/accept — Accept delivery
POST /api/v1/dasher/offers/{id}/decline — Decline delivery
POST /api/v1/dasher/delivery/{id}/update — Update delivery phase
GET /api/v1/dasher/earnings — Earnings summary
Internal Service APIs
POST /internal/orders/create — Create order
POST /internal/orders/{id}/route — Route order to restaurant
POST /internal/dispatch/assign — Assign dasher to order
GET /internal/dispatch/nearby — Find nearby dashers
POST /internal/menu/sync — Sync menu from restaurant
POST /internal/tracking/update — Update tracking event
POST /internal/payments/settle — Settle order payment
3.2 Key Request/Response Contracts
Place Order
POST /api/v1/orders
{
"restaurant_id": "rest_abc123",
"items": [
{ "item_id": "item_001", "quantity": 2, "options": ["no onions", "extra sauce"] },
{ "item_id": "item_005", "quantity": 1, "options": [] }
],
"delivery_address": {
"address_line": "123 Main St, Apt 4B",
"city": "San Francisco",
"state": "CA",
"zip": "94102",
"lat": 37.7849,
"lng": -122.4094
},
"payment_method_id": "pm_xyz789",
"promotion_code": "WELCOME20",
"special_instructions": "Ring doorbell twice"
}
201 Created
{
"order_id": "ord_def456",
"status": "confirmed",
"restaurant_id": "rest_abc123",
"items": [
{ "item_id": "item_001", "name": "Margherita Pizza", "quantity": 2, "unit_price": 14.99, "options": ["no onions", "extra sauce"] },
{ "item_id": "item_005", "name": "Garlic Bread", "quantity": 1, "unit_price": 5.99, "options": [] }
],
"pricing": {
"subtotal": 35.97,
"delivery_fee": 3.99,
"service_fee": 2.99,
"discount": -7.20,
"tax": 3.44,
"total": 39.19,
"currency": "USD"
},
"estimated_delivery_minutes": 30,
"restaurant_eta_minutes": 15
}
Menu Response
GET /api/v1/restaurants/{id}/menu
200 OK
{
"restaurant_id": "rest_abc123",
"name": "Tony's Pizza",
"rating": 4.6,
"delivery_time_min": 25,
"delivery_time_max": 40,
"delivery_fee": 3.99,
"minimum_order": 15.00,
"categories": [
{
"category_id": "cat_001",
"name": "Pizzas",
"items": [
{
"item_id": "item_001",
"name": "Margherita Pizza",
"description": "Fresh mozzarella, tomato sauce, basil",
"price": 14.99,
"image_url": "https://cdn.example.com/menu/item_001.jpg",
"available": true,
"options": [
{ "option_id": "opt_001", "name": "Extra Toppings", "type": "multi_select", "required": false, "choices": [...] }
]
}
]
}
]
}
4. High-Level Architecture
flowchart TB
subgraph Clients["Clients"]
CA["Customer App"]
RP["Restaurant Portal"]
DA["Dasher App"]
ADM["Admin Web"]
end
subgraph Edge["Edge Layer"]
CDN["CDN / WAF"]
LB["Load Balancer"]
GW["API Gateway"]
end
subgraph Auth["Auth & Security"]
AUTH["Auth Service<br/>(OAuth2 / JWT)"]
RBAC["Rate Limiter"]
end
subgraph Core["Core Services"]
RS["Restaurant Service"]
MS["Menu Service"]
OS["Order Service"]
DS["Dispatch Service"]
TRS["Tracking Service"]
PMS["Payment Service"]
SRCH["Search Service"]
REC["Recommendation Service"]
PROM["Promotion Service"]
USS["User Service"]
NS["Notification Service"]
end
subgraph Geo["Geospatial Layer"]
GEO["Geofence Service"]
RT["Routing Engine"]
DIST["Distance Matrix"]
end
subgraph Streaming["Streaming & Events"]
KAFKA["Kafka Cluster"]
ORDER_S["Order Events"]
TRACK_S["Tracking Events"]
MENU_S["Menu Updates"]
DISPATCH_S["Dispatch Events"]
end
subgraph Storage["Data Stores"]
PG["PostgreSQL<br/>(Orders, Users,<br/>Payments)"]
CAS["Cassandra<br/>(Tracking, History)"]
REDIS["Redis Cluster<br/>(Menu Cache,<br/>Sessions, Geo)"]
ES["Elasticsearch<br/>(Search, Logs)"]
S3["S3 / Object Store<br/>(Images, Receipts)"]
end
subgraph External["External Services"]
PAYGW["Payment Gateway"]
MAPS["Maps API"]
SMS["SMS / Push Gateway"]
KITCHEN["Kitchen Display<br/>(Optional Integration)"]
end
CA --> CDN
RP --> CDN
DA --> CDN
ADM --> CDN
CDN --> LB
LB --> GW
GW --> AUTH
GW --> RBAC
GW --> RS
GW --> MS
GW --> OS
GW --> SRCH
GW --> USS
GW --> TRS
RS --> MS
OS --> DS
OS --> PMS
OS --> TRS
OS --> NS
DS --> GEO
DS --> RT
MS --> KAFKA
OS --> KAFKA
DS --> KAFKA
TRS --> KAFKA
KAFKA --> ORDER_S
KAFKA --> TRACK_S
KAFKA --> MENU_S
KAFKA --> DISPATCH_S
MENU_S --> MS
ORDER_S --> OS
TRACK_S --> TRS
DISPATCH_S --> DS
MS --> REDIS
RS --> PG
OS --> PG
PMS --> PG
TRS --> CAS
SRCH --> ES
USS --> PG
NS --> SMS
DS --> MAPS
RT --> MAPS
DIST --> MAPS
MS --> S3
PMS --> PAYGW
OS --> KITCHEN
5. Core Component Design
5.1 Menu Service (Multi-Tenant Catalog)
The menu service is the read-heavy backbone of the platform. Every customer interaction starts with browsing a menu.
Architecture
flowchart TB
subgraph WritePath["Write Path (Restaurant)"]
R["Restaurant Portal"] --> W["Menu Write API"]
W --> VALID["Validation"]
VALID --> VER["Version Increment"]
VER --> DB["Write to PostgreSQL"]
DB --> KAFKA["Publish Menu Update<br/>to Kafka"]
end
subgraph ReadPath["Read Path (Customer)"]
C["Customer App"] --> CACHE["Menu Cache<br/>(Redis)"]
CACHE -->|"Hit"| RETURN["Return Menu"]
CACHE -->|"Miss"| DB2["Read from PostgreSQL"]
DB2 --> CACHE
DB2 --> RETURN
end
KAFKA --> SYNC["Cache Invalidation<br/>Consumer"]
SYNC --> CACHE
Menu Versioning Strategy
- Version number: Each menu has a monotonically increasing version
- Cache key:
menu:{restaurant_id}:{version} - Invalidation: On menu update, publish to Kafka → consumers invalidate old cache keys
- Stale-while-revalidate: Serve stale menu from cache while async refresh happens
- Consistency guarantee: Menu changes visible within 5 minutes (SLA)
Menu Data Structure
Menu {
restaurant_id: UUID
version: Long
categories: [
{
category_id: UUID
name: String
display_order: Int
items: [
{
item_id: UUID
name: String
description: String
price: Decimal
image_url: String
available: Boolean
prep_time_minutes: Int
options: [OptionGroup]
tags: [String] // "vegetarian", "spicy", "bestseller"
}
]
}
]
last_updated: Timestamp
}
5.2 Order Service
Order Lifecycle State Machine
stateDiagram-v2
[*] --> PLACED
PLACED --> CONFIRMED
PLACED --> PENDING_RESTAURANT
PENDING_RESTAURANT --> ACCEPTED_BY_RESTAURANT
PENDING_RESTAURANT --> REJECTED_BY_RESTAURANT
PENDING_RESTAURANT --> EXPIRED_NO_RESPONSE
REJECTED_BY_RESTAURANT --> REFUND_INITIATED
EXPIRED_NO_RESPONSE --> REFUND_INITIATED
REFUND_INITIATED --> [*]
ACCEPTED_BY_RESTAURANT --> PREPARING
PREPARING --> READY_FOR_PICKUP
READY_FOR_PICKUP --> ASSIGNING_DASHER
ASSIGNING_DASHER --> DASHER_ASSIGNED
DASHER_ASSIGNED --> DASHER_AT_RESTAURANT
DASHER_AT_RESTAURANT --> PICKED_UP
PICKED_UP --> ON_THE_WAY
ON_THE_WAY --> DELIVERED
DELIVERED --> PAYMENT_SETTLED
PAYMENT_SETTLED --> [*]
PLACED --> CANCELLED_BY_CUSTOMER
CONFIRMED --> CANCELLED_BY_CUSTOMER
PENDING_RESTAURANT --> CANCELLED_BY_CUSTOMER
CANCELLED_BY_CUSTOMER --> REFUND_INITIATED
ON_THE_WAY --> CANCELLED_DELIVERY_FAILED
CANCELLED_DELIVERY_FAILED --> REFUND_INITIATED
Order Creation Flow
sequenceDiagram
participant C as Customer
participant OS as Order Service
participant MS as Menu Service
participant PS as Promotion Service
participant PMS as Payment Service
participant RS as Restaurant Service
participant DS as Dispatch Service
C->>OS: POST /orders
OS->>MS: Validate items & prices
MS-->>OS: Items valid, prices confirmed
OS->>PS: Apply promotion
PS-->>OS: Discount applied
OS->>PMS: Authorize payment
PMS-->>OS: Auth token
OS->>RS: Forward order to restaurant
RS-->>OS: Restaurant acknowledged
OS->>DS: Request dasher assignment
DS-->>OS: Dasher assigned (async)
OS-->>C: Order confirmed
5.3 Dispatch Service (Dasher Assignment)
The dispatch service matches orders with available dashers. This is analogous to Uber’s matching but with a two-leg journey (restaurant → customer).
Dispatch Pipeline
flowchart TB
A["Order Ready for Pickup"] --> B["Define Pickup Zone<br/>(Restaurant location)"]
B --> C["Candidate Retrieval<br/>(Nearby dashers)"]
C --> D["Score Each Dasher"]
D --> E["Top-K Selection"]
E --> F["Parallel Offer"]
F --> G["First Accept Wins"]
G --> H["Lock Dasher"]
H --> I["Navigate to Restaurant"]
F -->|"All decline"| J["Expand Radius"]
J --> C
G -->|"Timeout"| K["Auto-assign<br/>nearest dasher"]
K --> H
Dasher Scoring Algorithm
Score = w1 × (1 / ETA_to_restaurant)
+ w2 × (1 / ETA_to_customer)
+ w3 × dasher_rating_normalized
+ w4 × acceptance_rate_bonus
+ w5 × current_load_penalty
+ w6 × batch_opportunity_bonus
| Factor | Weight | Rationale |
|---|---|---|
| ETA to restaurant | 0.25 | Faster pickup = faster delivery |
| ETA to customer | 0.25 | Total delivery time matters |
| Dasher rating | 0.15 | Quality of delivery |
| Acceptance rate | 0.10 | Reliability |
| Current load | 0.10 | Prefer dashers with fewer active deliveries |
| Batch opportunity | 0.15 | Bonus if dasher can pick up another nearby order |
Batch Order Logic
- Eligibility: Orders within 1km of each other, same general direction, compatible restaurants
- Time window: Orders placed within 10 minutes of each other
- Max batch size: 3 orders per dasher
- ETA impact: Additional order adds < 10 minutes to total delivery time
- Customer consent: Notify customer of potential batch; allow opt-out
5.4 Search Service
Search Architecture
flowchart TB
Q["Search Query"] --> PARSE["Query Parser"]
PARSE --> TOKENIZE["Tokenization + NLP"]
TOKENIZE --> MULTI["Multi-Index Search"]
MULTI --> REST_ES["Restaurant Index<br/>(Elasticsearch)"]
MULTI --> ITEM_ES["Menu Item Index<br/>(Elasticsearch)"]
MULTI --> GEO["Geospatial Filter<br/>(Redis GEO)"]
REST_ES --> RANK["Ranking Engine"]
ITEM_ES --> RANK
GEO --> RANK
RANK --> PERSONALIZE["Personalization<br/>(User history, preferences)"]
PERSONALIZE --> FILTER["Business Rules<br/>(Delivery range, min order)"]
FILTER --> RESULTS["Ranked Results"]
Search Index Design
Restaurant Index (Elasticsearch)
{
"mappings": {
"properties": {
"restaurant_id": { "type": "keyword" },
"name": { "type": "text", "analyzer": "restaurant_name_analyzer" },
"cuisine_types": { "type": "keyword" },
"description": { "type": "text" },
"rating": { "type": "float" },
"delivery_time_min": { "type": "integer" },
"delivery_fee": { "type": "float" },
"location": { "type": "geo_point" },
"price_range": { "type": "keyword" },
"is_open": { "type": "boolean" },
"order_count_7d": { "type": "integer" },
"tags": { "type": "keyword" }
}
}
}
Ranking Factors
| Factor | Weight | Description |
|---|---|---|
| Text relevance | 0.30 | BM25 score on name, cuisine, description |
| Rating | 0.15 | Higher rated restaurants rank higher |
| Distance | 0.15 | Closer restaurants preferred |
| Delivery time | 0.10 | Faster delivery preferred |
| Popularity | 0.10 | Recent order volume |
| Personalization | 0.10 | User’s past orders, favorites |
| Sponsored | 0.10 | Paid promotion boost |
5.5 Real-Time Tracking Service
Tracking Architecture
flowchart TB
subgraph Sources["Event Sources"]
R["Restaurant Status Updates"]
D["Dasher GPS Updates"]
end
subgraph Processing["Processing"]
KAFKA["Kafka: tracking_events"]
STREAM["Stream Processor<br/>(Flink / Kafka Streams)"]
WS["WebSocket Manager"]
end
subgraph Storage["Storage"]
REDIS["Redis<br/>(Current State)"]
CAS["Cassandra<br/>(Event History)"]
end
subgraph Consumers["Consumers"]
CA["Customer App<br/>(WebSocket)"]
ADM["Admin Dashboard"]
end
R --> KAFKA
D --> KAFKA
KAFKA --> STREAM
STREAM --> REDIS
STREAM --> CAS
STREAM --> WS
WS --> CA
ADM --> REDIS
Tracking Events
| Event | Source | Description |
|---|---|---|
order.confirmed |
Order Service | Order accepted by restaurant |
order.preparing |
Restaurant Portal | Food preparation started |
order.ready |
Restaurant Portal | Food ready for pickup |
dasher.assigned |
Dispatch Service | Dasher assigned to order |
dasher.enroute_to_restaurant |
Dasher App | Dasher heading to restaurant |
dasher.arrived_at_restaurant |
Dasher App | Dasher at restaurant |
dasher.picked_up |
Dasher App | Order picked up |
dasher.enroute_to_customer |
Dasher App | Dasher heading to customer |
dasher.nearby |
Dasher App | Dasher within 5 min of customer |
order.delivered |
Dasher App | Order delivered |
5.6 Payment Service
Payment Flow with Restaurant Settlement
sequenceDiagram
participant C as Customer
participant OS as Order Service
participant PMS as Payment Service
participant PG as Payment Gateway
participant R as Restaurant
participant D as Dasher
C->>PMS: authorize(total_amount)
PMS->>PG: authorize card
PG-->>PMS: auth_token
PMS-->>C: authorized
Note over OS,R: Restaurant prepares food...
Note over D: Dasher delivers...
OS->>PMS: settle(order_id, final_amount)
PMS->>PG: capture(auth_token, final_amount)
PG-->>PMS: captured
PMS->>PMS: Calculate splits:
Note over PMS: Customer pays: $40<br/>Platform commission: $8 (20%)<br/>Restaurant gets: $24<br/>Dasher gets: $8<br/>Tip: $5 (to dasher)
PMS->>R: credit $24 (settlement batch)
PMS->>D: credit $13 ($8 + $5 tip)
PMS-->>OS: settlement_complete
Settlement Model
| Party | Amount | Timing |
|---|---|---|
| Customer | Full amount (subtotal + fees + tax - discount + tip) | Immediate (capture) |
| Platform | Commission (15-25% of subtotal) | Immediate |
| Restaurant | Subtotal minus commission | Daily batch settlement |
| Dasher | Delivery fee + tip | Per-delivery or daily batch |
6. Data Model
6.1 Relational Schema (PostgreSQL)
-- Restaurants (multi-tenant entities)
CREATE TABLE restaurants (
restaurant_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
partner_id UUID, -- Parent partner/brand (for chains)
name VARCHAR(255) NOT NULL,
cuisine_types TEXT[], -- ['Italian', 'Pizza']
address VARCHAR(500),
lat DECIMAL(10,7),
lng DECIMAL(10,7),
delivery_radius_km DECIMAL(5,2) DEFAULT 5.00,
rating DECIMAL(3,2) DEFAULT 0.00,
total_orders INT DEFAULT 0,
is_active BOOLEAN DEFAULT TRUE,
min_order_amount DECIMAL(8,2),
delivery_fee DECIMAL(8,2),
prep_time_avg_min INT DEFAULT 20,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_restaurants_location ON restaurants USING GIST (
ll_to_earth(lat, lng)
);
CREATE INDEX idx_restaurants_cuisine ON restaurants USING GIN (cuisine_types);
-- Menu categories
CREATE TABLE menu_categories (
category_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
restaurant_id UUID REFERENCES restaurants(restaurant_id),
name VARCHAR(100) NOT NULL,
display_order INT DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Menu items
CREATE TABLE menu_items (
item_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
restaurant_id UUID REFERENCES restaurants(restaurant_id),
category_id UUID REFERENCES menu_categories(category_id),
name VARCHAR(255) NOT NULL,
description TEXT,
price DECIMAL(8,2) NOT NULL,
image_url VARCHAR(500),
is_available BOOLEAN DEFAULT TRUE,
prep_time_min INT DEFAULT 10,
tags TEXT[],
menu_version BIGINT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_menu_items_restaurant ON menu_items(restaurant_id, menu_version);
CREATE INDEX idx_menu_items_available ON menu_items(restaurant_id, is_available);
-- Menu item options (customizations)
CREATE TABLE menu_item_options (
option_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
item_id UUID REFERENCES menu_items(item_id),
name VARCHAR(100) NOT NULL,
option_type VARCHAR(20) NOT NULL, -- 'single_select', 'multi_select', 'text'
is_required BOOLEAN DEFAULT FALSE,
choices JSONB, -- [{id, name, price_adjustment}]
menu_version BIGINT NOT NULL
);
-- Orders
CREATE TABLE orders (
order_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
customer_id UUID NOT NULL,
restaurant_id UUID REFERENCES restaurants(restaurant_id),
dasher_id UUID NULL,
status VARCHAR(30) NOT NULL,
delivery_address JSONB NOT NULL,
delivery_lat DECIMAL(10,7),
delivery_lng DECIMAL(10,7),
special_instructions TEXT,
subtotal DECIMAL(10,2) NOT NULL,
delivery_fee DECIMAL(10,2),
service_fee DECIMAL(10,2),
discount DECIMAL(10,2) DEFAULT 0.00,
tax DECIMAL(10,2),
tip DECIMAL(10,2) DEFAULT 0.00,
total DECIMAL(10,2) NOT NULL,
currency VARCHAR(3) DEFAULT 'USD',
promotion_id UUID NULL,
scheduled_for TIMESTAMPTZ NULL,
estimated_delivery TIMESTAMPTZ NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_orders_customer ON orders(customer_id, created_at DESC);
CREATE INDEX idx_orders_restaurant ON orders(restaurant_id, status, created_at);
CREATE INDEX idx_orders_dasher ON orders(dasher_id, status);
CREATE INDEX idx_orders_status ON orders(status, updated_at);
-- Order items
CREATE TABLE order_items (
order_item_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
order_id UUID REFERENCES orders(order_id),
item_id UUID NOT NULL,
item_name VARCHAR(255) NOT NULL, -- Denormalized snapshot
quantity INT NOT NULL,
unit_price DECIMAL(8,2) NOT NULL, -- Snapshot at order time
options_json JSONB, -- Selected options snapshot
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_order_items_order ON order_items(order_id);
-- Payment transactions
CREATE TABLE payment_transactions (
txn_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
order_id UUID NOT NULL,
customer_id UUID NOT NULL,
amount DECIMAL(10,2) NOT NULL,
currency VARCHAR(3) DEFAULT 'USD',
type VARCHAR(20) NOT NULL, -- 'authorization', 'capture', 'refund'
status VARCHAR(20) NOT NULL,
provider_txn_id VARCHAR(255),
idempotency_key VARCHAR(64) UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Restaurant settlements (batch)
CREATE TABLE restaurant_settlements (
settlement_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
restaurant_id UUID REFERENCES restaurants(restaurant_id),
settlement_date DATE NOT NULL,
total_orders INT DEFAULT 0,
gross_amount DECIMAL(12,2),
commission DECIMAL(12,2),
net_amount DECIMAL(12,2),
status VARCHAR(20) DEFAULT 'pending', -- 'pending', 'paid', 'failed'
created_at TIMESTAMPTZ DEFAULT NOW(),
paid_at TIMESTAMPTZ,
UNIQUE(restaurant_id, settlement_date)
);
-- Users
CREATE TABLE users (
user_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) UNIQUE,
phone VARCHAR(20) UNIQUE,
name VARCHAR(255) NOT NULL,
role VARCHAR(20) NOT NULL CHECK (role IN ('customer', 'dasher', 'restaurant_staff', 'admin')),
status VARCHAR(20) DEFAULT 'active',
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Dasher profiles
CREATE TABLE dashers (
dasher_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES users(user_id) UNIQUE,
rating DECIMAL(3,2) DEFAULT 5.00,
total_deliveries INT DEFAULT 0,
acceptance_rate DECIMAL(5,4) DEFAULT 0.0000,
is_online BOOLEAN DEFAULT FALSE,
current_lat DECIMAL(10,7),
current_lng DECIMAL(10,7),
vehicle_type VARCHAR(20), -- 'car', 'bike', 'scooter', 'walking'
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Reviews
CREATE TABLE reviews (
review_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
order_id UUID NOT NULL,
reviewer_id UUID NOT NULL,
restaurant_id UUID,
dasher_id UUID,
rating INT CHECK (rating BETWEEN 1 AND 5),
comment TEXT,
review_type VARCHAR(20) NOT NULL CHECK (review_type IN ('restaurant', 'dasher')),
created_at TIMESTAMPTZ DEFAULT NOW()
);
6.2 NoSQL Schema (Cassandra — Tracking)
-- Tracking events (partitioned by order_id for fast lookup)
CREATE TABLE order_tracking_events (
order_id UUID,
event_time TIMESTAMP,
event_type TEXT,
event_data TEXT, -- JSON payload
source TEXT, -- 'restaurant', 'dasher', 'system'
PRIMARY KEY ((order_id), event_time)
) WITH CLUSTERING ORDER BY (event_time ASC);
-- Dasher current state (partitioned by dasher_id)
CREATE TABLE dasher_current_state (
dasher_id UUID PRIMARY KEY,
is_online BOOLEAN,
current_lat DECIMAL,
current_lng DECIMAL,
active_order_id UUID,
last_updated TIMESTAMP
);
6.3 Redis Cache Schema
# Menu cache (per restaurant, versioned)
SET menu:rest_abc123:v42 '{"categories":[...]}' EX 300
# Restaurant quick info
HSET restaurant:rest_abc123 name "Tony's Pizza" rating 4.6 delivery_min 25 delivery_max 40 is_open 1
# Dasher locations (geo-indexed)
GEOADD dashers:active 37.7749 -122.4194 dash_001
GEOADD dashers:active 37.7849 -122.4094 dash_002
# Order real-time state
HSET order:ord_def456 status PREPARING dasher dash_001 eta_min 25 updated 1724839200
EXPIRE order:ord_def456 7200
# Restaurant open/close status
SET restaurant:rest_abc123:open 1 EX 3600
# Search result cache
SET search:query:pizza:sf:94102 '[rest_abc123, rest_def456]' EX 300
6.4 Elasticsearch Indices
// Restaurant search index
{
"index": "restaurants_v2",
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"restaurant_id": { "type": "keyword" },
"name": {
"type": "text",
"analyzer": "custom_name_analyzer",
"fields": {
"keyword": { "type": "keyword" }
}
},
"cuisine_types": { "type": "keyword" },
"description": { "type": "text" },
"rating": { "type": "float" },
"delivery_time_min": { "type": "integer" },
"delivery_fee": { "type": "float" },
"location": { "type": "geo_point" },
"price_range": { "type": "keyword" },
"is_open": { "type": "boolean" },
"order_count_7d": { "type": "integer" },
"tags": { "type": "keyword" },
"partner_id": { "type": "keyword" }
}
}
}
// Menu item search index
{
"index": "menu_items_v2",
"mappings": {
"properties": {
"item_id": { "type": "keyword" },
"restaurant_id": { "type": "keyword" },
"name": { "type": "text" },
"description": { "type": "text" },
"price": { "type": "float" },
"tags": { "type": "keyword" },
"available": { "type": "boolean" }
}
}
}
7. Key Design Decisions
7.1 Decision Matrix
| Decision | Option A | Option B | Chosen | Rationale |
|---|---|---|---|---|
| Menu Storage | Single JSON blob | Normalized tables | Normalized + JSON cache | Normalized for updates/queries; JSON assembled for cache |
| Menu Cache | Database read-through | Write-through cache | Cache-aside with invalidation | Menu reads vastly outnumber writes; invalidation via Kafka |
| Order Storage | NoSQL | PostgreSQL | PostgreSQL | Orders need ACID, complex queries (by customer, restaurant, date), and joins |
| Search Engine | Database full-text | Elasticsearch | Elasticsearch | Rich relevance ranking, geo-filtering, faceted search, sub-200ms latency |
| Dasher Matching | Centralized queue | Geospatial broadcast | Geospatial broadcast | Two-leg journey needs spatial reasoning; scales with city size |
| Tracking Transport | REST polling | WebSocket | WebSocket | Real-time map updates require push model; 1 Hz updates |
| Multi-Tenancy | Shared DB, row-level | Separate schemas | Shared DB, row-level isolation | Cost-effective; restaurant_id on every table; RBAC for access control |
| Menu Sync | Polling restaurant API | Webhook push | Hybrid | Webhook for real-time; polling fallback for reliability |
| Payment Capture | At order placement | At delivery | At order placement (auth), capture at delivery | Authorize at placement, capture at delivery to handle cancellations |
7.2 Trade-off Deep Dives
Menu Cache Strategy: Cache-Aside vs. Write-Through
| Aspect | Cache-Aside | Write-Through |
|---|---|---|
| Read latency | First miss slow, then fast | Consistently fast |
| Write latency | Fast (async invalidation) | Slower (sync cache update) |
| Consistency | Eventually consistent | Near-strong consistency |
| Complexity | Medium (invalidation logic) | High (cache coherency) |
| Failure mode | Stale data on cache miss | Write failures block updates |
Decision: Cache-aside with Kafka-driven invalidation. Menu changes are infrequent (~500/hr) vs. reads (~20K QPS). 5-minute staleness is acceptable.
Multi-Tenancy: Row-Level vs. Schema-Level
| Aspect | Row-Level (Shared) | Schema-Level (Separate) |
|---|---|---|
| Isolation | Logical (via restaurant_id) | Physical |
| Cost | Lower (shared infra) | Higher (per-tenant overhead) |
| Query flexibility | Cross-tenant analytics easy | Cross-tenant queries hard |
| Blast radius | One bug affects all | Isolated failures |
| Operations | Simpler | Complex (schema migrations × N) |
Decision: Row-level with restaurant_id partition key. For enterprise partners (large chains), offer schema-level isolation as a premium tier.
Payment Capture Timing
| Strategy | Pros | Cons |
|---|---|---|
| Capture at placement | Simple; guaranteed funds | Refund complexity on cancellation |
| Auth at placement, capture at delivery | Clean cancellation; no refunds needed | Card may expire between auth and capture |
| Capture at restaurant acceptance | Balance; funds secured early | Restaurant rejection still needs refund |
Decision: Authorize at order placement, capture at delivery completion. If authorization expires before delivery, re-authorize. This minimizes refund operations while protecting against no-shows.
8. Scalability Strategy
8.1 Geographic Sharding
flowchart TB
subgraph Metro1["Metro: SF Bay Area"]
GW1["API Gateway"]
MS1["Menu Service"]
OS1["Order Service"]
DS1["Dispatch Service"]
REDIS1["Redis Cluster A"]
ES1["ES Cluster A"]
end
subgraph Metro2["Metro: NYC"]
GW2["API Gateway"]
MS2["Menu Service"]
OS2["Order Service"]
DS2["Dispatch Service"]
REDIS2["Redis Cluster B"]
ES2["ES Cluster B"]
end
subgraph Metro3["Metro: Chicago"]
GW3["API Gateway"]
MS3["Menu Service"]
OS3["Order Service"]
DS3["Dispatch Service"]
REDIS3["Redis Cluster C"]
ES3["ES Cluster C"]
end
subgraph Global["Global / Shared"]
PG["PostgreSQL (Orders, Payments)"]
USR["User Service"]
PAY["Payment Service"]
KAFKA["Kafka (cross-metro)"]
CDN["Global CDN"]
end
GW1 --> MS1 --> REDIS1
GW1 --> OS1 --> PG
GW1 --> DS1 --> REDIS1
OS1 --> KAFKA
GW2 --> MS2 --> REDIS2
GW2 --> OS2 --> PG
GW2 --> DS2 --> REDIS2
OS2 --> KAFKA
GW3 --> MS3 --> REDIS3
GW3 --> OS3 --> PG
GW3 --> DS3 --> REDIS3
OS3 --> KAFKA
8.2 Scaling Dimensions
| Dimension | Strategy | Trigger |
|---|---|---|
| Geographic | Metro-level sharding | Each metro gets isolated service pools |
| Compute | K8s HPA auto-scaling | CPU > 70% for 3 min |
| Menu Cache | Redis shard addition | Memory > 75% or hit ratio < 90% |
| Search | ES shard addition | Index size > 50 GB per shard |
| Database | PostgreSQL read replicas | Read latency > 20ms p99 |
| Streaming | Kafka partition increase | Consumer lag > 50K messages |
| WebSocket | Connection sharding by user_id | Connections per proxy > 30K |
8.3 Caching Strategy
| Cache Layer | Content | TTL | Eviction | Hit Ratio Target |
|---|---|---|---|---|
| L1: In-process (Caffeine) | Menu fragments, restaurant info | 60s | LRU | 50% |
| L2: Redis | Full menus, order state, dasher locations | 5-30 min | LRU + TTL | 90% |
| L3: CDN | Menu images, static assets | 24h-7d | Age | 95% |
| L4: Elasticsearch | Search results | 5 min | LRU | 70% |
8.4 Database Partitioning
- PostgreSQL:
orderstable: Range partition bycreated_at(monthly partitions)payment_transactions: Range partition bycreated_at(monthly)menu_items: Partition byrestaurant_idhash (for large restaurant counts)
- Elasticsearch:
- Time-based index rollover (monthly):
restaurants_v2_2026.08 - Alias-based reindexing for zero-downtime schema changes
- Time-based index rollover (monthly):
- Redis:
- Key hash tagging:
menu:{restaurant_id_hash}:{restaurant_id}:{version} - Dasher geo keys per metro:
dashers:{metro_id}:active
- Key hash tagging:
8.5 Peak Handling Strategy
Food delivery has predictable peaks (lunch and dinner). Leverage this:
| Strategy | Description |
|---|---|
| Pre-warming | Scale up compute 30 min before expected peak (11:00 AM, 5:00 PM) |
| Menu pre-caching | Cache all menus in delivery zone before peak hours |
| Dasher pre-positioning | Incentivize dashers to be in high-demand zones before peak |
| Request queuing | Buffer order requests in Kafka during extreme spikes |
| Graceful degradation | During extreme load: disable recommendations, simplify search, reduce image quality |
9. Curveball Scenarios
Curveball 1: “A popular restaurant’s menu changes prices by 20% right before lunch rush. 10,000 customers have already loaded the old menu. How do you handle this?”
Analysis
- Conflict: Customers see old prices but order at new prices → trust issue
- Scale: 10K affected users in a short window
- Business impact: Potential chargebacks, negative reviews, churn
Response
1. Price Lock Guarantee
- Order-time pricing: Prices are locked at the moment the customer adds items to cart
- Cart snapshot: When items are added to cart, capture item prices in the cart object
- Order confirmation: Use cart-snapshot prices, not live menu prices
- Grace period: Cart prices valid for 10 minutes; after that, re-validate
2. Menu Versioning
- Each menu update increments
menu_version - Cart stores
menu_versionat creation time - Order validation checks: if
cart.menu_versiondiffers from current, re-validate prices - If price increased: honor cart price (customer protection)
- If price decreased: use new lower price (customer delight)
3. Real-Time Invalidation
- Active carts: Push notification to users with active carts from affected restaurant
- Message: “Menu updated — your cart prices are locked. Complete order to secure these prices.”
- UI indicator: Show “prices locked” badge on cart items
4. Restaurant-Side Controls
- Change window: Restrict major price changes during peak hours (11 AM - 2 PM, 5 PM - 8 PM)
- Gradual rollout: Price changes take effect after 15-minute grace period
- Approval workflow: Price changes > 10% require manager approval
5. Data Model Support
-- Cart stores price snapshot
CREATE TABLE carts (
cart_id UUID PRIMARY KEY,
customer_id UUID,
restaurant_id UUID,
menu_version BIGINT NOT NULL, -- Snapshot version
items JSONB NOT NULL, -- [{item_id, name, price_snapshot, quantity, options}]
created_at TIMESTAMPTZ,
expires_at TIMESTAMPTZ -- 10 min from creation
);
Curveball 2: “Design a ‘group order’ feature where multiple people in an office can add items to a single order from the same restaurant.”
Analysis
- New actor: Multiple customers, one order
- Coordination: Who pays? When does the order finalize?
- UX complexity: Real-time collaboration on a shared cart
Response
1. Group Order Lifecycle
stateDiagram-v2
[*] --> CREATED
CREATED --> SHARED
SHARED --> ITEMS_BEING_ADDED
ITEMS_BEING_ADDED --> FINALIZING
FINALIZING --> PAYMENT_COLLECTED
PAYMENT_COLLECTED --> PLACED
PLACED --> DELIVERED
DELIVERED --> [*]
SHARED --> EXPIRED
EXPIRED --> [*]
ITEMS_BEING_ADDED --> CANCELLED
CANCELLED --> [*]
2. Architecture
flowchart TB
subgraph Creator["Order Creator"]
C1["Create Group Order"]
C2["Set Deadline"]
C3["Share Link/Code"]
end
subgraph Participants["Participants"]
P1["Join via Link"]
P2["Browse Menu"]
P3["Add Items"]
end
subgraph System["System"]
GC["Group Cart Service"]
WS["WebSocket<br/>(Real-time sync)"]
TIMER["Deadline Timer"]
PAY["Payment Split"]
end
C1 --> GC
C3 --> P1
P1 --> P2 --> P3
P3 --> GC
GC --> WS
WS --> P2
TIMER --> GC
GC -->|"Deadline reached"| PAY
PAY --> C1
3. Key Design Decisions
| Aspect | Design |
|---|---|
| Cart model | Shared cart with item-level ownership (who added what) |
| Real-time sync | WebSocket broadcasts cart changes to all participants |
| Conflict resolution | Last-write-wins for quantity changes; version vector for consistency |
| Payment split | Each participant pays for their items; organizer pays fees |
| Deadline | Configurable (default 15 min); auto-place when deadline hits |
| Delivery | Single delivery to one address (office lobby/reception) |
4. Data Model
CREATE TABLE group_orders (
group_order_id UUID PRIMARY KEY,
creator_id UUID NOT NULL,
restaurant_id UUID NOT NULL,
delivery_address JSONB NOT NULL,
status VARCHAR(20),
deadline TIMESTAMPTZ NOT NULL,
join_code VARCHAR(8) UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE group_order_participants (
participant_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
group_order_id UUID REFERENCES group_orders(group_order_id),
user_id UUID NOT NULL,
joined_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE group_order_items (
item_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
group_order_id UUID REFERENCES group_orders(group_order_id),
participant_id UUID REFERENCES group_order_participants(participant_id),
menu_item_id UUID NOT NULL,
menu_item_name VARCHAR(255),
quantity INT NOT NULL,
unit_price DECIMAL(8,2),
options_json JSONB
);
Curveball 3: “A restaurant partner wants to integrate their existing POS (Point of Sale) system so orders flow automatically without a separate tablet. Design the integration.”
Analysis
- Integration pattern: Bi-directional sync between external POS and our platform
- Reliability: POS systems vary in quality; network connectivity is unreliable
- Data mapping: Different POS systems have different data models
Response
1. Integration Architecture
flowchart TB
subgraph POS["Restaurant POS System"]
POS1["Square / Toast / Clover"]
POS_API["POS REST API / Webhooks"]
end
subgraph Integration["Integration Layer"]
ADAPTER["POS Adapter<br/>(Strategy Pattern)"]
NORMALIZE["Data Normalizer"]
QUEUE["Reliability Queue"]
SYNC["Sync Orchestrator"]
end
subgraph Platform["DoorDash Platform"]
OS["Order Service"]
MS["Menu Service"]
end
POS_API -->|"Webhook: New Order"| ADAPTER
POS_API -->|"API: Menu Sync"| ADAPTER
ADAPTER --> NORMALIZE
NORMALIZE --> QUEUE
QUEUE --> SYNC
SYNC --> OS
SYNC --> MS
OS -->|"API: Push Order"| ADAPTER
ADAPTER --> POS_API
2. POS Adapter Framework
Interface POSAdapter {
// Menu sync
fetchMenu(): NormalizedMenu
pushMenuUpdate(menu: NormalizedMenu): Result
// Order flow
receiveOrder(order: NormalizedOrder): Result
updateOrderStatus(orderId: String, status: String): Result
// Health
healthCheck(): HealthStatus
}
// Concrete adapters
class SquareAdapter implements POSAdapter { ... }
class ToastAdapter implements POSAdapter { ... }
class CloverAdapter implements POSAdapter { ... }
3. Data Normalization Layer
Maps POS-specific data models to our internal schema:
| POS Field | Normalized Field |
|---|---|
Square.item_name |
name |
Toast.menu_item.title |
name |
Square.price |
price (always in cents) |
Toast.availability |
is_available |
Square.modifiers |
options |
4. Reliability Mechanisms
| Mechanism | Description |
|---|---|
| Idempotency keys | Every sync operation carries a unique idempotency key |
| Retry with backoff | Exponential backoff (1s, 5s, 30s, 5min, 15min) |
| Dead letter queue | Failed operations after max retries go to DLQ for manual review |
| Checksum sync | Periodic full menu diff to detect drift |
| Circuit breaker | If POS is down > 5 min, switch to manual mode |
5. Order Flow Options
| Mode | Description | When to Use |
|---|---|---|
| Push (POS → Platform) | POS sends order webhook → Platform creates order | POS is primary system |
| Pull (Platform → POS) | Platform pushes order to POS via API | Platform is primary system |
| Bidirectional | Both directions with conflict resolution | Both systems of record |
Curveball 4: “Design a feature to detect and prevent fraud: customers ordering large quantities to resell, or dashers faking deliveries.”
Response
1. Customer Fraud Detection
| Signal | Detection Method | Action |
|---|---|---|
| Excessive orders | > 10 orders/hour from same account | Flag for review; require phone verification |
| Reselling pattern | Same items, same restaurant, bulk quantities | Price surcharge; limit quantities |
| Stolen payment | Chargeback rate > 5% | Block payment method; require pre-auth |
| Coupon abuse | > 5 promo codes used in 24 hours | Invalidate remaining codes |
| Fake addresses | GPS doesn’t match delivery address | Require photo verification on delivery |
2. Dasher Fraud Detection
| Signal | Detection Method | Action |
|---|---|---|
| Fake delivery | GPS never near customer address | Flag delivery; require photo proof |
| Collusion | Same dasher + same customer, frequent orders | Investigate; suspend both accounts |
| Speed anomalies | Impossible travel speeds between locations | Flag for review |
| Self-delivery | Dasher orders from restaurant they’re near | Cross-reference dasher ID with customer ID |
3. ML Fraud Pipeline
flowchart LR
EVENTS["Behavioral Events"] --> FEATURE["Feature Store"]
FEATURE --> MODEL["Fraud ML Model<br/>(XGBoost / Isolation Forest)"]
MODEL --> SCORE["Fraud Score (0-1)"]
SCORE -->|"Score > 0.9"| BLOCK["Auto-block"]
SCORE -->|"0.5-0.9"| REVIEW["Human review queue"]
SCORE -->|"Score < 0.5"| ALLOW["Allow"]
4. Photo Verification
- Dasher must take photo at customer location upon delivery
- Image geotagged and timestamped
- ML validates photo authenticity (not stock image, matches location)
10. Failure Modes and Mitigations
10.1 Failure Mode Analysis
| Failure Mode | Impact | Probability | Mitigation |
|---|---|---|---|
| Menu cache stampede | All menu requests hit DB simultaneously | Medium | Cache stampede protection (single-flight); staggered TTL jitter |
| Restaurant API down | Can’t accept orders from that restaurant | Medium | Auto-mark restaurant as temporarily unavailable; queue orders |
| Dispatch service overload | Orders not assigned to dashers | Medium | Fallback to nearest-dasher auto-assign; increase offer timeout |
| Payment gateway failure | Orders can’t be charged | Low | Multi-gateway failover (Stripe → Adyen → PayPal); queue payments |
| Elasticsearch cluster split | Search returns incomplete results | Low | Multi-zone ES cluster; fallback to database search |
| WebSocket connection loss | Tracking stops for customers | Medium | Auto-reconnect; SSE fallback; periodic REST polling backup |
| Kafka consumer lag | Tracking events delayed | Medium | Auto-scale consumers; priority topics for real-time events |
| Restaurant rejects all orders | Customers can’t order | Low | Auto-notify customers; suggest alternatives; alert restaurant partner |
| DDoS attack | Service unavailable | Medium | WAF + rate limiting + CDN + auto-scaling + CAPTCHA |
| Region outage | Entire metro offline | Very Low | Multi-region active-active; DNS failover; graceful degradation |
10.2 Disaster Recovery Plan
flowchart TB
subgraph Primary["Primary Region (US-East)"]
APP1["All Services"]
DB1["DB Primary"]
ES1["ES Primary"]
end
subgraph Secondary["Secondary Region (US-West)"]
APP2["All Services (standby)"]
DB2["DB Replica"]
ES2["ES Replica"]
end
APP1 -->|"Async replication"| DB2
DB1 -->|"Async replication"| DB2
ES1 -->|"Cross-cluster replication"| ES2
DNS["Global DNS"] -->|"Health check"| APP1
DNS -->|"Failover"| APP2
HEALTH["Health Monitor"] -->|"Detect failure"| DNS
DNS -->|"Switch traffic"| APP2
- RPO: < 1 minute (async replication lag)
- RTO: < 5 minutes (automated DNS failover + warm standby)
- Failover drill: Quarterly automated testing
10.3 Observability Stack
| Component | Tool | Purpose |
|---|---|---|
| Metrics | Prometheus + Grafana | Latency, throughput, error rates, business KPIs |
| Tracing | Jaeger / OpenTelemetry | End-to-end request tracing |
| Logging | ELK Stack | Centralized structured logging |
| Alerting | PagerDuty | On-call alerts with auto-escalation |
| APM | Datadog | Application performance monitoring |
| Business Dashboard | Custom Grafana | Orders/hour, avg delivery time, restaurant uptime, dasher utilization |
10.4 Key SLOs
| Service | SLO | Measurement |
|---|---|---|
| Menu Browse | 99.5% < 300ms | Menu API latency histogram |
| Order Placement | 99.9% success | Order creation success rate |
| Search | 99.5% < 200ms | Search latency histogram |
| Dispatch | 95% assigned within 2 min | Dasher assignment latency |
| Payment Processing | 99.99% success | Payment success ratio |
| Tracking | 99.9% uptime | WebSocket connection uptime |
| Overall Platform | 99.95% availability | Error budget: 26 min/month downtime |
10.5 Menu Consistency Guarantee
| Scenario | Behavior | Recovery |
|---|---|---|
| Restaurant updates price | Old price served from cache for up to 5 min | Kafka invalidation → cache refresh |
| Restaurant removes item | Item hidden from search immediately; cached menu shows “unavailable” badge | Next cache refresh removes item |
| Restaurant closes | is_open flag updated in Redis within 30 sec |
All menus show “closed” status |
| Cache node failure | Traffic routed to replica; slight latency increase | Automatic failover; no data loss |
Appendix A: Glossary
| Term | Definition |
|---|---|
| Dasher | DoorDash’s term for delivery drivers |
| POS | Point of Sale — restaurant’s ordering/payment system |
| Multi-tenant | Architecture serving multiple independent customers (restaurants) on shared infrastructure |
| Batch Order | Multiple customer orders combined for efficient single delivery |
| Menu Versioning | Tracking menu changes with monotonically increasing version numbers |
| Cache Stampede | Multiple requests simultaneously miss cache and hit the backend |
| Single-Flight | Deduplicating concurrent identical requests to a single backend call |
| H3 | Hexagonal hierarchical spatial index (used for zone-based operations) |
Appendix B: Comparison with Uber Design
| Aspect | Uber (Cab) | DoorDash (Food) | Key Difference |
|---|---|---|---|
| Matching | Rider ↔ Driver (1 leg) | Order → Restaurant → Dasher (2 legs) | Food has an intermediate stop |
| Catalog | Vehicle types (fixed) | Restaurant menus (dynamic, per-tenant) | Food catalog is vastly more complex |
| Peak pattern | Evening/weekend | Lunch + dinner (predictable) | Food peaks are more predictable |
| Real-time data | GPS tracking (continuous) | Status events (discrete) | Food tracking is event-driven, not continuous |
| Pricing | Dynamic (surge) | Menu prices + delivery fee | Food has item-level pricing complexity |
| Multi-tenancy | Not applicable | Core requirement | Food must isolate 100K+ restaurants |
| External integration | Minimal | POS integration critical | Food must integrate with restaurant systems |
Appendix C: References
- DoorDash Engineering Blog: Scaling DoorDash’s Real-Time Tracking
- Uber H3: Hexagonal Hierarchical Spatial Index
- Elasticsearch: Elasticsearch Guide
- Kafka: Apache Kafka Documentation