Skip to content

krisn2/loadbalancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚦 Go Load Balancer

A high-performance, open-source load balancer written in Go, supporting multiple algorithms, health checks, Prometheus metrics, circuit breakers, and rate limiting.

✨ Features

  • Multiple Load Balancing Algorithms

    • Round Robin
    • Weighted Round Robin
    • Least Connections
    • Power of 2 Choices
    • IP Hash
    • Consistent Hash
  • Real-time Health Checking (active & passive)

  • Rate Limiting per client (token bucket)

  • Circuit Breaker to avoid cascading failures

  • Prometheus Metrics (/metrics endpoint)

  • Hot Config Reload (via SIGHUP)

  • Sticky Sessions (optional, via cookies)

  • TLS Support

📂 Project Structure


krisn2-loadbalancer/
├── cmd/                # CLI entry points
│   ├── main.go         # Load balancer server
│   └── client.go       # Test client
├── internal/           # Core packages
│   ├── balancer/       # Load balancing algorithms
│   ├── backend/        # Demo backend servers
│   ├── circuit/        # Circuit breaker
│   ├── config/         # YAML config loader
│   ├── health/         # Health checker
│   ├── metrics/        # Prometheus integration
│   ├── proxy/          # Proxy handler
│   └── ratelimit/      # Rate limiting
├── config.yaml         # Example configuration
├── go.mod              # Go dependencies
├── LICENSE             # MIT License
└── README.md           # Project documentation


Screenshot

Screenshot

Screenshot

🛠️ Getting Started

To run the load balancer, ensure you have Go installed on your system.

Clone the repository:

git clone https://github.com/krisn2/loadbalancer.git
cd loadbalancer

Build and run the application:

go run ./cmd/main.go

The load balancer will start on http://localhost:8080 and will distribute requests to the configured backend servers.

📈 Monitoring

Access the Prometheus metrics endpoint to monitor the load balancer's performance.

Metrics endpoint: http://localhost:8080/metrics

Useful metrics:

  • lb_requests_total → total requests handled

  • lb_request_duration_seconds → latency distribution

  • lb_backend_up → backend health status

📝 Configuration (config.yaml)

Example config file:

listen: ":8080"

tls:
  cert: ""   # path to TLS cert (leave empty for HTTP)
  key: ""

backends:
  - "http://localhost:9001"
  - "http://localhost:9002"
  - "http://localhost:9003"

lb:
  algorithm: "weighted_rr"   # round_robin | weighted_rr | least_conn | ip_hash | pow2 | consistent_hash
  weights: [3,1,1]           # only used by weighted_rr
  retry_count: 2
  request_timeout_seconds: 5
  sticky_sessions: true

health:
  interval_seconds: 5
  consecutive_failures_to_mark_down: 2
  consecutive_success_to_mark_up: 2
  tcp_check: false

rate_limit:
  requests_per_second: 100
  burst: 20

client:
  concurrent_clients: 20
  requests_each: 50

👉 Tip: You can reload the config at runtime with:

kill -SIGHUP <pid_of_lb>

🧪 Running the Test Client

go run ./cmd/client.go

🙏 Contributing

We welcome contributions! Please feel free to open an issue or submit a pull request with new features, bug fixes, or improvements.

🤝 Contributing

Fork the repo

Create a feature branch: git checkout -b feature/my-feature

Commit changes: git commit -m "Added my feature"

Push to branch: git push origin feature/my-feature

Open a PR 🎉

📜 License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages