A Layer 4 (Transport Layer) Load Balancer built from scratch using Java's native Networking API. This project acts as a Reverse Proxy that distributes incoming traffic across multiple backend servers using various balancing algorithms.
- TCP Socket Streaming: High-performance byte-level proxying between clients and backends.
- Multi-threaded Architecture: Utilizes a
ThreadPoolto handle multiple concurrent connections efficiently. - Strategy Design Pattern: Decoupled balancing logic allowing easy switching between algorithms.
- HTTP/1.1 Compatible: Includes connection handling (
Connection: close) to prevent hanging proxy streams.
The system follows a clean, modular structure:
- Core: Manages the ServerSocket and delegates connection handling to workers.
- Strategy: The "Brain" of the system. Implements the balancing logic.
- Backend: Mock HTTP servers to demonstrate the traffic distribution.
- Round Robin: Distributes requests in a circular, fair order (1, 2, 1, 2...).
- Random: Uses probabilistic distribution to select the next available backend.
- JDK 17 or higher.
From the src directory, run:
javac br/com/luisfelipe/loadbalancer/Main.java br/com/luisfelipe/loadbalancer/core/*.java br/com/luisfelipe/loadbalancer/backend/*.java br/com/luisfelipe/loadbalancer/strategy/*.javajava br.com.luisfelipe.loadbalancer.MainThe Load Balancer will start on port 8080, proxying to backends on ports 8081 and 8082.