A high-performance load balancer written in Go that distributes traffic across multiple backend servers using round-robin algorithm.
Before running the load balancer, you need to:
- Create config.yaml file - Copy from sample_config.yaml and modify backend URLs
- Start backend servers - Run your application servers on ports 8081, 8082, 8083.
If you are using different ports that's fine. Just need to update the config.yaml. For Bolt, I put 8100 as port. If you need different one. change in config.yaml and Dockerfile.
server:
port: 8100
host: "0.0.0.0"
backends:
- url: "http://localhost:8081" # For non-Docker
- url: "http://host.docker.internal:8082" # For Docker
- url: "http://host.docker.internal:8083" # For Docker
strategy: "round_robin"
health_check:
enabled: true
interval: "30s"
timeout: "5s"
path: "/health"
expected_status: 200
logging:
level: "info"
format: "text"
access_log: trueI added this sample server just for test purpose. In real world or production you need not this section. Instead of this one. you will have your own backend application with multiple running instances.
# 1. Start backend servers (in separate terminals)
go run test_servers/be1.go
go run test_servers/be2.go
go run test_servers/be3.go
# 2. Run load balancer
go run ./cmd -c config.yaml# Run automated test script
chmod +x bolt_test.sh
./bolt_test.shThe script will automatically:
- Create configuration
- Start backend servers
- Run load balancer
- Perform health checks
- Execute load test with 1000 requests
- Show performance statistics
# 1. Start backend servers locally (in separate terminals)
go run test_servers/be1.go # optional, use only to test the Bolt
go run test_servers/be2.go # optional, use only to test the Bolt
go run test_servers/be3.go # optional, use only to test the Bolt
# 2. Update config.yaml for Docker
# Change backend URLs to use host.docker.internal:
backends:
- url: "http://host.docker.internal:8081"
- url: "http://host.docker.internal:8082"
- url: "http://host.docker.internal:8083"
# 3. Build and run Docker container
docker build -t bolt-loadbalancer .
docker run -d \
--name bolt-lb \
-p 8100:8100 \
-v $(pwd)/config.yaml:/app/config.yaml:ro \
--add-host=host.docker.internal:host-gateway \
bolt-loadbalancer:latest# Run automated Docker test script
chmod +x docker_test.sh
./docker_test.shThe Docker test script will:
- Start backend servers locally
- Build Docker image
- Run load balancer in container
- Execute comprehensive tests
- Run load test and show results
# Check health
curl http://localhost:8100/health
# Test load balancing
for i in {1..6}; do
curl -s http://localhost:8100/ | head -1
done
# Load test with Apache Bench
ab -n 1000 -c 50 http://localhost:8100/# Stop Docker container
docker stop bolt-lb && docker rm bolt-lb
# View logs
docker logs bolt-lb
# Kill backend processes
pkill -f "go run test_servers"# Health check
curl http://localhost:8100/health
# Status info
curl http://localhost:8100/status
# Simple load test
for i in {1..100}; do curl -s http://localhost:8100/ >/dev/null; donego test ./tests/ # run all unit tests
go test -cover ./tests/ # run unit tests with coverage
doneRound Robin- Distributes requests evenly across all healthy backends
Weighted Round Robin (v0.2.0)- Distribute based on backend capacity weightsLeast Connections (v0.2.0)- Route to backend with fewest active connectionsIP Hash (v0.4.0)- Sticky sessions based on client IPAdaptive Load Balancing (v1.2.0)- Machine learning based routing
Built with Go's efficient concurrency model for production-grade performance.
The load balancer typically achieves:
- 15,000+ requests per second
- Sub-millisecond response times
- 100% success rate under normal load
- Automatic failover when backends are unavailable
Built with Go's efficient concurrency model for production-grade performance.