Swiggy Auto Assignment System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-06-06

Problem Statement

Design and implement a system that automatically assigns orders to delivery partners based on location, availability, capacity, and other constraints. Focus on real-time decision-making, concurrency, and scalability.


Functional Requirements (FR)


Delivery Partner Management


•Track delivery partners’ status: Available, Busy, Offline.
•Track real-time location of delivery partners.
•Maintain partner attributes: vehicle type, maximum order capacity, delivery history.


Order Management


•Orders arrive from restaurants and need assignment to delivery partners.
•Orders have attributes: restaurant location, delivery location, priority, expected delivery time.


Auto Assignment Rules


•Assign the nearest available delivery partner.
•Consider constraints:
•Delivery partner capacity (max orders they can carry simultaneously).
•Vehicle type for order type (e.g., large orders need bike/truck).
•Load balancing among partners to prevent overloading.
•Support manual override if needed.


Notifications & Updates


•Notify delivery partner of assigned order.
•Notify user of delivery partner assignment and estimated delivery time.
•Reassign orders if a delivery partner becomes unavailable.


Non-Functional Requirements (NFR)


•Low Latency → Assign orders in near real-time.
•Scalability → Support thousands of orders and partners simultaneously.
•Concurrency → Handle multiple incoming orders and partner status updates concurrently.
•Reliability → Ensure no order is left unassigned or lost.
•Extensibility → Add future features like route optimization or AI-based ETA predictions.


Core Entities & Attributes


DeliveryPartner


•partnerId, name, location, status (Available/Busy), maxCapacity, currentOrders[]


Order


•orderId, restaurantLocation, deliveryLocation, priority, status (Pending/Assigned/Delivered), assignedPartnerId


Assignment


•assignmentId, orderId, partnerId, timestamp, status (Assigned/Reassigned)


Location


•latitude, longitude


Core Components


OrderService


•Receives new orders and updates order status.


DeliveryPartnerService


•Tracks delivery partner availability, location, and capacity.


AssignmentManager


•Matches orders to delivery partners based on rules and constraints.
•Handles reassignments and notifications.


NotificationService


•Sends real-time alerts to partners and users about assignments.


RouteEstimator (Optional)


•Estimates delivery time based on distance and traffic.


APIs / Operations


Delivery Partner APIs


•updateStatus(partnerId, status)
•updateLocation(partnerId, location)
•getAssignedOrders(partnerId)


Order APIs


•createOrder(orderDetails)
•getOrderStatus(orderId)
•reassignOrder(orderId)


Assignment APIs


•assignOrder(orderId) → Finds best delivery partner and assigns order.
•getAssignments(partnerId / orderId)


Notification APIs


•sendAssignmentNotification(partnerId, orderId)
•sendUserNotification(userId, orderId)


LLD Design Goals


•Class Diagram → DeliveryPartner, Order, Assignment, AssignmentManager, OrderService, DeliveryPartnerService, NotificationService.


Design Patterns:


•Observer Pattern → Notify users and partners of assignment changes.
•Singleton Pattern → For central AssignmentManager.
•Strategy Pattern → For different assignment rules (nearest, least busy, priority-based).
•Concurrency Handling → Thread-safe order assignment when multiple orders arrive simultaneously.
•Trade-offs → Accuracy of nearest-partner assignment vs. speed of computation; synchronous vs. asynchronous notifications.


Solution

Comments