Skip to content

suchismitacodes/load-balancer

Repository files navigation

HTTP Load Balancer

A multi-threaded TCP-based HTTP load balancer implemented in C++ for distributing client requests across backend servers with automatic health monitoring and real-time statistics.

Core Implementations

1. Weighted Least Connections Algorithm — Intelligent traffic distribution based on current server load
2. Automatic Health Monitoring — Continuous health checks with failover capabilities
3. Real-time Statistics — Live monitoring of connections, requests, and server status
4. Multi-threaded Architecture — High-performance concurrent connection handling

Workflow Diagram

lb

Diagram created using StarUML 7.0.0

How to Run

Prerequisites

  • C++14 compatible compiler (g++)
  • Python 3.x (for test backend servers)
  • Linux/Unix environment

Build

# Clone the repository
git clone https://github.com/psychomita/load-balancer.git
cd load-balancer

# Build the project
make

Usage Examples

# Equal Weight Distribution (as shown in the diagram) 
./bin/load_balancer 8080 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003

# Weighted Distribution
./bin/load_balancer 8080 127.0.0.1:9001:2 127.0.0.1:9002:1 127.0.0.1:9003:1

# With Connection Limits
./bin/load_balancer 8080 127.0.0.1:9001:2:500 127.0.0.1:9002:1:200

Start Test Servers in the Backend

# Terminal 1
python3 test_server.py 9001 9002 9003

Start Load Balancer

# Terminal 2
./bin/load_balancer 8080 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003

Test the Load Balancer

# Terminal 3

# Single request
curl http://localhost:8080

# Multiple requests to see load balancing
for i in {1..1000}; do curl -s http://localhost:8080 > /dev/null & done
wait

Demo

Test Servers (written in Python for testing purpose)

ss1

Test servers run on ports 9001, 9002 and 9003, serving content about Bjarne Stroustrup

Health Check and Statistics Display

  • When all backend servers are running, they are marked as HEALTHY.
  • When all servers are stopped, the load balancer reports no healthy servers available.
  • Metrics tracked are Total requests processed, Number of healthy servers and Server-wise statistics.
ss2

Load Balancing

The load balancer uses a Weighted Least Connections algorithm:

  1. Health Check: Only healthy servers are considered
  2. Weight Calculation: effective_load = active_connections / weight
ss3

Requests from port 8080 are routed to backend servers 9001, 9002 and 9003

Core Concepts Implemented

  • Operating Systems: Process Management, Memory Management, Inter-Process Communication
  • Computer Networks: Socket Programming, HTTP Protocol, Network Architecture
  • Data Structures & Algorithms: Vector Containers, Load Balancing Algorithm, Concurrency Control

For visual reference, watch the Demo Video

About

A multi-threaded HTTP load balancer written in C++, for efficient request routing, health monitoring and real-time statistics.

Topics

Resources

License

Stars

Watchers

Forks

Contributors