A scalable, modular background job processing system built with FastAPI and Redis. Offload time-consuming tasks (like sending emails, image processing, or PDF generation) from your main application for a faster, more responsive user experience.
- RESTful API for enqueuing jobs (
/enqueue) - Redis-backed queue for reliable job storage
- Worker service for concurrent, asynchronous task execution
- Live metrics endpoint (
/metrics) for monitoring - Simple logging to
logs.txtfor traceability - Easily extensible: add your own job types with minimal code
Client → [FastAPI Producer] → [Redis Queue] ← [Worker Service]
↓
[logs.txt]
- Client: Sends HTTP POST requests to enqueue jobs
- Producer (API): Receives jobs and enqueues them in Redis
- Redis: Stores jobs until processed
- Worker: Dequeues and processes jobs, logs results, exposes
/metrics
When a user signs up, your app can POST a job to /enqueue to send a welcome email. The user gets an instant response, while the email is sent in the background by the worker.
- Clone the repository
- Install Redis (if not already running)
sudo apt update && sudo apt install redis-server redis-server - Install Python dependencies
pip install -r requirements.txt
- Configure environment
- Create a
.envfile:REDIS_URL=redis://localhost:6379/0
- Create a
- Start the Producer API
uvicorn app.main:app --reload
- Start the Worker
python -m worker.run_worker
Send a POST request to /enqueue:
{
"type": "send_email",
"payload": {
"to": "user@example.com",
"subject": "Welcome!"
},
"retries": 3
}Curl Example:
curl -X POST "http://127.0.0.1:8000/enqueue" -H "Content-Type: application/json" -d '{"type":"send_email","payload":{"to":"user@example.com","subject":"Welcome!"},"retries":3}'Check worker stats at:
GET http://127.0.0.1:8001/metrics
- Add new job types by editing the
process_taskfunction inworker/run_worker.py. - Add your logic under a new
iforelifblock for your custom task type.
Developed by [BilalShah+Cursor]. Inspired by distributed systems best practices.