A simple HTTP load balancer built with Node.js and Express. It distributes incoming requests to multiple backend servers using a round-robin algorithm and performs periodic health checks to ensure requests are only sent to healthy servers.
- Round-robin load balancing across multiple backend servers
- Health checks for backend servers (via
/health
endpoint) - Automatic failover: unhealthy servers are skipped until healthy again
- Detailed logging of incoming requests and backend responses
- Handles all HTTP methods and paths
- Graceful shutdown on SIGINT
-
Clone the repository:
git clone https://github.com/mohanad-80/load-balancer.git cd load-balancer
-
Install dependencies:
npm install
Start multiple backend servers on different ports (e.g., 3001, 3002, 3003):
PORT=3001 node be.js
PORT=3002 node be.js
PORT=3003 node be.js
You can use multiple terminals or a process manager like pm2.
Start the load balancer (listens on port 3000 by default):
node lb.js
Send HTTP requests to the load balancer:
curl http://localhost:3000/
Requests will be forwarded to healthy backend servers in round-robin order.
lb.js
- Main load balancer logicbe.js
- Simple backend server for testingpackage.json
- Project metadata and dependencies
- Edit the
servers
array inlb.js
to add or remove backend servers. - Adjust health check interval or timeout as needed.
This project is licensed under the MIT License. See LICENSE for details.
Author: Mohanad Ahmed