A simple load balancer implemented in Go that routes incoming HTTP requests to multiple backend servers. This project serves as an educational example and is not production-ready.
- Least Connections Algorithm for Load Balancing
- Random Algorithm for Load Balancing
- Weighted Random Algorithm for Load Balancing
- Dynamic Weighted Random Algorithm for Load Balancing
- Least Response Time Algorithm for Load Balancing
- Atomic updates for active connection counts
- Simple HTTP backend servers for demonstration
- Supports multiple backend servers
- Thread-safe operations
- Go 1.20.5
git clone https://github.com/araujo88/simple-load-balancer
cd simple-load-balancer
Navigate to the /server
directory:
cd server
Run the example servers on ports 8081 and 8082:
go run main.go 8081
go run main.go 8082
Navigate back to the root directory and start the load balancer:
cd ..
go run . -algorithm=<algorithm-type>
Your load balancer should now be running and forwarding incoming HTTP requests to the backend servers.
main.go
: The load balancer code./server/main.go
: Example HTTP server code.
This load balancer supports multiple algorithms for distributing incoming HTTP requests among backend servers. Below are the algorithms implemented:
In the Least Connections method, incoming requests are routed to the server with the fewest active connections. This helps ensure a more equitable distribution of load.
The Random method randomly selects a backend server for each incoming request. All servers have an equal chance of being chosen, regardless of their current load or performance.
The Weighted Random method assigns a static weight to each backend server. The probability of selecting a particular server is proportional to its weight. Servers with higher weights will receive more requests than those with lower weights.
The Least Response Time algorithm selects the server that has the lowest response time for a new request. Implementing this algorithm would involve measuring the response time of each server and directing incoming requests to the server with the least recent response time.
This is an extension of the Weighted Random algorithm. In this method, the weight of each server is determined dynamically based on the inverse of its current number of active connections. This allows the load balancer to adapt to the real-time load on each server.
Similar to Dynamic Weighted Random, but including the server response time as an additional parameter.
- Fork the repository
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request
This project is open source and available under the GNU GENERAL PUBLIC LICENSE.
- Incorporate moving averages to measure response time
- Handle failed requests
- Incorporate health checks
- Measure CPU and disk usage
- Metrics and logging
- Rate limiting
- Connection draining
- Circuit breaker pattern
- Sticky sessions
- DDoS Protection
- Response Caching
- IPv6 Support
- WebSockets Support
- API for Management
- Automated Tests