# AVS Load Balancer

#### How AVS Load Balancer Works

The **AVS Load Balancer** component within the AVS-SDK is designed to manage and distribute requests across multiple nodes effectively. It continuously monitors the health of these nodes and ensures that requests are routed to the most optimal node based on its health status. This guide introduces the AVS Load Balancer setup and functionality, ensuring your application maintains high availability and performance.

***

#### Importing and Using AVS Load Balancer

To incorporate **AVS Load Balancer** into your application, begin by importing it from the AVS-SDK. Here’s an example of how to do this:

```javascript
const AVSLoadBalancer = require('avs-sdk').AVSLoadBalancer; // Import AVSLoadBalancer from avs-sdk
```

After importing, create an instance of AVSLoadBalancer to manage your nodes and load balancing.

***

#### AVS Load Balancer: Functions and Usage Overview

The AVS Load Balancer module in the AVS-SDK provides essential tools to register nodes, monitor their health, and route requests optimally. Here’s a breakdown of the available functions and how they can be used for effective load management.

***

#### Available Functions

**Constructor - `AVSLoadBalancer()`**

* **Purpose**: Initializes an instance of AVSLoadBalancer that manages registered nodes and their health checks.
* **Usage**: Create an instance to start registering nodes and monitoring their health.
*   **Example**:

    ```javascript
    const loadBalancer = new AVSLoadBalancer();
    ```

    Here, `loadBalancer` is an instance of AVSLoadBalancer ready to manage node health and request distribution.

***

**Register Node - `registerNode(nodeId, endpoint)`**

* **Purpose**: Registers a new node with a unique identifier (`nodeId`) and its corresponding endpoint URL.
* **Usage**: Call `registerNode` to add nodes to the load balancer for health monitoring and load distribution.
*   **Example**:

    ```javascript
    loadBalancer.registerNode('node1', 'https://node1.example.com');
    ```
* **What to Expect**: This function adds the node to the load balancer and initiates health checks at the specified endpoint.

***

**Start Health Check - `startHealthCheck(nodeId)`**

* **Purpose**: Initiates periodic health checks for the registered node to monitor its status.
* **Usage**: Automatically called when registering a new node.
*   **Example**:

    ```javascript
    // Called within registerNode, no direct usage needed
    loadBalancer.registerNode('node2', 'https://node2.example.com');
    ```
* **What to Expect**: Health checks are performed every 30 seconds. The node's health is updated based on the response received from the endpoint.

***

**Update Node Health - `updateNodeHealth(nodeId, health)`**

* **Purpose**: Updates the health status of a registered node based on the result of the health check.
* **Usage**: This function is called internally during health checks and updates the node’s health and last check time.
*   **Example**:

    ```javascript
    loadBalancer.updateNodeHealth('node1', 75); // Updates the health of node1
    ```
* **What to Expect**: The health value is updated (0-100), and the timestamp of the last check is recorded.

***

**Get Optimal Node - `getOptimalNode()`**

* **Purpose**: Returns the node with the highest health score, which is considered the optimal node for routing requests.
* **Usage**: Call `getOptimalNode` when you need to determine which node to route a request to.
*   **Example**:

    ```javascript
    const optimalNode = loadBalancer.getOptimalNode();
    console.log('Optimal Node:', optimalNode); // Outputs the node with the highest health
    ```
* **What to Expect**: This function scans all registered nodes and returns the one with the highest health. If all nodes are unhealthy, it may return `null`.

***

#### Putting It All Together

With AVS Load Balancer, you can efficiently manage and distribute requests to your nodes while ensuring high availability. Here’s a full example demonstrating the usage of AVS Load Balancer functions:

```javascript
const AVSLoadBalancer = require('avs-sdk').AVSLoadBalancer;

const loadBalancer = new AVSLoadBalancer();

// Register nodes
loadBalancer.registerNode('node1', 'https://node1.example.com');
loadBalancer.registerNode('node2', 'https://node2.example.com');

setTimeout(async () => {
    const optimalNode = loadBalancer.getOptimalNode(); // Get the optimal node based on health
    if (optimalNode) {
        console.log('Routing request to:', optimalNode.nodeId);
    } else {
        console.log('No healthy nodes available.');
    }
}, 60000); // Wait 60 seconds for health checks to update
```

#### Summary of Functionality

* **`AVSLoadBalancer()`**: Initializes an AVSLoadBalancer instance for managing node health and load distribution.
* **`registerNode(nodeId, endpoint)`**: Registers a new node and starts health monitoring.
* **`startHealthCheck(nodeId)`**: Begins periodic health checks on the registered node.
* **`updateNodeHealth(nodeId, health)`**: Updates the health status of a registered node based on health check results.
* **`getOptimalNode()`**: Retrieves the node with the highest health score for request routing.

The AVS Load Balancer module is essential for maintaining high availability and efficient resource utilization in your decentralized system, enabling seamless management of multiple nodes.
