An HTTP load balancer built using Go and the Gin framework, with support for multiple balancing strategies(round-robin, least connections, and sticky sessions).
- Balancing algorithms
- Round-robin
- Least connections
- Sticky sessions
- Backend Health Checks - Monitors backend health at regular intervals
- TLS Encryption - Secure communication between the clients and the load balancer
- Metrics Monitoring - Tracks incoming requestsand backend performance
- Prerequisites
- Go 1.20+
- OpenSSL(to generate certificates)
- Clone the Repository
git clone https://github.com/lokeshllkumar/load-balancer.git
cd load-balancer
- Install the Required Dependencies
go mod tidy
- Generate TLS Certificates
- Create a directory called
certs
to store the certificate - Generate an RSA key
openssl gen rsa -out key.pem 2048
- Generate a CSR file to create a Certificate
openssl req -new -key key.pem -out server.csr
- Generate the Self-Signed Certificate
openssl x509 -req -days 365 -in server.csr -signkey key.pem -out cert.pem
- Verify the Certificate
openssl x509 -in cert.pem -text -noout
- Create a directory called
- Build the Project
go build -o load-balancer .
- Run the server
./load-balancer
- Health Check Endpoint
- Check if the server is running
curl https://localhost:843/health --insecure
- Add a Backend Server
- Add a backend server dynamically
curl -X POST https://localhost:8443/api/backends -d '{"url": "https://backend3:8082"}' -H "Content-Type: application/json" --insecure
- List All Backends
curl https://localhost:8443/api/backends --insecure
- Round Robin
- Distributes requests evenly across all available backends.
- Least Connections
- Directs traffic to the backend with the fewest active connections.
- Sticky Sessions
- Ensures requests from the same client are directed to the same backend.