Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust TCP Load Balancer

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.


Features

  • 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

Quick Start

Requirements

  • Rust (stable)
  • Cargo

Run the Load Balancer

cargo run -- --client 127.0.0.1:8000 --server 127.0.0.1:9001 --server 127.0.0.1:9002

This starts the load balancer on port 8000 and distributes connections across the backend servers on ports 9001 and 9002.

Optional: Set Health Check Interval

cargo run -- --client 127.0.0.1:8000 --server 127.0.0.1:9001 --interval 10

This configures the health check interval to 10 seconds.


How It Works

  • 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

📁 Project Structure

  • main.rs: Core application logic, argument parsing, health checks, and proxy forwarding
  • ServerEntry: Tracks each backend server's address and health status
  • Uses tokio for async networking and clap for command-line argument parsing

Future Enhancements

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)

About

A lightweight, asynchronous TCP load balancer written in Rust using the Tokio runtime

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages