A lightweight, fault-tolerant distributed task queue built from scratch in Python. Features automatic retry logic, dead letter queue handling, and worker health monitoring.
- Priority-based task scheduling
- Automatic retry with exponential backoff
- Dead letter queue for permanently failed tasks
- Worker health monitoring with automatic failover
- Real-time performance metrics
- No external dependencies (Python stdlib + SQLite)
cd task-queue
bash scripts/init_db.shTerminal 1 - Workers:
bash scripts/start_workers.sh 5Terminal 2 - Monitor:
python3 src/monitor.pyTerminal 3 - Submit Tasks:
python3 examples/submit_tasks.pyClients → SQLite Queue ← Worker Pool
↓
Monitor
↓
Dead Letter Queue
How it works:
- Clients submit tasks to SQLite database with priority levels
- Workers poll the queue and execute tasks concurrently
- Failed tasks retry with exponential backoff (1s, 2s, 4s...)
- After max retries, tasks move to Dead Letter Queue
- Monitor detects dead workers and reassigns their tasks
Edit config.py:
# Worker settings
DEFAULT_WORKER_COUNT = 5
POLL_INTERVAL = 0.5
HEARTBEAT_INTERVAL = 3
# Retry settings
MAX_RETRIES = 3
INITIAL_BACKOFF = 1
BACKOFF_MULTIPLIER = 2
# Monitor settings
WORKER_TIMEOUT = 10
STATS_REFRESH = 5Test queue performance:
python3 scripts/load_test.py --tasks 1000 --workers 10Example output:
Performance:
Throughput: 22.1 tasks/second
Average latency: 180ms
Task Completion:
Total submitted: 1000
Completed: 985
Failed: 15
Success rate: 98.5%
Five pre-built task types in src/tasks.py:
process_data- Data processing simulationsend_email- Email notification simulationprocess_order- Order processing with random failuresgenerate_report- Report generation simulationcleanup_task- Cleanup operation simulation
- Define function in
src/tasks.py:
def my_task(arg1, arg2):
print(f"Processing {arg1} and {arg2}")
return {'status': 'success'}- Register in
TASK_REGISTRY:
TASK_REGISTRY = {
'my_task': my_task,
}- Submit:
client.submit_task('my_task', {'arg1': 'foo', 'arg2': 'bar'})task-queue/
├── config.py # Configuration
├── schema.sql # Database schema
├── src/
│ ├── client.py # Task submission
│ ├── worker.py # Worker logic
│ ├── monitor.py # Health monitoring
│ └── tasks.py # Task definitions
├── scripts/
│ ├── init_db.sh # DB initialization
│ ├── start_workers.sh # Start workers
│ └── load_test.py # Performance testing
└── examples/
├── submit_tasks.py # Sample tasks
└── interactive_demo.py # Demo
- Automatic retry with exponential backoff
- Dead letter queue for permanent failures
- Worker failure detection and task reassignment
- Heartbeat-based health monitoring
- Atomic task claiming via database transactions
- Graceful degradation under failures
- Real-time metrics (throughput, latency, success rate)
- Worker health tracking
- Queue depth monitoring
Benchmarked on MacBook Air M3:
- Throughput: 500-850 tasks/second (5-10 workers)
- Latency: p50: 120ms, p99: 450ms
- Reliability: 98-99% completion under simulated failures
- Scaling: Linear up to ~10 workers (SQLite write limit)
Workers not processing tasks?
- Verify database exists:
ls queue.db - Check workers are running
- Look for error messages in worker terminal
Tasks stuck in "in_progress"?
- Workers may have crashed
- Monitor will reassign after 10 seconds
Database locked errors?
- Reduce number of workers
- Increase POLL_INTERVAL in config.py