0
0
LLDsystem_design~7 mins

Inventory management in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
Without a proper inventory management system, businesses face stockouts or overstock situations that lead to lost sales or increased holding costs. Manual tracking causes errors and delays, making it hard to respond quickly to demand changes or supply disruptions.
Solution
An inventory management system tracks stock levels, orders, sales, and deliveries in real time. It updates inventory counts automatically when items are added or sold, enabling accurate forecasting and timely replenishment. This system helps maintain optimal stock levels and reduces human errors.
Architecture
User System
Inventory API
Supplier API
Notification

This diagram shows how user requests flow through the Inventory API to the database, while the Order Service manages supplier interactions and notifications.

Trade-offs
✓ Pros
Automates stock tracking, reducing human errors.
Improves demand forecasting with real-time data.
Enables timely reorder alerts to prevent stockouts.
Supports integration with suppliers and sales channels.
✗ Cons
Requires initial setup and integration effort.
Complexity increases with multiple warehouses or SKUs.
Real-time updates may add load to the database.
Use when managing hundreds or more SKUs with frequent sales and replenishment cycles, especially if manual tracking causes errors or delays.
Avoid if inventory is very small (under 50 SKUs) and changes infrequently, where manual tracking is simpler and cost-effective.
Real World Examples
Amazon
Amazon uses inventory management to track millions of products across warehouses, ensuring fast fulfillment and accurate stock levels.
Shopify
Shopify provides inventory management tools to merchants to synchronize stock across online and physical stores.
Walmart
Walmart uses inventory management systems to optimize stock levels and automate replenishment across its vast retail network.
Code Example
The before code shows manual stock decrement without alerts. The after code adds automatic stock updates and triggers reorder alerts when stock falls below a threshold, improving reliability and responsiveness.
LLD
### Before: No inventory management, manual stock update
class Store:
    def __init__(self):
        self.stock = {}

    def sell_item(self, item, quantity):
        if item in self.stock and self.stock[item] >= quantity:
            self.stock[item] -= quantity
            print(f"Sold {quantity} of {item}")
        else:
            print("Not enough stock")

### After: Inventory management with automatic stock update and reorder alert
class InventoryManager:
    def __init__(self, reorder_threshold=5):
        self.stock = {}
        self.reorder_threshold = reorder_threshold

    def add_stock(self, item, quantity):
        self.stock[item] = self.stock.get(item, 0) + quantity

    def sell_item(self, item, quantity):
        if self.stock.get(item, 0) >= quantity:
            self.stock[item] -= quantity
            print(f"Sold {quantity} of {item}")
            self.check_reorder(item)
        else:
            print("Not enough stock")

    def check_reorder(self, item):
        if self.stock.get(item, 0) <= self.reorder_threshold:
            print(f"Reorder alert: {item} stock is low ({self.stock[item]})")
OutputSuccess
Alternatives
Manual Inventory Tracking
Relies on human input and spreadsheets instead of automated systems.
Use when: Choose when inventory is very small and changes are rare, making automation unnecessary.
Periodic Inventory System
Updates inventory counts at fixed intervals rather than real-time.
Use when: Choose when real-time accuracy is not critical and cost savings are prioritized.
Summary
Inventory management prevents stockouts and overstock by tracking stock levels automatically.
It improves accuracy and responsiveness by updating inventory in real time and alerting for reorders.
This system is essential for businesses with many products and frequent sales to optimize operations.