Skip to content

About

A multi-threaded Java Load Balancer and Reverse Proxy. Built with native TCP Sockets and the Strategy Design Pattern to dynamically distribute network traffic across multiple backends.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

Repository files navigation

Java Multi-Threaded TCP Load Balancer

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.

Features

  • TCP Socket Streaming: High-performance byte-level proxying between clients and backends.
  • Multi-threaded Architecture: Utilizes a ThreadPool to 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.

System Architecture

The system follows a clean, modular structure:

  1. Core: Manages the ServerSocket and delegates connection handling to workers.
  2. Strategy: The "Brain" of the system. Implements the balancing logic.
  3. Backend: Mock HTTP servers to demonstrate the traffic distribution.

Balancing Strategies

  • Round Robin: Distributes requests in a circular, fair order (1, 2, 1, 2...).
  • Random: Uses probabilistic distribution to select the next available backend.

Getting Started

Prerequisites

  • JDK 17 or higher.

Compilation

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/*.java

Running the project

java br.com.luisfelipe.loadbalancer.Main

The Load Balancer will start on port 8080, proxying to backends on ports 8081 and 8082.

About

A multi-threaded Java Load Balancer and Reverse Proxy. Built with native TCP Sockets and the Strategy Design Pattern to dynamically distribute network traffic across multiple backends.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages