local-load-balancer v1.0.1
# Local Load Balancer
A Node.js-based load balancer for local testing that supports both Round Robin and Weighted Round Robin strategies. This module allows you to set up multiple servers on local ports and distribute incoming HTTP requests to them efficiently.
## Table of Contents
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [1. Create a Load Balancer Instance](#1-create-a-load-balancer-instance)
- [2. Start Your Servers](#2-start-your-servers)
- [3. Request Distribution](#3-request-distribution)
- [4. Monitoring Server Performance](#4-monitoring-server-performance)
- [Configuration](#configuration)
- [numServers](#numservers)
- [portRange](#portrange)
- [balanceStrategy](#balancestrategy)
- [serverWeights](#serverweights)
- [Example Configuration](#example-configuration)
- [Metrics Logging](#metrics-logging)
- [Troubleshooting](#troubleshooting)
- [Port Already in Use](#port-already-in-use)
- [Missing Dependencies](#missing-dependencies)
- [Error Fetching CPU/Memory Stats](#error-fetching-cpumemory-stats)
- [Contributing](#contributing)
- [Author](#author)
- [Keywords](#keywords)
## Features
- **Round Robin**: Distributes requests evenly across all servers.
- **Weighted Round Robin**: Allows for weighting servers, giving more traffic to certain servers based on weights.
- **Real-time Metrics**: Logs the number of requests handled by each server every 10 seconds.
- **CPU & Memory Monitoring**: Monitors the CPU and memory usage of each server at regular intervals.
## Installation
### Install Dependencies
Make sure to install required dependencies by running:
```bash
npm install local-load-balancer
Usage
1. Create a Load Balancer Instance
To create a load balancer and specify your configurations, you need to import the module and configure it according to your needs.
const LocalLoadBalancer = require("local-load-balancer");
const config = {
numServers: 4, // Number of backend servers to create
portRange: { start: 3000, end: 3003 }, // Port range for the servers
balanceStrategy: "weightedRoundRobin", // or 'roundRobin'
serverWeights: [1, 2, 3, 4], // Weight for each server (only for weightedRoundRobin)
};
const loadBalancer = new LocalLoadBalancer(config);
// Start the load balancer
loadBalancer.start();
2. Start Your Servers
Once the load balancer is running, it will create and start backend servers at ports within the specified range. Requests sent to the load balancer will be routed to these servers according to the balancing strategy.
3. Request Distribution
The load balancer will distribute requests as follows:
- Round Robin: Distributes requests evenly across all servers.
- Weighted Round Robin: Requests will be distributed based on the weights defined in
serverWeights
.
4. Monitoring Server Performance
Your load balancer will automatically log:
- The number of requests handled by each server every 10 seconds.
- The CPU and memory usage of each server process every 5 seconds.
These logs will help you monitor the health and load distribution of your backend servers.
Configuration
The load balancer can be configured via the config
object passed to the LocalLoadBalancer
constructor. Here are the available options:
Option | Type | Description | Default |
---|---|---|---|
numServers | Number | The number of backend servers to create. | 4 |
portRange | Object | The range of ports to assign to the backend servers. | { start: 3000, end: 3003 } |
balanceStrategy | String | The strategy to use for load balancing. Can be either roundRobin or weightedRoundRobin . | roundRobin |
serverWeights | Array | An array of weights for each backend server (used with weightedRoundRobin ). If no weights are provided, all servers will have equal weight (1). | [] (equal weight for all servers) |
Example Configuration
const config = {
numServers: 3,
portRange: { start: 4000, end: 4002 },
balanceStrategy: "weightedRoundRobin",
serverWeights: [2, 1, 3],
};
const loadBalancer = new LocalLoadBalancer(config);
loadBalancer.start();
This configuration will:
- Create 3 servers on ports 4000 to 4002.
- Use a weighted round-robin strategy to distribute requests with weights
[2, 1, 3]
.
Metrics Logging
Every 10 seconds, the load balancer will log the following information for each server:
- Number of requests handled.
- CPU and memory usage.
Example Output:
Metrics after 10 seconds:
Server on port 3000 handled 50 requests
Server on port 3001 handled 40 requests
Server on port 3002 handled 30 requests
------------------------------
CPU and Memory Stats Example:
Server on port 3000 CPU Usage:
- CPU: 12.5%
- Memory: 32.45 MB
Troubleshooting
Port Already in Use
If a port is already in use when starting the servers, the load balancer will log an error, and you will need to change the port range or ensure that the ports are free.
Missing Dependencies
Ensure that all dependencies are installed. If you encounter errors related to missing packages, try running:
npm install
Error Fetching CPU/Memory Stats
If you get an error while fetching CPU or memory stats, ensure that the server process is running and accessible. This module uses the pidusage
library to get stats, so make sure it's installed.
Contributing
Feel free to open issues or submit pull requests! Here are a few ways you can contribute:
- Bug reports: Open an issue if you encounter bugs.
- New features: Feel free to add features, such as more load balancing strategies or extended monitoring capabilities.
- Documentation: Improve the README or add more examples for better clarity.
Author
Vipul Jain
Email: jain.vipul3108@gmail.com
Keywords
- load balancing
- proxy
- node
- server