|
| 1 | +## Load Balancing Strategies |
| 2 | + |
| 3 | +The `llm-router` supports various strategies for selecting the most suitable provider |
| 4 | +when multiple options exist for a given model. This ensures efficient |
| 5 | +and reliable routing of requests. The available strategies are: |
| 6 | + |
| 7 | +### 1. `balanced` (Default) |
| 8 | + |
| 9 | +* **Description:** This is the default strategy. It aims to distribute requests |
| 10 | + evenly across available providers by keeping track of how many times each provider has |
| 11 | + been used for a specific model. It selects the provider that has been used the least. |
| 12 | +* **When to use:** Ideal for scenarios where all providers are considered equal |
| 13 | + in terms of capacity and performance. It provides a simple and effective way to balance the load. |
| 14 | +* **Implementation:** Implemented in `llm_router_api.base.lb.balanced.LoadBalancedStrategy`. |
| 15 | + |
| 16 | +### 2. `weighted` |
| 17 | + |
| 18 | +* **Description:** This strategy allows you to assign static weights to providers. |
| 19 | + Providers with higher weights are more likely to be selected. The selection is deterministic, |
| 20 | + ensuring that over time, the request distribution closely matches the configured weights. |
| 21 | +* **When to use:** Useful when you have providers with different capacities or performance |
| 22 | + characteristics, and you want to prioritize certain providers without needing dynamic adjustments. |
| 23 | +* **Implementation:** Implemented in `llm_router_api.base.lb.weighted.WeightedStrategy`. |
| 24 | + |
| 25 | +### 3. `dynamic_weighted` (beta) |
| 26 | + |
| 27 | +* **Description:** An extension of the `weighted` strategy. It not only uses weights |
| 28 | + but also tracks the latency between successive selections of the same provider. |
| 29 | + This allows for more adaptive routing, as providers with consistently high latency |
| 30 | + might be de-prioritized over time. You can also dynamically update provider weights. |
| 31 | +* **When to use:** Recommended for dynamic environments where provider performance |
| 32 | + can fluctuate. It offers more sophisticated load balancing by considering both |
| 33 | + configured weights and real-time performance metrics (latency). |
| 34 | +* **Implementation:** Implemented in `llm_router_api.base.lb.weighted.DynamicWeightedStrategy`. |
| 35 | + |
| 36 | +### 4. `first_available` |
| 37 | + |
| 38 | +* **Description:** This strategy selects the very first provider that is available. |
| 39 | + It uses Redis to coordinate across multiple workers, ensuring that only one |
| 40 | + worker can use a specific provider at a time. |
| 41 | +* **When to use:** Suitable for critical applications where you need the fastest |
| 42 | + possible response and want to ensure that a request is immediately handled by any available |
| 43 | + provider, without complex load distribution logic. It guarantees that a provider, |
| 44 | + once taken, is exclusive until released. |
| 45 | +* **Implementation:** Implemented in `llm_router_api.base.lb.first_available.FirstAvailableStrategy`. |
| 46 | + |
| 47 | +**When using the** `first_available` load balancing strategy, a **Redis server is required** |
| 48 | +for coordinating provider availability across multiple workers. |
| 49 | + |
| 50 | +### 4. `first_available_optim` |
| 51 | +**UNDER DEVELOPMENT, DESCRIPTION WILL BE SOON** |
| 52 | + |
| 53 | +The connection details for Redis can be configured using environment variables: |
| 54 | + |
| 55 | +```shell |
| 56 | +LLM_ROUTER_BALANCE_STRATEGY="first_available" \ |
| 57 | +LLM_ROUTER_REDIS_HOST="your.machine.redis.host" \ |
| 58 | +LLM_ROUTER_REDIS_PORT=redis_port \ |
| 59 | +``` |
| 60 | + |
| 61 | +**Installing Redis on Ubuntu** |
| 62 | + |
| 63 | +To install Redis on an Ubuntu system, follow these steps: |
| 64 | + |
| 65 | +1. **Update package list:** |
| 66 | + |
| 67 | +```shell |
| 68 | +sudo apt update |
| 69 | +``` |
| 70 | + |
| 71 | +2. **Install Redis server:** |
| 72 | + |
| 73 | +```shell |
| 74 | +sudo apt install redis-server |
| 75 | +``` |
| 76 | + |
| 77 | +3. **Start and enable Redis service:** |
| 78 | + The Redis service should start automatically after installation. |
| 79 | + To ensure it's running and starts on system boot, you can use the following commands: |
| 80 | + |
| 81 | +``` shell |
| 82 | +sudo systemctl status redis-server |
| 83 | +sudo systemctl enable redis-server |
| 84 | +``` |
| 85 | + |
| 86 | +4. **Configure Redis (optional):** |
| 87 | + The default Redis configuration (`/etc/redis/redis.conf`) is usually sufficient |
| 88 | + to get started. If you need to adjust settings (e.g., address, port), |
| 89 | + edit this file. After making configuration changes, restart the Redis server: |
| 90 | + |
| 91 | +```shell |
| 92 | +sudo systemctl restart redis-server |
| 93 | +``` |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## Extending with Custom Strategies |
| 98 | + |
| 99 | +To use a different strategy (e.g., round‑robin, random weighted, latency‑based), |
| 100 | +implement `ChooseProviderStrategyI` and pass the instance to `ProviderChooser`: |
| 101 | + |
| 102 | +``` python |
| 103 | +from llm_router_api.base.lb.chooser import ProviderChooser |
| 104 | +from my_strategies import RoundRobinStrategy |
| 105 | + |
| 106 | +chooser = ProviderChooser(strategy=RoundRobinStrategy()) |
| 107 | +``` |
| 108 | + |
| 109 | +The rest of the code – `ModelHandler`, endpoint implementations, etc. – will |
| 110 | +automatically use the chooser you provide. |
0 commit comments