A simple load balancer for FastAPI applications.
Project description
🚀 FastAPI Load Balancer
A lightweight load balancer for FastAPI applications that distributes traffic across multiple backend services.
It provides round-robin request distribution, automatic failover, caching, and rate limiting.
🌟 Features
✅ Round-robin Load Balancing - Distributes requests evenly across backends.
✅ Health Checks - Automatically detects and removes unhealthy backends.
✅ Auto-Retry & Failover - If a backend is down, the request is retried on another backend.
✅ Rate Limiting (Optional) - Prevent API abuse.
✅ Caching (Optional) - Improve performance by caching responses.
📦 Installation
Install via pip:
pip install fastapi-loadbalancer
🚀 Usage
1️⃣ Run Backend Services
You need at least two backend FastAPI servers running.
Example: backend1.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/data")
def get_data():
return {"backend": "Backend 1", "message": "Hello from Backend 1"}
Run it with:
uvicorn backend1:app --port 8001 --reload
Create another backend (backend2.py) with:
from fastapi import FastAPI
app = FastAPI()
@app.get("/data")
def get_data():
return {"backend": "Backend 2", "message": "Hello from Backend 2"}
Run it with:
uvicorn backend2:app --port 8002 --reload
2️⃣ Create the Load Balancer
Now, create a gateway.py file that will act as the load balancer:
from fastapi import FastAPI, Request
from fastapi_loadbalancer import LoadBalancer
app = FastAPI()
# List of backend servers
backend_servers = [
"http://127.0.0.1:8001",
"http://127.0.0.1:8002"
]
# Initialize Load Balancer
load_balancer = LoadBalancer(backend_servers)
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
async def handle_request(request: Request, path: str):
return await load_balancer.proxy_request(request)
3️⃣ Run the Load Balancer
Now, start the load balancer:
uvicorn gateway:app --port 8000 --reload
4️⃣ Test the Load Balancer
Send a request to the load balancer:
curl http://127.0.0.1:8000/data
The response will alternate between:
{"backend": "Backend 1", "message": "Hello from Backend 1"}
or
{"backend": "Backend 2", "message": "Hello from Backend 2"}
⚙️ Advanced Features
🛠 Health Check
The package automatically removes unhealthy backends from the rotation.
🚀 Rate Limiting (Optional)
To prevent abuse, you can enable rate limiting in loadbalancer.py.
⚡ Caching (Optional)
If you enable caching, the load balancer will store responses to reduce backend load.
🎯 Why Use This?
- Easily distribute requests between multiple FastAPI backends.
- Automatic failover ensures high availability.
- Lightweight & easy to integrate with any FastAPI project.
📜 License
This project is licensed under the MIT License.
🤝 Contributing
- Fork the repository.
- Create a new branch (
feature-new). - Commit changes and open a PR.
📬 Need Help?
For issues, please open a GitHub Issue.
⭐ Star this project if you find it useful! ⭐
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_loadbalancer-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_loadbalancer-0.1.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec2af41d2e26fc359322afe5874579889e8b96a8a97b744d5f47dd8beac4b958
|
|
| MD5 |
ca7a7c109a0d38508e2d1d2e62e683e8
|
|
| BLAKE2b-256 |
080309f77cf780277ecb6755edc5062e09110c2260e89b298a5be85a9a8cf87f
|
File details
Details for the file fastapi_loadbalancer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_loadbalancer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
074ebcec0f75c30a28dff04e06d6cf507587ed8cdfb0fe7a0032eef2ea7cb101
|
|
| MD5 |
badc875e62d0a1d05ad18874172aa50c
|
|
| BLAKE2b-256 |
e46fa155c51b0d8087e614a4c8506fc17ff8ee12a1cb6211bdb9ed9b348cfac6
|