Food Delivery System Design (HLD & LLD)
Functional Requirements
Section titled “Functional Requirements”Customer
Section titled “Customer”- Search restaurants
- Browse menu
- Add items to cart
- Place order
- Online payment / Cash on Delivery (COD)
- Track order
- View order history
Restaurant
Section titled “Restaurant”- Manage menu
- Accept or reject orders
- Update order status
Delivery Partner
Section titled “Delivery Partner”- Accept delivery requests
- Pick up orders
- Deliver orders
- Update delivery status
Non-Functional Requirements
Section titled “Non-Functional Requirements”- High availability
- Low latency
- Horizontal scalability
- Fault tolerance
- Real-time tracking
- High throughput during peak hours
High-Level Architecture
Section titled “High-Level Architecture”flowchart TD
A["Client Apps<br/>Customer / Restaurant / Delivery Partner"]
A --> B[API Gateway]
B --> U[User Service]
B --> R[Restaurant Service]
B --> O[Order Service]
B --> P[Payment Service]
B --> D[Delivery Service]
B --> N[Notification Service]
O --> K[(Kafka / Message Queue)]
K --> W[Tracking / Analytics / Notification Workers]
Components
Section titled “Components”API Gateway
Section titled “API Gateway”- Authentication
- Rate limiting
- Routing
- Logging
User Service
Section titled “User Service”- User profile
- Addresses
- Authentication
Restaurant Service
Section titled “Restaurant Service”- Restaurant data
- Menu management
- Availability
Order Service
Section titled “Order Service”- Cart
- Order creation
- Order state management
Payment Service
Section titled “Payment Service”- Payment gateway integration
- Refunds
- Transaction tracking
Delivery Service
Section titled “Delivery Service”- Assign delivery partner
- Track delivery status
Notification Service
Section titled “Notification Service”- Push notifications
- SMS
Message Queue
Section titled “Message Queue”- Order events
- Delivery updates
- Notifications
Order Flow
Section titled “Order Flow”- User selects restaurant and menu items.
- Client invokes the Create Order API.
- Order Service validates items, creates the order, and publishes an event.
- Payment Service processes payment.
- Restaurant accepts the order.
- Delivery partner is assigned.
- Order status is updated throughout the lifecycle.
- Notifications are sent to the customer.
sequenceDiagram
participant C as Customer
participant O as Order Service
participant P as Payment Service
participant R as Restaurant
participant D as Delivery Service
C->>O: Create Order
O->>P: Process Payment
P-->>O: Success
O->>R: Send Order
R-->>O: Accept Order
O->>D: Assign Driver
D-->>C: Live Status Updates
Database Design
Section titled “Database Design”Users-----user_id (PK)namephoneemailcreated_atRestaurants
Section titled “Restaurants”Restaurants-----------restaurant_id (PK)namelocationratingis_openMenu Items
Section titled “Menu Items”MenuItems---------item_id (PK)restaurant_id (FK)namepriceis_availableOrders
Section titled “Orders”Orders------order_id (PK)user_idrestaurant_idstatustotal_pricepayment_statuscreated_atOrder Items
Section titled “Order Items”OrderItems----------idorder_iditem_idquantitypriceDelivery
Section titled “Delivery”Delivery--------delivery_idorder_iddriver_idstatuspickup_timedelivery_timeER Diagram
Section titled “ER Diagram”erDiagram
USERS ||--o{ ORDERS : places
RESTAURANTS ||--o{ MENU_ITEMS : offers
RESTAURANTS ||--o{ ORDERS : receives
ORDERS ||--|{ ORDER_ITEMS : contains
MENU_ITEMS ||--o{ ORDER_ITEMS : references
ORDERS ||--|| DELIVERY : fulfilled_by
Order State Machine
Section titled “Order State Machine”stateDiagram-v2
[*] --> CREATED
CREATED --> PAYMENT_PENDING
PAYMENT_PENDING --> PAYMENT_SUCCESS
PAYMENT_PENDING --> PAYMENT_FAILED
PAYMENT_SUCCESS --> RESTAURANT_ACCEPTED
RESTAURANT_ACCEPTED --> PREPARING
PREPARING --> PICKED_UP
PICKED_UP --> DELIVERED
CREATED --> CANCELLED
RESTAURANT_ACCEPTED --> REJECTED_BY_RESTAURANT
Delivery Partner Assignment
Section titled “Delivery Partner Assignment”Use:
- GeoHash
- Redis GeoSpatial Index
Algorithm:
Find drivers within a 3 km radiusSort by distanceAssign the first available driverFor the full geospatial matching mechanics (Redis GEOADD/GEORADIUS, handling hundreds of thousands of location updates/sec, and preventing double-assignment race conditions), see the Ride Booking system design case study.
Real-Time Order Tracking
Section titled “Real-Time Order Tracking”flowchart LR
A[Driver App] --> B[Location Service]
B --> C[WebSocket / SSE]
C --> D[Customer App]
Technologies:
- WebSocket
- Redis Pub/Sub
- Kafka Streaming
Caching Strategy
Section titled “Caching Strategy”Use Redis for:
- Restaurant list
- Menu items
- Active drivers
- Frequently ordered items
Example key:
menu:{restaurant_id}Scaling Strategy
Section titled “Scaling Strategy”Read Scaling
Section titled “Read Scaling”- Read replicas
- CDN for images
- Redis cache
Write Scaling
Section titled “Write Scaling”- Shard orders table
- Partition by order ID or region
Microservices
Section titled “Microservices”- Kubernetes deployment
- Autoscaling
Handling Peak Traffic
Section titled “Handling Peak Traffic”- Queue incoming orders
- Rate limiting
- Load balancing
- Autoscaling
Failure Handling
Section titled “Failure Handling”Use the Saga Pattern.
Workflow:
Create Order-> Process Payment-> Confirm Restaurant-> Assign DeliveryCompensation:
Refund PaymentSecurity
Section titled “Security”- JWT authentication
- HTTPS
- Rate limiting
- Fraud detection
- Secure payment gateway integration
Low-Level Design (LLD)
Section titled “Low-Level Design (LLD)”class Order { String orderId; Long userId; Long restaurantId; List<OrderItem> items; OrderStatus status; PaymentStatus paymentStatus; BigDecimal totalPrice;}OrderItem
Section titled “OrderItem”class OrderItem { Long itemId; int quantity; BigDecimal price;}Delivery
Section titled “Delivery”class Delivery { String deliveryId; Long driverId; String orderId; DeliveryStatus status; Location currentLocation;}Create Order
Section titled “Create Order”POST /ordersRequest:
{ "restaurantId": 101, "items": [ { "itemId": 1, "qty": 2 }, { "itemId": 5, "qty": 1 } ]}Track Order
Section titled “Track Order”GET /orders/{orderId}/statusRestaurant Menu
Section titled “Restaurant Menu”GET /restaurants/{id}/menuAdvanced Interview Topics
Section titled “Advanced Interview Topics”- Multi-region deployment
- ETA prediction using machine learning
- Dynamic pricing
- Surge delivery fee
- Restaurant ranking algorithm
- Fraud detection
- Delivery batching
Key Takeaways
Section titled “Key Takeaways”- Microservices isolate business capabilities and scale independently.
- Event-driven communication improves resilience and decouples services.
- Redis and Kafka are critical for low latency and asynchronous workflows.
- Geo-spatial indexing enables efficient driver matching.
- Saga pattern provides reliable distributed transaction handling.