This project is a simple load balancer built using Node.js and Express. It distributes incoming HTTP requests to multiple backend servers, ensuring that the traffic is balanced and system resources are used efficiently.
- Distributes requests between multiple backend servers.
- Performs health checks to ensure that only healthy servers are included in the load balancing.
- Automatically removes unhealthy servers from the list of available servers.
- Uses a round-robin strategy to distribute the traffic evenly across servers.
- Backend Servers: The load balancer keeps a list of available backend servers.
- Round-Robin Strategy: Requests are forwarded to the backend servers in a cyclic manner (i.e., each incoming request is forwarded to the next server in line).
- Health Checks: Every few seconds, the load balancer checks the health of the backend servers by sending a request to the
/health
endpoint of each server.- If a server responds with a status code 200, it is considered healthy and remains in the list.
- If a server is unhealthy or unresponsive, it is removed from the list.
- Server Addition: When a previously removed server becomes healthy again, it is re-added to the pool of available servers.
-
Clone the repository:
git clone https://github.com/Lalit9025/Load-Balancer.git cd Load-Balancer
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory to configure the port for the load balancer:PORT=80
-
Start the load balancer:
npm run start
-
Start the backend servers (
server1
andserver2
) on their respective ports (8081
,8082
).
Once the load balancer is running, it will start distributing requests to backend servers. You can test it by sending requests to the load balancer's endpoint (e.g., http://localhost
).
curl http://localhost