Welcome to a hands-on systems project that walks through how to build a custom Layer 7 Load Balancer using Python and Flask, capable of distributing HTTP requests across multiple backend servers with health checks and concurrent client handling.
This project simulates a real-world application-layer load balancer — similar to what AWS ALB or Nginx does — but written from scratch using low-level socket programming. It's designed to:
- Distribute traffic using Round Robin Scheduling
- Ensure high availability via automated health checks
- Handle server failures and recoveries
- Manage concurrent client connections
| Feature | Tech/Method |
|---|---|
| Language | Python 3 |
| Web Framework | Flask |
| Networking | socket, requests |
| Concurrency | threading, multiprocessing |
| Testing Tool | curl, urls.txt |
.
├── backend.py # Launches Flask servers on multiple ports
├── load_balancer.py # Load Balancer with request routing + health checks
├── urls.txt # File with repeated URLs for load testing
├── README.md # Project documentation
├── LICENSE # License
- Implemented Round Robin Load Balancing
- Built a Layer 7 HTTP Router using raw sockets
- Designed a multi-threaded client handler system
- Used multiprocessing to simulate scalable backend servers
- Implemented real-time health checks for fault tolerance
- Understood backend recovery using dynamic server pools
✅ Distributes traffic across multiple backend servers
✅ Health checks every 5 seconds (configurable)
✅ Excludes dead backends from rotation
✅ Re-adds backends when they recover
✅ Supports multiple concurrent clients
✅ Easy to extend with new features
git clone https://github.com/<your-username>/CC_Build-You-Own-Load-Balancer.git
cd CC_Build-You-Own-Load-Balancerpython3 backend.pyThis will launch two backend Flask servers on ports 8080 and 8081.
python3 load_balancer.pyThe load balancer will start listening on port 5001 and begin health checking backends.
curl http://localhost:5001/Send a few requests and observe them alternate between the backends.
Stop one backend server and confirm that all traffic is now routed to the other. Restart it and it will be auto-included again.
url = "http://localhost:5001/"
url = "http://localhost:5001/"
url = "http://localhost:5001/"
url = "http://localhost:5001/"Run with:
curl --parallel --parallel-immediate --parallel-max 3 --config urls.txtLoad Balancer listening on port 5001...
Connection received from ('127.0.0.1', 55002)
Forwarding request to backend 127.0.0.1:8080
Response sent back to client.
Connection received from ('127.0.0.1', 55003)
Forwarding request to backend 127.0.0.1:8081
Response sent back to client.
- Add async I/O support via
asyncio - Add logging for monitoring performance & metrics
- Introduce Docker for easy container-based testing
- Build a GUI dashboard to visualize server status
- Implement weighted routing or least connection algorithms
This project was inspired by the "Build Your Own Load Balancer" challenge from Coding Challenges and was completed as part of my system design and backend development learning journey.
This project is licensed under the MIT License.