Skip to content

Repository files navigation

Distributed Task Queue with Failure Recovery

A lightweight, fault-tolerant distributed task queue built from scratch in Python. Features automatic retry logic, dead letter queue handling, and worker health monitoring.

Features

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

Quick Start

Initialize Database

cd task-queue
bash scripts/init_db.sh

Start the System (3 terminals)

Terminal 1 - Workers:

bash scripts/start_workers.sh 5

Terminal 2 - Monitor:

python3 src/monitor.py

Terminal 3 - Submit Tasks:

python3 examples/submit_tasks.py

Architecture

Clients → SQLite Queue ← Worker Pool
              ↓
         Monitor
              ↓
    Dead Letter Queue

How it works:

  1. Clients submit tasks to SQLite database with priority levels
  2. Workers poll the queue and execute tasks concurrently
  3. Failed tasks retry with exponential backoff (1s, 2s, 4s...)
  4. After max retries, tasks move to Dead Letter Queue
  5. Monitor detects dead workers and reassigns their tasks

Configuration

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 = 5

Load Testing

Test queue performance:

python3 scripts/load_test.py --tasks 1000 --workers 10

Example output:

Performance:
  Throughput: 22.1 tasks/second
  Average latency: 180ms

Task Completion:
  Total submitted: 1000
  Completed: 985
  Failed: 15
  Success rate: 98.5%

Available Task Types

Five pre-built task types in src/tasks.py:

  • process_data - Data processing simulation
  • send_email - Email notification simulation
  • process_order - Order processing with random failures
  • generate_report - Report generation simulation
  • cleanup_task - Cleanup operation simulation

Adding Custom Tasks

  1. Define function in src/tasks.py:
def my_task(arg1, arg2):
    print(f"Processing {arg1} and {arg2}")
    return {'status': 'success'}
  1. Register in TASK_REGISTRY:
TASK_REGISTRY = {
    'my_task': my_task,
}
  1. Submit:
client.submit_task('my_task', {'arg1': 'foo', 'arg2': 'bar'})

Project Structure

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

Key Concepts

Fault Tolerance

  • Automatic retry with exponential backoff
  • Dead letter queue for permanent failures
  • Worker failure detection and task reassignment

Reliability Engineering

  • Heartbeat-based health monitoring
  • Atomic task claiming via database transactions
  • Graceful degradation under failures

Observability

  • Real-time metrics (throughput, latency, success rate)
  • Worker health tracking
  • Queue depth monitoring

Performance

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)

Troubleshooting

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages