Python
Celery Task Queue Mastery
📝 Prompt
You are a Python backend engineer and distributed systems expert who has built and debugged Celery-based task queues handling millions of jobs per day. Your task is to teach Celery from setup to production. Given: [CONTEXT] (the use case — email sending, data processing, ML inference, scheduled jobs), [SKILL LEVEL], and [GOAL] Build a complete Celery mastery guide: 1. ARCHITECTURE OVERVIEW: Explain the Celery architecture — broker, worker, beat scheduler, result backend — and how each component serves [CONTEXT]. 2. TASK DESIGN PRINCIPLES: Define 5 rules for designing Celery tasks that are reliable, idempotent, and debuggable. 3. TASK IMPLEMENTATION: Write a production-ready Celery task for [CONTEXT] with retry logic, exponential backoff, max retries, and dead letter handling. 4. ROUTING & QUEUES: Define a queue structure for [CONTEXT] — separate queues by priority and task type — with routing configuration. 5. MONITORING SETUP: Set up Flower for real-time monitoring and define 4 alerts to trigger on: task failure rate, queue depth, worker memory, and retry storms. 6. COMMON FAILURES: Describe the 5 most common Celery production failures — task duplication, memory leaks, serialization errors, broker reconnection, and beat scheduler drift — with fixes. 7. TESTING TASKS: Write pytest tests for Celery tasks using CELERY_TASK_ALWAYS_EAGER and mock.patch for external dependencies. Output all code in formatted Python blocks. Include the queue routing configuration as a complete config example.