This project implements a customizable load balancer that routes requests coming from several clients asynchronously among several servers to ensure an even load distribution.
This project includes a load balancer implemented using Flask and multiple server instances managed using Docker. The load balancer uses consistent hashing to distribute incoming requests among the server instances.
- Distributes requests evenly among servers using consistent hashing.
- Handles both GET and POST requests.
- Includes health check endpoints for monitoring server status.
- Easily scalable by adding more server instances.
- Windows Subsystem for Linux (WSL)
- Ubuntu 20.04 or above
- Docker 20.10.23 or above
- Docker Compose 2.15.1 or above
- Python 3.8 or above
-
Enable WSL:
Open PowerShell as Administrator and run:
wsl --installRestart your computer if prompted.
-
Install Ubuntu for WSL:
Open Microsoft Store, search for "Ubuntu", and install it. Alternatively, you can install it via PowerShell:
wsl --install -d Ubuntu-20.04
-
Set WSL Version to 2:
Ensure WSL is set to version 2:
wsl --set-default-version 2
-
Start Ubuntu:
Launch Ubuntu from the Start menu and complete the initial setup.
-
Update and Upgrade Ubuntu:
Inside the Ubuntu terminal, run:
sudo apt update sudo apt upgrade -y
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Build and run the Docker containers:
docker-compose up --build
Once the Docker containers are up and running, you can interact with the load balancer via the following endpoints:
-
Load Balancer API Endpoint:
http://localhost:5000/api- Supports both GET and POST requests.
- Forwards requests to one of the servers based on consistent hashing.
-
Server Endpoints:
http://localhost:5001/datahttp://localhost:5002/datahttp://localhost:5003/data- Servers will respond based on the load balancer's distribution.
server.py: Contains the code for the server instances.load_balancer.py: Contains the code for the load balancer.Dockerfile_loadbalancer: Dockerfile for the load balancer.Dockerfile_server: Dockerfile for the server instances.docker-compose.yml: Docker Compose configuration to set up the services.
- Used Flask for simplicity in setting up HTTP endpoints.
- Implemented consistent hashing to evenly distribute requests among servers.
- Used Docker Compose to manage multiple containers and networking.
- The server instances are identical and stateless.
- The load balancer and servers are running in the same Docker network.
- The client IP is used as the key for consistent hashing.
- Manually tested the load balancer by sending GET and POST requests using tools like
curland Postman. - Verified that requests are distributed evenly among the servers.
- The load balancer efficiently distributes requests using consistent hashing.
- Docker ensures isolated environments for each server instance, making it easy to scale horizontally.