Skip to content

mehmetepeli/python_multithreaded_load_balancer

Repository files navigation

🚦 Multithreaded Load Balancer in Python

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

🚀 Features

✅ 1. Multithreaded Request Handling

Each server handles requests in a separate thread, allowing for concurrent processing.

✅ 2. Round-Robin Load Distribution

Requests are distributed across servers in a fair, round-robin fashion.

✅ 3. Priority Queue

Requests are placed in a PriorityQueue based on their priority (lower number = higher priority).

✅ 4. Timeout & Retry

If a server doesn't finish a request in time, it is retried up to a defined maximum number of times.

✅ 5. Per-Server Stats

The system keeps track of how many requests each server has successfully processed.


🧠 How It Works

  • 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.

📦 Requirements

- Python 3.7+
- No external libraries required

▶️ How to Run

  1. Save the code into load_balancer.py
  2. Run the script:
  python load_balancer.py

📚 Learning Goals

This project demonstrates:

Python multithreading and threading.Lock

Producer/consumer pattern with a dispatcher

Queues for scheduling and priority control

Simple retry and timeout patterns

Information

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages