This project demonstrates a simple load balancer implemented in Rust using the Tokio asynchronous runtime. The load balancer supports multiple strategies for distributing client requests across multiple servers. Servers dynamically subscribe to the load balancer, and clients simulate multiple instances sending requests concurrently.
- Dynamic Server Subscription: Servers register themselves with the load balancer upon startup.
- Multiple Load Balancing Strategies:
- Round Robin
- Sticky Round Robin
- Weighted Round Robin
- Least Connections
- Concurrent Clients: Simulate multiple clients sending requests to the load balancer.
- Clear Logging: Detailed logs for the load balancer, servers, and clients.
- Rust and Cargo installed. If not, follow the instructions at rustup.rs.
- Tokio runtime (included in
Cargo.toml).
src/
├── bin/
│ ├── client.rs # Simulate multiple clients
│ ├── server.rs # Simulate multiple servers
├── load_balancer.rs # Load balancer logic
├── main.rs # Entry point
git clone <repository-url>
cd load-balancer-democargo buildRun the load balancer with the desired strategy:
cargo run --bin load_balancer -- <strategy>Replace <strategy> with one of the following:
round_robinsticky_round_robinweighted_round_robinleast_connectionsleast_time
Example:
cargo run --bin load_balancer -- round_robinStart multiple servers on different ports. Each server will automatically subscribe to the load balancer.
cargo run --bin server -- 127.0.0.1:8000
cargo run --bin server -- 127.0.0.1:8001Simulate multiple clients sending requests to the load balancer:
cargo run --bin client- Load Balancer Logs: Show which server is handling each request.
- Server Logs: Show received requests and responses.
- Client Logs: Show client connections and received responses.
This project serves as an educational example of implementing a load balancer in Rust. Feel free to experiment and extend its functionality! 🚀