Disclaimer: this project is a work in progress. I will be updating the readme as new features will be implemented.
This project implements a load balancer in Go with the following features:
- Request timeouts for backend servers
- Rate limiting using a token bucket algorithm
- Circuit breaker pattern for backend servers
- Prometheus metrics for monitoring
main.go
: Contains themain
function and initializes the load balancer.load_balancer.go
: Contains theLoadBalancer
struct and its methods.rate_limiter.go
: Contains theRateLimiter
struct and its methods.circuit_breaker.go
: Contains theCircuitBreaker
struct and its methods.logging.go
: Contains the logging function.metrics.go
: Contains the Prometheus metrics definitions and registration.
- Go 1.16 or later
- Prometheus
-
Clone the repository:
git clone https://github.com/alexovidiupopa/load-balancer.git cd load-balancer
-
Install dependencies:
go mod tidy
- Start the backend servers (e.g., on ports 8081, 8082, 8083).
go run backend/main.go 8081
go run backend/main.go 8082
go run backend/main.go 8083
-
Run the load balancer:
go run lb/main.go
-
The load balancer will start on port 8080.
- The metrics are exposed at
/metrics
endpoint. - Configure Prometheus to scrape the metrics from the load balancer.
The load balancer uses a custom http.Transport
with timeouts for dialing and TLS handshake.
The RateLimiter
struct implements a token bucket algorithm to limit the rate of incoming requests.
The CircuitBreaker
struct manages the state of each backend server (closed, open, half-open) to prevent sending requests to unhealthy servers.
The load balancer tracks the total number of requests and the duration of each request using Prometheus metrics.