A lightweight, high-performance HTTP load balancer implemented in Go using the round-robin algorithm. This load balancer distributes incoming HTTP requests across multiple backend servers to optimize resource utilization and improve application reliability.
- Round-Robin Algorithm: Evenly distributes requests across all available backend servers
- Reverse Proxy: Built on Go's
httputil.ReverseProxyfor efficient request forwarding - Health Check Ready: Infrastructure for server health monitoring (extensible)
- Simple Configuration: Easy to add or remove backend servers
- Zero External Dependencies: Uses only Go standard library
- Go 1.25.4 or higher
- Basic understanding of HTTP and load balancing concepts
- Clone the repository:
git clone https://github.com/yourusername/go-loadbalancer.git
cd go-loadbalancer- Initialize Go modules (if needed):
go mod downloadcd src
go run main.goThe load balancer will start on localhost:8000 by default.
Open your browser or use curl to test:
curl http://localhost:8000Each request will be forwarded to a different backend server in a round-robin fashion.
To modify the backend servers, edit the servers slice in main.go:
servers := []Server{
newSimpleServer("https://www.google.com"),
newSimpleServer("https://www.bing.com"),
newSimpleServer("https://www.duckduckgo.com"),
}To change the port:
lb := NewLoadBalancer("8000", servers) // Change "8000" to your desired port-
Server Interface: Defines the contract for backend servers
Address(): Returns the server addressIsAlive(): Checks server health statusServe(): Handles the request forwarding
-
simpleServer: Concrete implementation of the Server interface
- Wraps
httputil.ReverseProxyfor request forwarding
- Wraps
-
LoadBalancer: Core load balancing logic
- Maintains a list of backend servers
- Implements round-robin selection algorithm
- Routes incoming requests to available servers
Client Request
↓
Load Balancer (localhost:8000)
↓
Round-Robin Selection
↓
Backend Server (Reverse Proxy)
↓
Response to Client
- Client Request: A client sends an HTTP request to the load balancer
- Server Selection: The load balancer uses round-robin algorithm to select the next available server
- Request Forwarding: The request is forwarded to the selected backend server via reverse proxy
- Response: The backend server's response is sent back to the client
go-loadbalancer/
├── go.mod # Go module definition
├── README.md # Project documentation
└── src/
└── main.go # Main application code
Add Health Checks: Implement actual health checking in the IsAlive() method:
func (s *simpleServer) IsAlive() bool {
resp, err := http.Head(s.addr)
if err != nil {
return false
}
return resp.StatusCode == http.StatusOK
}Add Different Load Balancing Algorithms: Implement weighted round-robin, least connections, or IP hash algorithms.
Add Logging: Integrate a logging framework for better observability.
Add Metrics: Implement request counting, latency tracking, and server performance metrics.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is open source and available under the MIT License.
Your Name - @Tanishq4501
- Built with Go's standard library
- Inspired by production load balancers like NGINX and HAProxy
