0
0
LLDsystem_design~7 mins

Delivery agent assignment in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When multiple delivery agents are available, assigning orders manually or randomly can cause delays, uneven workload, and poor customer experience. Without a systematic approach, some agents may be overloaded while others remain idle, leading to inefficiency and slower deliveries.
Solution
This pattern assigns delivery agents to orders based on criteria like proximity, current workload, and delivery capacity. The system automatically selects the best available agent for each order, balancing load and minimizing delivery time. It continuously updates agent status to optimize assignments dynamically.
Architecture
Order
Service
Assignment
Agent Status Store
Agent Status Store

This diagram shows how the order service sends new orders to the assignment service, which consults the agent status store to pick the best delivery agent and then assigns the order to that agent.

Trade-offs
✓ Pros
Improves delivery speed by assigning nearest or least busy agents.
Balances workload among agents to prevent burnout and idle time.
Automates assignment, reducing manual errors and delays.
Allows dynamic updates as agent availability changes.
✗ Cons
Requires real-time tracking of agent status, adding system complexity.
Needs careful tuning of assignment criteria to avoid unfair load distribution.
May require fallback logic if no suitable agent is available.
Use when the delivery system has multiple agents and order volume exceeds 100 orders per hour, requiring automated, efficient assignment to maintain service quality.
Avoid when the delivery area is very small with only a few agents or order volume is below 10 orders per hour, where manual assignment is simpler and sufficient.
Real World Examples
Uber Eats
Assigns delivery partners to food orders based on proximity and current workload to minimize delivery time and balance agent utilization.
Amazon
Automatically assigns packages to delivery drivers considering route optimization and driver capacity to ensure timely deliveries.
DoorDash
Uses dynamic assignment to match delivery agents with orders in real-time, adjusting for traffic and agent availability.
Code Example
The before code assigns orders randomly, which can overload some agents. The after code assigns orders to the agent with the least current workload, balancing assignments and improving efficiency.
LLD
### Before: naive random assignment
import random

class DeliveryAgent:
    def __init__(self, id):
        self.id = id

class Order:
    def __init__(self, id):
        self.id = id

class AssignmentService:
    def __init__(self, agents):
        self.agents = agents

    def assign(self, order):
        agent = random.choice(self.agents)
        print(f"Order {order.id} assigned to agent {agent.id} (random)")


### After: assignment based on least workload

class DeliveryAgentWithLoad:
    def __init__(self, id):
        self.id = id
        self.current_load = 0

class AssignmentServiceWithLoad:
    def __init__(self, agents):
        self.agents = agents

    def assign(self, order):
        # Pick agent with least current load
        agent = min(self.agents, key=lambda a: a.current_load)
        agent.current_load += 1
        print(f"Order {order.id} assigned to agent {agent.id} (least load)")

# Example usage
agents = [DeliveryAgentWithLoad(i) for i in range(3)]
service = AssignmentServiceWithLoad(agents)
orders = [Order(i) for i in range(5)]
for order in orders:
    service.assign(order)
OutputSuccess
Alternatives
Round Robin Assignment
Assigns orders to agents in a fixed cyclic order without considering proximity or workload.
Use when: Use when agent workload is uniform and delivery area is small, making complex criteria unnecessary.
First-Come-First-Serve Assignment
Assigns orders to the first available agent regardless of location or load.
Use when: Use when order volume is low and agent availability is stable.
Auction-Based Assignment
Agents bid for orders based on their preferences and current status, and the system assigns based on bids.
Use when: Use when agents have varying preferences and incentives, and a market-driven approach is desired.
Summary
Delivery agent assignment automates matching orders to agents to prevent delays and overload.
It balances workload and considers factors like proximity to improve delivery speed.
This pattern is essential for systems with many agents and high order volumes.