This project simulates a multithreaded load balancer using Python's threading
and PriorityQueue
. It mimics how real load balancers distribute traffic across multiple backend servers, complete with:
- 🔁 Round-robin server selection
- ⏳ Timeout and retry mechanism
- 🧾 Priority-based request queueing
- 📊 Per-server metrics tracking
Each server handles requests in a separate thread, allowing for concurrent processing.
Requests are distributed across servers in a fair, round-robin fashion.
Requests are placed in a PriorityQueue based on their priority (lower number = higher priority).
If a server doesn't finish a request in time, it is retried up to a defined maximum number of times.
The system keeps track of how many requests each server has successfully processed.
- Clients send requests via code simulation (could be extended to HTTP API).
- LoadBalancer tries to assign the request to the next available server.
- If all servers are busy, the request is put in a priority queue.
- A background dispatcher thread monitors the queue and assigns requests as servers become available.
- If a server doesn’t finish in time, the request is retried.
- If the retry limit is reached, the request is dropped.
- Python 3.7+
- No external libraries required
- Save the code into
load_balancer.py
- Run the script:
python load_balancer.py
Python multithreading and threading.Lock
Producer/consumer pattern with a dispatcher
Queues for scheduling and priority control
Simple retry and timeout patterns
- Website: www.mehmettepeli.com
- E-Mail: mehmet@mehmettepeli.com
- Linkedin: https://www.linkedin.com/in/mehmettepeli/