Rotabalance is a lightweight and efficient load balancer written in Go. It uses a thread-safe round-robin algorithm to evenly distribute traffic across backend servers while dynamically handling server availability.
With simplicity and high performance as its core principles, Rotabalance is perfect for developers who need a reliable load-balancing solution without the complexity of heavyweight tools.
- Round-Robin Load Balancing: Distributes requests evenly across available servers.
- Health Checks: Automatically skips unhealthy servers during traffic distribution.
- Failover Support: Ensures high availability by dynamically handling server outages.
- Concurrency-Safe: Uses Go's
atomicpackage for efficient and safe multi-threaded operation. - Configurable and Extendable: Easily add custom logic for routing and server selection.
- Go 1.18 or later installed on your machine.
- Basic understanding of Go modules.
- Clone the repository:
git clone https://github.com/yourusername/rotabalance.git
To get started, navigate to the Rotabalance project directory:
cd rotabalanceBuild the load balancer application using the following command:
go build -o rotabalanceExample Configuration To configure your backend servers, modify the main.go file as shown below:
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
// Backend servers
backends := []*Backend{
{URL: mustParseURL("http://localhost:8081"), Alive: true},
{URL: mustParseURL("http://localhost:8082"), Alive: true},
{URL: mustParseURL("http://localhost:8083"), Alive: true},
}
// Initialize ServerPool
serverPool := &ServerPool{backends: backends}
// Start the server
log.Println("Starting Rotabalance on :8080")
log.Fatal(http.ListenAndServe(":8080", serverPool))
}
func mustParseURL(rawURL string) *url.URL {
u, err := url.Parse(rawURL)
if err != nil {
log.Fatalf("Failed to parse URL: %v", err)
}
return u
}Run the load balancer application:
./rotabalanceSend traffic to the load balancer, and it will automatically distribute requests to the backend servers.
Rotabalance includes built-in health checks to ensure traffic is only routed to "alive" backend servers. Unavailable servers are automatically skipped until they are back online.
We welcome contributions from the community! If you have an idea, bug fix, or enhancement, feel free to submit an issue or pull request on the GitHub repository.
Fork the repository to your GitHub account. Create a new branch for your feature or fix:
git checkout -b feature-nameCommit your changes:
git commit -m "Add feature-name"Push your branch to your fork:
git push origin feature-nameOpen a pull request to the main repository.
This project is licensed under the MIT License. See the LICENSE file for details.
Rotabalance is inspired by the simplicity of round-robin algorithms and the power of Go's concurrency primitives. We extend special thanks to the open-source community for their invaluable contributions and support.
Happy balancing! 🚀