This project is a Go-based load balancer designed to distribute incoming HTTP requests to a pool of backend servers based on their current load and health status. It uses internal structures for storing server state information such as load and health status, allowing for a robust and scalable architecture.
- Dynamic Load Balancing: Distributes requests to the least loaded server.
- Health Checks: Regularly checks the health of each server and only routes traffic to healthy servers.
- Graceful Shutdown: Ensures that ongoing requests are handled before shutting down the load balancer.
- Configuration via JSON: Server details and settings are loaded from a JSON configuration file.
- Detailed Logging: Provides comprehensive logging for monitoring and debugging.
- Go 1.16 or later
- Clone the Repository:
git clone https://github.com/itsfuad/LoadBalancer.git
cd LoadBalancer
- Build the Load Balancer:
go build -o LoadBalancer main.go
1. Create a JSON Configuration File: Create a servers.json file in the root directory with the following structure:
{
"load_balancer": {
"port": 8080, // Port on which the load balancer will listen
"health_check_interval_seconds": 10 // Interval in seconds for health checks
},
"servers": {
"urls": [
"http://localhost:9001", // server 1
"http://localhost:9002", // server 2
"http://localhost:9003" // server 3
]
}
}
- port: The port on which the load balancer will listen.
- health_check_interval_seconds: Interval in seconds for health checks.
- urls: List of backend server URLs.
2. Running the Load Balancer: Start the load balancer by executing:
./LoadBalancer
The load balancer will start on the specified port and begin routing traffic to the least loaded server.
Sending Requests: Send HTTP requests to the load balancer's address (e.g., http://localhost:8080). The load balancer will forward the request to the backend server with the least load.
Server Health Check:
The load balancer periodically checks each server's health by sending a GET
request to the /
root endpoint of each server. If a server does not respond with a status code of 200, it is marked as unhealthy and temporarily removed from the load balancer's pool.
For development mode the code uses
/
root path. But it is a good choice to have a separate end point like/health
in the production which returns small amount of response data which is more efficient that a root path which may return large data. Graceful Shutdown: The load balancer handles OS signals (SIGINT, SIGTERM) to perform a graceful shutdown, allowing in-progress requests to complete before terminating.
You can extend this load balancer by:
- Adding more sophisticated load balancing algorithms.
- Implementing server auto-scaling features.
- Adding support for HTTPS requests.
- Creating a user interface for monitoring server status and load in real-time.
Feel free to contribute by opening issues or submitting pull requests. Make sure to follow the existing coding style and include relevant tests for any new features or bug fixes.
This project is licensed under the GNU License. See the LICENSE file for more information.