A simple, efficient load balancer implemented in C++ using an object-oriented approach. It uses the least-connections strategy to distribute incoming client connections across backend servers. The project supports multithreading and ensures a fair distribution of workload.
.
├── LoadBalancer.h # Header file for the LoadBalancer class
├── LoadBalancer.cpp # Implementation of the LoadBalancer class
├── main.cpp # Main entry point of the application
├── Makefile # Makefile for building the project (optional)
└── README.md # Project documentation
- Least-connections strategy for fair load distribution.
- Multithreaded handling of client connections for better performance.
- Clean, object-oriented design for easy understanding and maintenance.
- A C++ compiler (e.g.,
g++). - Basic understanding of C++ socket programming.
- A POSIX-compliant operating system (Linux, macOS).
git clone <repository-url>
cd load_balancerEnsure you have a Makefile in the project directory. Run:
makeManually compile the project using g++:
g++ -std=c++11 -pthread -c LoadBalancer.cpp main.cpp
g++ -std=c++11 -pthread -o load_balancer main.o LoadBalancer.o./load_balancer-
Start the server:
cd server npm install node server.js -
Start the testing server:
cd testing npm install node index.js
- Listening Port: The load balancer listens on port
8080by default. - Backend Ports: Backend servers are defined as
{8001, 8002, 8003}in themain.cppfile. You can modify these values if needed.
| File | Description |
|---|---|
LoadBalancer.h |
Declaration of the LoadBalancer class. |
LoadBalancer.cpp |
Implementation of the load balancing logic. |
main.cpp |
Entry point of the application. |
Makefile |
Optional: Automates the build process. |
README.md |
Project documentation. |
Here’s an example of running the build and test:
# Build
make
# Run the load balancer
./load_balancerFor testing:
# Run the server
cd server
npm install
node server.js
# Run the testing server
cd testing
npm install
node index.jsThis project is licensed under the MIT License. See the LICENSE file for more details.
Contributions are welcome! Feel free to fork the repository, submit issues, or create pull requests.
- Add support for more sophisticated load-balancing algorithms (e.g., round-robin, IP-hash).
- Implement health checks for backend servers.
- Add dynamic scaling of backend servers.