Overview
Warehouse selection strategies determine how the system chooses which warehouse to assign products from when fulfilling store demands. The application provides two built-in strategies and supports custom strategy implementation.Configuration
The warehouse selection strategy is configured inapplication.properties:
distanceOnlyStrategydistanceWithToleranceStrategy
Available Strategies
Distance Only Strategy
Bean Name:distanceOnlyStrategy
Description: Orders warehouses solely by geographic distance from the store using the Haversine formula.
Implementation:
- Minimize transportation costs
- Reduce delivery times
- Simplest strategy for predictable demand patterns
- When all warehouses have similar stock levels
- Simple and predictable behavior
- Minimizes transportation distance
- Easy to understand and explain
- Fast computation
- Ignores stock availability at closer warehouses
- May deplete nearby warehouse stock quickly
- Doesn’t balance warehouse utilization
- Can lead to more unfulfilled demands
Distance With Tolerance Strategy
Bean Name:distanceWithToleranceStrategy
Description: Orders warehouses considering both distance and product availability. If two warehouses are within a distance tolerance (10%), prioritizes the warehouse with more stock for the specific product and size.
Implementation:
- Balance between distance and stock availability
- Reduce unfulfilled demands
- Better warehouse utilization
- When warehouses have varying stock levels
- Reduces unfulfilled demands
- Better stock distribution across warehouses
- Balances transportation costs with fulfillment rates
- Considers product-specific availability
- Slightly more complex logic
- May result in longer average distances
- Requires real-time stock information
- 10% tolerance is fixed (not configurable)
Strategy Comparison
| Feature | Distance Only | Distance With Tolerance |
|---|---|---|
| Primary Factor | Distance | Distance + Stock |
| Stock Awareness | No | Yes |
| Distance Tolerance | N/A | 10% |
| Computation Cost | Low | Medium |
| Fulfillment Rate | Lower | Higher |
| Average Distance | Minimized | Slightly higher |
| Warehouse Utilization | Unbalanced | Balanced |
| Best For | Cost minimization | Fulfillment optimization |
Distance Tolerance Explained
The 10% distance tolerance works as follows: Example 1: Warehouses are significantly different distances- Warehouse A: 50 km away
- Warehouse B: 70 km away
- Difference: 20 km (40% of 50 km) → Significant difference
- Result: Choose Warehouse A (closer)
- Warehouse A: 50 km away, 10 units in stock
- Warehouse B: 54 km away, 100 units in stock
- Difference: 4 km (8% of 50 km) → Within tolerance
- Result: Choose Warehouse B (more stock)
Creating Custom Strategies
You can implement custom warehouse selection strategies by following these steps:1. Implement the Interface
Create a class that implementsWarehouseSelectionStrategy:
2. Register as Spring Bean
Ensure your strategy is annotated with@Component and has a unique bean name:
3. Update Configuration
Change the strategy inapplication.properties:
4. Access Dependencies
Inject services via constructor injection:Custom Strategy Examples
Priority-Based Strategy
Prioritize specific warehouses based on business rules:Region-Based Strategy
Prefer warehouses in the same country or region:Cost-Optimized Strategy
Consider transportation costs with different rates per km:Testing Strategies
Create unit tests for your custom strategies:Best Practices
- Performance: Keep strategy logic efficient as it runs for every product-store combination
- Consistency: Return a consistent ordering for the same inputs
- Null Safety: Handle edge cases like empty warehouse lists or missing coordinates
- Distance Calculation: Reuse
GeoDistanceServicefor consistent distance calculations - Documentation: Document your strategy’s behavior and use cases
- Testing: Write comprehensive unit tests covering edge cases
- Monitoring: Log strategy decisions for debugging and optimization
Switching Strategies
To switch strategies at runtime:- Update Configuration: Modify
application.properties - Restart Application: Required to load the new strategy bean
- Verify: Check logs for strategy initialization messages
- Monitor: Observe metrics to evaluate strategy effectiveness
Strategy Performance Impact
The strategy selection affects:- Fulfillment Rate: Percentage of demands successfully fulfilled
- Average Distance: Mean distance between warehouses and stores
- Transportation Cost: Total shipping costs
- Warehouse Utilization: Balance of stock across warehouses
- Processing Time: Computation time for distribution algorithm