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
- 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-balancerNavigate to the /server directory:
cd serverRun the example servers on ports 8081 and 8082:
go run main.go 8081
go run main.go 8082Navigate back to the root directory and start the load balancer:
cd ..
go run main.goYour 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.
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.
Each algorithm has its own advantages and trade-offs, making them suitable for different kinds of applications and scenarios. You can select the algorithm that best fits your specific use case.
- 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.