System design · LLD design

Inventory Management System

FR

1. We have a multi warehouse system which contains multiple product with its stock
2. Find product with a specific qty in warehouse with most stock for reservation and if not available then return a message “product not available”
3. Reserved order can be fulfilled or cancelled
4. remove from reserved state if no action happen within 15mins
4. Restock the product with specific qty to a specific warehouse
5. Trigger a notification if a product stock is less than threshold (keep threshold as configurable per product)

NFR

1.Scalable (data grow): system should handle addition of more warehouse/product
2. Extensible: find product logic should be swappable with other logic
3. Concurrency: Handle concurrent request for reservation of product coming from two different threads , also to handle concurrency between reservation vs reservation and restocking vs reservation
4.Scalable(expiry mechanism ): Expiry detection should be scale efficiently as active reservation grows

Entities and classes

Status: enum
RESERVED , FREE , SOLD, CANCELLED

Product:
-productId : int
-productName : String
-quantity : int
-quantityThreshold : int
-warehouseID : int

+Product(workflowID , productname , quantity)
+addProduct(quantity) : Product - synchronized
+removeProduct(quantity) - synchronized

Warehouse:
-warehouseId : int
-Map<Integer, Product> products

+addProduct(productName, qty)
Find product in list and if not present add a new product

+getProduct(workflowId)

Booking:
-reservationID : int
-status: Status
-expiryTime: epochTime
-productId: int
-qty : int
-warehouseId : int

+reserveOrder(productId, qty, warehouseID)
Create Booking with status RESERVED
Set expiryTime
removeProduct(qty)

+cancelOrder() - need this to be synchronized as order can actually be cancelled or can be cancelled due to expiry time
status change to CANCELLED
call addProduct(qty)

+confirmOrder() -need this to be synchronized
Status change to SOLD

+expiryTimeHandler() : Boolean
- now > expiryTime call cancelOrder()

ProductFindStrategy - interface
WarehouseWithMostStockStrategy - implements ProductFindStrategy : warehouse
- in this if nothing found send a message “No Product available”

InventoryManagementSystem
- strategy : WarehouseWithMostStockStrategy
-ConcurrentHashMap<String, Booking> activeReservedOrders
-List<Warehouse> warehouse

+reserveOrder(productid, qty)
optional<Warehouse> find using strategy and when found reserveOrder()
Add in activeReservedOrders

+bookOrder(reservationID)
Remove from activeReservedOrders
confirmOrder()
+cancelOrder()
Remove from activeReservedOrders

+checkExceedReservedOrders()
if true remove from activeReservedOrders

+restockOrder(wareshouseId, productname , qty)
First warehouse.get(warehouseid) : Optional<Product> p

+addWarehouse()

+addNewProduct(warehouseId , productname , qty )
warehouse.addProduct()