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:
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:
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:
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:
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:
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:
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:
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.
Last updated