| Scale | Users (Orders/Day) | Delivery Agents | Key Changes |
|---|---|---|---|
| Small | 100 | 20 | Simple assignment logic, single server, direct DB queries |
| Medium | 10,000 | 2,000 | Introduce caching, load balancing, asynchronous assignment |
| Large | 1,000,000 | 200,000 | Microservices, sharded DB, geo-partitioning, message queues |
| Very Large | 100,000,000 | 20,000,000 | Global distributed system, advanced sharding, AI-based assignment, CDN for static data |
Delivery agent assignment in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
At small to medium scale, the database becomes the first bottleneck. This is because the system needs to quickly find available delivery agents and update their status for each order. The database handles many read and write queries per second, and as orders grow, it struggles to keep up.
- Database Read Replicas: Use replicas to handle read queries, reducing load on the primary database.
- Caching: Cache delivery agent availability and location data to reduce frequent DB hits.
- Load Balancing: Distribute incoming assignment requests across multiple application servers.
- Asynchronous Processing: Use message queues to decouple order intake from assignment processing.
- Sharding: Partition the database by geography or agent ID to spread load.
- Microservices: Separate assignment logic into dedicated services for better scalability.
- Geo-Partitioning: Assign orders to agents within the same region to reduce latency and data scope.
- AI-based Assignment: Use machine learning to optimize agent selection and reduce assignment time.
- At 10,000 orders/day (~7 orders/min), assuming each order triggers 5 DB queries, total ~35 queries/min (~0.6 QPS) - easily handled by a single DB.
- At 1,000,000 orders/day (~700 orders/min), queries rise to ~3500 QPS, nearing a single DB instance limit.
- Storage: Each order record ~1 KB, 1M orders/day = ~1 GB/day storage; 100M orders = 100 GB/day, requiring archival strategies.
- Network bandwidth: Assuming 1 KB per request/response, 1M orders/day = ~12 MB/s peak, manageable with standard infrastructure.
Start by clarifying the scale and requirements. Discuss the current bottleneck and how it changes with growth. Propose incremental solutions: caching, load balancing, then sharding and microservices. Always justify why each step is needed and how it helps scalability.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Introduce read replicas and caching to reduce load on the primary database before considering sharding or adding more servers.
Practice
What is the primary goal of a delivery agent assignment system?
Solution
Step 1: Understand the system purpose
The delivery agent assignment system focuses on connecting orders with delivery agents who can fulfill them.Step 2: Identify the main function
Matching orders to free agents nearby ensures timely delivery and efficient resource use.Final Answer:
Match orders to available delivery agents nearby -> Option AQuick Check:
Delivery agent assignment = Matching orders to agents [OK]
- Confusing delivery assignment with payment processing
- Thinking inventory management is part of agent assignment
- Assuming delivery charges calculation is the main goal
Which data structure is best to quickly find the nearest free delivery agent for an order?
Solution
Step 1: Identify the need for sorting by distance
To find the nearest free agent, sorting agents by their distance to the order location is essential.Step 2: Choose a data structure supporting efficient nearest retrieval
A priority queue can efficiently provide the closest agent by always giving the smallest distance first.Final Answer:
Priority queue sorted by distance from order location -> Option AQuick Check:
Nearest agent search = Priority queue [OK]
- Using hash map which doesn't sort by distance
- Using stack or linked list which are inefficient for nearest search
- Ignoring the need to sort by distance
Consider this pseudocode for assigning an agent:for agent in agents:
if agent.status == 'free' and distance(agent, order) < min_distance:
min_distance = distance(agent, order)
assigned_agent = agent
return assigned_agent
What will this code return if all agents are busy?
Solution
Step 1: Check variable initializations
The code does not initialize assigned_agent or min_distance before the loop.Step 2: Trace execution when all agents are busy
The if condition's first part (agent.status == 'free') fails for all agents, so due to short-circuit evaluation of 'and', the second part (distance < min_distance) is never evaluated. The loop ends without ever setting assigned_agent. Returning an uninitialized assigned_agent causes an error due to undefined variable.Final Answer:
An error due to undefined variable -> Option DQuick Check:
No initialization + all busy = undefined variable error [OK]
- Thinking assigned_agent defaults to None or null
- Assuming it returns the first agent regardless of status
- Believing the code handles no free agents gracefully
Given this snippet:assigned_agent = None
for agent in agents:
if agent.status = 'free':
assigned_agent = agent
return assigned_agent
What is the main error in this code?
Solution
Step 1: Check the if condition syntax
The condition uses '=' which assigns value instead of '==' which compares values.Step 2: Understand impact of wrong operator
This causes a syntax error or unintended behavior because '=' cannot be used in conditions.Final Answer:
Using assignment operator '=' instead of comparison '==' in if condition -> Option CQuick Check:
Use '==' for comparison, not '=' [OK]
- Confusing '=' with '==' in if statements
- Thinking assigned_agent must be initialized inside loop
- Assuming return inside loop is the error
You want to design a scalable delivery agent assignment system for a city with thousands of agents and orders per minute. Which approach best improves scalability?
Solution
Step 1: Understand scalability challenges
Checking all agents for every order is slow and resource-heavy at large scale.Step 2: Choose a partitioning strategy
Dividing the city into zones limits search space, making assignment faster and scalable.Step 3: Evaluate other options
Random or oldest agent assignment ignores location, causing delays and inefficiency.Final Answer:
Partition the city into zones and assign agents within zones only -> Option BQuick Check:
Zone partitioning = scalable assignment [OK]
- Using centralized server causing bottlenecks
- Ignoring agent location in assignment
- Assigning agents randomly or by registration time
