This is my submission for the assessment.Here i build the simple order processing system using the python that checks stocks and handles customers orders.
how to run
make sure we have the python instelled.Then just run : python3 order-system.py
MY Approach
I started by reading the problem and understand what exactly is the problem needed to happen.
The main things I figured out was that I should validate everythings first before touching any stocks so,if someone orders 3 itemns 3 items and 2 has a problem, nothing gets deducted at all. Either the whole order works or nothings does.
Handling the edge case
- Empty order - if someone sends an order with no items,it returns an error right away.
- Products not found - if the product_id in the order doesn't exits in our inventory, it returns an error with the id that was missing.
- Not enough stocks - if the quantity requested is more than what we have, it tells us exactly how much was requested and how much is available.
- Duplicate products in same order - if P1 appears twice in the same order we just add the quantities together before validation so it treats it as one combined request
Given, we have two warehouses and need to fulfill this order:
- P1 Shampoo - 8 units
- P2 Soap - 6 units
Current stock :
- Warehouse A : P1 = 5 units, P2 = 10 units
- Warehouse B : P1 = 10 units, P2 = 5 units
Our Allocation Plan for P1 Shampoo(need 8 units total):
- take 5 from Warehouse A(That everthings they have)
- take 3 from the Warehouse B
- Total = 8 units
for P2 Soap(need 6 units total):
- take 5 from Warehouse B
- Take 1 from Warehouse A to cover the rest Total = 6 units
so, the final plan :
- warehouse A ships -> 5 Shampoo and 1 soap
- Warehoue B ships -> 3 shampoo and 5 soap
Strategy used :-
We went with minimize shipment splits as our main priority.My thinking was that splitting a shipment across two warehouse already adds complexity so i wanted to empty one warehouse completely for each product before going to the next products.That keeps the logic simple and reduce the chance of coordinating error between warehouses.
Assumption I made
- Both warehouse can ship at the same time independently.
- Shipping cost is the same from both warehouse.
- Delivery time is roughly the same from both locations.
- stocks numbers shown are accurate and up to date.
Q1.Throughput Calculation How many orders per minute?
The system handles 5 orders at the same times and each order takes 2 seonds.
so, in 2 seconds it processes 5 orders. In 60 sec(1min) there are 60/2 = 30 batches.
30 batches * 5 orders = 150 orders per minutes.
Q2. Overload Scenario If orders suddenly spikes 300 per minutes,what happens to the system?Describes the effectd and the failure modes.
THe system can takes 150 orders in a minutes but now 300 are coming in so that means 150 orders per minutes are remaining or pilled up in the queue.
After that,the remaining orders gets bigger and some problem can been seen like ;
- memory fills up, Response time may get slow, system also may get crashed due to overloads.
Q3. Improvement Suggestion Suggest one concrete improvement that would allow the system to handle load.Explain why its helps?
We can do the horizontal scaling where we can runs multiple instances of the system with a load balancer in front that distributes incoming orders across all instances.
It can be helpful as, if we spin 3 instances,we can have the 450 orders per minutes capacity instead of the 150. this works because each orders is independent,so splitting the work across the multiple instances is effective and can perform smoothly.