LogoMasst Docs

Task Queues

Understanding task queues for distributed job processing.

What is a Task Queue?

A Task Queue is a mechanism for distributing work across multiple workers. Unlike simple message queues, task queues typically focus on job execution with features like retries, scheduling, and progress tracking.


Task Queue vs Message Queue

AspectMessage QueueTask Queue
FocusMessage deliveryJob execution
FeaturesBasic deliveryRetries, scheduling, results
SemanticsData transferWork processing
ExamplesRabbitMQ, SQSCelery, Sidekiq, Bull

How It Works

Client                  Task Queue              Workers
   │                        │                      │
   │─── Submit Task ───────►│                      │
   │                        │                      │
   │◄── Task ID ────────────│                      │
   │                        │                      │
   │                        │──── Assign Task ────►│
   │                        │                      │
   │                        │◄─── Progress ────────│
   │                        │                      │
   │                        │◄─── Result ──────────│
   │                        │                      │
   │◄── Poll Result ────────│                      │

Key Features

FeatureDescription
RetriesAutomatically retry failed tasks
SchedulingRun tasks at specific times
PriorityHigh-priority tasks first
Rate limitingControl task execution rate
Result storageStore and retrieve task results
Progress trackingMonitor task completion

Common Use Cases

Use CaseExample
Background jobsSend emails, generate reports
Scheduled tasksDaily cleanup, cron jobs
Long-running tasksVideo encoding, data processing
Distributed computationMap-reduce operations
WebhooksProcess incoming webhook payloads

Task States

PENDING ──► STARTED ──► SUCCESS
                   └──► FAILURE ──► RETRY
                              └──► DEAD (max retries)

SystemLanguageBackend
CeleryPythonRedis, RabbitMQ
SidekiqRubyRedis
BullNode.jsRedis
BullMQNode.jsRedis
Hangfire.NETSQL Server, Redis

Code Example (Celery)

from celery import Celery

app = Celery('tasks', broker='redis://localhost:6379')

@app.task(bind=True, max_retries=3)
def process_order(self, order_id):
    try:
        # Process the order
        result = do_processing(order_id)
        return result
    except Exception as e:
        # Retry with exponential backoff
        raise self.retry(exc=e, countdown=2 ** self.request.retries)

# Submit task
task = process_order.delay(order_id=123)

# Check result
result = task.get(timeout=30)

Interview Tips

  • Differentiate from simple message queues
  • Explain retry strategies and dead letter handling
  • Discuss idempotency for at-least-once delivery
  • Mention scheduling and priority features
  • Give examples: email sending, report generation