This project is a deep dive into the brain of a distributed system: the load balancer. It showcases a powerful service architecture where a central hub intelligently manages traffic to prevent [...]
The system is composed of five distinct applications that work together seamlessly to demonstrate the principles of service discovery and load balancing.
- Load Balancer (Port 9000): The entry point for all requests. It dynamically retrieves the list of available services from the registry and forwards requests based on the *Least Connections[...]
- Service Registry (Port 9004): A central registry where all backend services automatically register themselves, acting as the single source of truth for the load balancer.
- Backend Services (3 instances): Simple RESTful services running on ports 9001, 9002, and 9003. They handle incoming requests and represent the core business logic.
- Test Client: A simple application that generates 100 concurrent requests to fully test the load balancer's performance under load.
┌─────────────────────────────────────────────────────────────────┐
│ TEST CLIENT │
│ (100 Concurrent Requests) │
└────────────────────────┬────────────────────────────────────────┘
│
│ HTTP Requests
▼
┌─────────────────────────────────────────────────────────────────┐
│ LOAD BALANCER (Port 9000) │
│ │
│ 1. Query Service Registry for available services │
│ 2. Apply Least Connections Algorithm │
│ 3. Route request to service with fewest active connections │
└────────────────────────┬────────────────────────────────────────┘
│ │ │
│ Route │ Route │ Route
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│Service 1│ │Service 2│ │Service 3│
│Port 9001│ │Port 9002│ │Port 9003│
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
│ Register │ Register │ Register
│ Heartbeat │ Heartbeat │ Heartbeat
└───────┬───────┴───────┬───────┘
│ │
▼ ▼
┌──────────────────────────────┐
│ SERVICE REGISTRY (Port 9004) │
│ - Service Instances │
│ - Health Status │
│ - Connection Counts │
└──────────────────────────────┘
This project's load balancer uses a Least Connections algorithm. Unlike a simple round-robin approach that sends requests sequentially, this algorithm is intelligent and adaptive.
It works by tracking the number of active connections to each backend service. When a new request arrives, the load balancer checks each service's connection count and forwards the request to the service with the least number of active connections.
To run this project, you need to set up a multi-module Maven project in your preferred IDE (e.g., IntelliJ IDEA).
- Java Development Kit (JDK) 17 or higher
- An IDE with Maven support (e.g., IntelliJ IDEA)
-
Clone the Repository:
git clone https://github.com/vishwasio/load-balancer.git cd load-balancer -
Run the Services (in this specific order):
- Service Registry: Run
maininServiceRegistry.java. Wait for it to confirm it's running on port 9004. - Backend Services: Run
maininBackendService1.java,BackendService2.java, andBackendService3.javain separate terminals. - Load Balancer: Run
maininLoadBalancer.java. It will start on port 9000 and establish a connection with the registry. - Test Client: Run
maininTestClient.java. Observe the output in all consoles as the requests are distributed by the Least Connections algorithm.
- Service Registry: Run
- vishwasio - GitHub Profile