Background Job Patterns: Processing Work Outside the Request Cycle
Some work doesnβt belong in a web request. Sending emails, processing uploads, generating reports, syncing with external APIs β these tasks are too slow, too unreliable, or too resource-intensive to run while a user waits. Background jobs solve this by moving work out of the request cycle and into a separate processing system. The Basic Architecture β β β β β β W β β e β β b β β β β β β β A β β β p β β β p β β β β β β β β β β β β β β β β β βΆ β β β β βΆ β β β β β β R β β β β e β β Q β β s β β u β β u β β e β β l β β u β β t β β e β β s β β β β β β β β β β β β β β β β β β β βΆ β β β β β β β β β W β β β o β β β r β β β β k β β β e β β r β β s β β β β β β β β Producer: Web app enqueues jobs Queue: Stores jobs until workers are ready Workers: Process jobs independently Results: Optional storage for job outcomes Choosing a Queue Backend Redis (with Sidekiq, Bull, Celery) 1 2 3 4 5 6 7 8 9 # Celery with Redis from celery import Celery app = Celery('tasks', broker='redis://localhost:6379/0') @app.task def send_email(user_id, template): user = get_user(user_id) email_service.send(user.email, template) Pros: Fast, simple, good ecosystem Cons: Not durable by default (can lose jobs on crash) ...