Skip to content

A lightweight HTTP load balancer built in Go. Advanced routing, health checks, policy enforcement, and real-time observability. It's experimentation! πŸš€

License

Notifications You must be signed in to change notification settings

rixtrayker/go-loadbalancer

Repository files navigation

πŸš€ Advanced Go Load Balancer

Version Go Version License Build Status

A high-performance, feature-rich HTTP/S load balancer built from scratch in Go

Educational β€’ Modular β€’ Production-Ready

πŸ“‘ Table of Contents


✨ Overview

⚠️ Beta Release: This project is currently in beta and actively under development. Features and APIs may evolve.

This project showcases an advanced HTTP/S load balancer implementation in Go, designed as both an educational resource and a practical networking infrastructure component. Built with modularity and extensibility in mind, it demonstrates core concepts of distributed systems, load balancing, and network programming.

🎯 Features

πŸ”„ Load Balancing

  • Multiple Algorithms: Round Robin, Least Connections, Weighted
  • Smart Distribution: Intelligent traffic routing
  • Backend Pool Management: Organized backend grouping

πŸ₯ Health Monitoring

  • Active Health Checks: Continuous backend monitoring
  • Pluggable Probes: HTTP, TCP health check support
  • Automatic Failover: Seamless backend switching

πŸ›£οΈ Advanced Routing

  • Multi-Factor Routing: Host, path, method, headers
  • Flexible Rules: Complex routing configurations
  • Dynamic Updates: Runtime route modifications

πŸ›‘οΈ Policy Enforcement

  • Rate Limiting: Request throttling and control
  • Header Transformation: Request/response modification
  • IP Access Control: Whitelist/blacklist support

πŸ”§ Additional Features

  • HTTP/S Reverse Proxying with high performance
  • Graceful Shutdown for clean terminations
  • Observability Integration with logging, metrics, and tracing
  • Modular Architecture for easy extension

πŸ“ Project Structure

The project follows the standard Go project layout:

go-loadbalancer/
β”œβ”€β”€ πŸ“„ cmd/go-lb/                # Application entry point
β”‚   └── main.go                  # Main application file
β”œβ”€β”€ 🌐 api/                      # API definitions
β”‚   └── http/v1/                 # HTTP API version 1
β”œβ”€β”€ βš™οΈ  configs/                  # Configuration files and templates
β”‚   β”œβ”€β”€ config.go                # Configuration structures
β”‚   └── loader.go                # Configuration loading logic
β”œβ”€β”€ πŸ”’ internal/                  # Internal business logic
β”‚   β”œβ”€β”€ app/                     # Application initialization
β”‚   β”œβ”€β”€ admin/                   # Admin interface
β”‚   β”œβ”€β”€ backend/                 # Backend server management
β”‚   β”œβ”€β”€ healthcheck/             # Health checking system
β”‚   β”œβ”€β”€ logging/                 # Structured logging
β”‚   β”œβ”€β”€ middleware/              # HTTP middleware
β”‚   β”œβ”€β”€ monitoring/              # Metrics and monitoring
β”‚   β”œβ”€β”€ serverpool/              # Backend pools & algorithms
β”‚   β”œβ”€β”€ routing/                 # Request routing engine
β”‚   β”œβ”€β”€ policy/                  # Policy enforcement
β”‚   β”œβ”€β”€ tracing/                 # Distributed tracing
β”‚   └── handler/                 # Core request handlers
β”œβ”€β”€ πŸ“¦ pkg/                      # Reusable utilities
β”œβ”€β”€ πŸ§ͺ test/                     # Additional test applications
β”œβ”€β”€ πŸ”§ scripts/                  # Scripts for various tasks
β”œβ”€β”€ 🚒 deployments/              # Deployment configurations
β”‚   └── docker/                  # Docker-related files
β”œβ”€β”€ πŸ“Š tools/                    # Tools and utilities
β”‚   └── k6/                      # K6 load testing scripts
└── πŸ“š docs/                     # Documentation files

πŸš€ Quick Start

Prerequisites

  • Go 1.23+ installed on your system
  • Backend services to load balance (for testing)

Installation

# Clone the repository
git clone https://github.com/rixtrayker/go-loadbalancer.git

# Navigate to project directory
cd go-loadbalancer

# Build the project
go build -o build/go-lb ./cmd/go-lb

Running

# Start with default configuration
./build/go-lb

# Or specify a config file
./build/go-lb --config configs/config.yml

βš™οΈ Configuration

The load balancer uses a flexible configuration system supporting:

  • 🎯 Listener Settings: Address, port, TLS configuration
  • 🏊 Backend Pools: Server groups with load balancing algorithms
  • πŸ’“ Health Checks: Monitoring intervals, timeouts, and probe types
  • πŸ›£οΈ Routing Rules: Complex request matching and forwarding
  • πŸ“‹ Policies: Rate limiting, transformations, and access control
  • πŸ“Š Monitoring: Logging, metrics, and tracing configuration

Configuration Example

server:
  address: ":8080"
  
backend_pools:
  - name: "web-servers"
    algorithm: "round_robin"
    backends:
      - url: "http://localhost:3001"
        weight: 1
      - url: "http://localhost:3002"
        weight: 2
    health_check:
      path: "/health"
      interval: "30s"
      timeout: "5s"

routing_rules:
  - match:
      host: "example.com"
      path: "/api/*"
    target_pool: "web-servers"
    policies:
      - rate_limit: "100/minute"

monitoring:
  prometheus:
    enabled: true
    path: "/metrics"
    port: 9090
  tracing:
    enabled: true
    service_name: "go-loadbalancer"
    endpoint: "localhost:4317"

πŸ—οΈ Architecture

Design Principles

  • πŸ”§ Modularity: Clean separation of concerns
  • πŸ”Œ Extensibility: Plugin-based architecture
  • ⚑ Performance: Optimized for high throughput
  • πŸ›‘οΈ Reliability: Robust error handling and recovery

Core Components

Component Purpose
Router Intelligent request routing and matching
Server Pool Backend management and load balancing
Health Checker Continuous backend monitoring
Policy Engine Request/response transformation and control
Admin Interface Runtime configuration and monitoring

πŸ” Observability

The load balancer includes comprehensive observability features:

Logging

  • Structured JSON logging with zap
  • Configurable log levels and formats
  • Trace context correlation

Metrics

  • Prometheus metrics for all components
  • Request/response metrics
  • Backend health and performance metrics
  • System resource usage

Tracing

  • OpenTelemetry integration
  • Distributed tracing support
  • Trace context propagation
  • Span attributes for detailed analysis

πŸŽ“ Lessons Learned & Skills Demonstrated

This project serves as an excellent learning resource, demonstrating several important concepts and best practices in Go development:

  • Standard Go project layout
  • Modular architecture with clean interfaces
  • Effective use of middleware patterns
  • Comprehensive observability implementation
  • Advanced configuration management
  • Multiple load balancing algorithms
  • Policy-based request handling

🀝 Contributing

We welcome contributions! See the Contributing Guide for more information.


πŸ“Š Roadmap

  • gRPC Load Balancing: Support for gRPC protocols
  • Docker Integration: Containerized deployment
  • Kubernetes Support: Native K8s integration
  • WebSocket Proxying: Real-time connection support
  • Circuit Breaker: Fault tolerance patterns

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ❀️ by rixtrayker

⭐ Star this repo if you find it helpful! ⭐

About

A lightweight HTTP load balancer built in Go. Advanced routing, health checks, policy enforcement, and real-time observability. It's experimentation! πŸš€

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published