A lightweight, asynchronous TCP load balancer written in Rust using the Tokio runtime. This project distributes incoming TCP connections across multiple backend servers using round-robin selection, with real-time health checks to ensure traffic is only sent to healthy servers.
- Round-Robin Load Balancing: Evenly distributes incoming connections across available servers
- Real-Time Health Checks: Continuously verifies server availability before forwarding traffic
- Shared State Management: Uses
Arc<Mutex<>>for safe, concurrent access to server state - Graceful Shutdown: Supports clean termination with
Ctrl+C - Configurable Health Check Interval: Adjustable health check timing via CLI argument
- Rust (stable)
- Cargo
cargo run -- --client 127.0.0.1:8000 --server 127.0.0.1:9001 --server 127.0.0.1:9002This starts the load balancer on port 8000 and distributes connections across the backend servers on ports 9001 and 9002.
cargo run -- --client 127.0.0.1:8000 --server 127.0.0.1:9001 --interval 10This configures the health check interval to 10 seconds.
- The load balancer listens for incoming TCP connections on the client port
- Backend servers are tracked with health status updated via periodic TCP connection checks
- Only healthy servers receive traffic
- Connections are forwarded using a round-robin algorithm, ensuring even distribution
- A background task continuously monitors server health
- Supports clean shutdown with
Ctrl+C
main.rs: Core application logic, argument parsing, health checks, and proxy forwardingServerEntry: Tracks each backend server's address and health status- Uses
tokiofor async networking andclapfor command-line argument parsing
Potential next steps:
- Weighted load balancing based on server capacity
- Prometheus-style metrics endpoint
- Configurable connection limits per backend
- Support for additional balancing algorithms (least-connections, random selection)