Skip to main content

Django Celery Task System

Set up Celery with Django for async task processing — task design patterns, retry strategies, rate limiting, monitoring, and periodic tasks.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
Set up a Celery task system for a e-commerce platform Django project. The application needs to handle order confirmation emails, payment processing, inventory sync, report generation, image resizing asynchronously. Configure: 1) Celery app configuration in ecommerce/celery.py — broker (Redis (redis://localhost:6379/0)), result backend (django-db (django-celery-results)), serializer, task time limits (soft=300s, hard=600s), and worker concurrency settings for prefork with 4 processes workers. 2) Task design patterns: implement a base task class with automatic retry for transient errors (ConnectionError, Timeout) with max_retries=5 and exponential backoff. Use task_acks_late=True and reject_on_worker_lost=True for critical tasks. 3) Rate-limited tasks using @shared_task(rate_limit=10/m) for third-party API calls (Stripe, shipping providers). 4) Task chains and groups: design a workflow for order processing: validate → charge payment → update inventory → send confirmation → notify warehouse using chain() for sequential steps and group() for parallel processing with a chord() callback. 5) Periodic tasks using Celery Beat with django-celery-beat: schedule hourly inventory sync, daily report generation, weekly user digest emails with proper timezone handling. 6) Priority queues: define queues for critical (payments), default (emails, notifications), bulk (reports, exports), low (analytics) and route tasks to appropriate queues based on priority and resource needs. 7) Monitoring with Flower dashboard + Prometheus metrics — track task success/failure rates, queue depths, and worker utilization with alerts for queue backlog exceeding 1000 tasks.

What this prompt does

This prompt sets up a full Celery task system for a Django project. You define the [project_type] and the kinds of work to run asynchronously via [task_examples], and the AI configures a Celery app in [project_name]/celery.py with your [broker_type] broker, [result_backend], serializer, time limits (soft=[soft_limit]s and hard=[hard_limit]s), and concurrency tuned for [worker_type] workers.

The design centers on resilience. It produces a base task class that auto-retries transient errors like ConnectionError and Timeout with exponential backoff up to [max_retries], using task_acks_late=True and reject_on_worker_lost=True for critical work so a crash doesn't silently drop a task. Rate-limited tasks use [rate_limit] for [rate_limited_tasks] to stay under third-party API caps. The [complex_workflow] is expressed with chain() for sequential steps, group() for parallel work, and a chord() callback, while [periodic_tasks] run through Celery Beat with django-celery-beat and timezone handling. Priority queues from [queue_definitions] route work by urgency and resource need, and [monitoring_tool] watches success and failure rates, queue depth, and worker utilization, alerting when backlog exceeds [backlog_threshold].

When to use it

  • You have slow or flaky operations (emails, payments, third-party calls) blocking request latency
  • You need automatic retries with backoff so a downstream outage doesn't cascade
  • You're hitting rate limits on external APIs and need throttled task execution
  • You want a multi-step workflow expressed as sequential chains plus parallel groups with a callback
  • You need scheduled jobs like nightly reports or hourly syncs via Celery Beat
  • You want priority queues so payments never wait behind bulk analytics
  • You need monitoring that alerts before a backlog quietly turns into a backlog incident

Example output

The AI returns a celery.py configuration block, a base task class with retry decorators and backoff, example @shared_task definitions for rate-limited and queued work, a chain/group/chord workflow for [complex_workflow], a Beat schedule dict for [periodic_tasks], queue routing config for [queue_definitions], and a monitoring section describing what [monitoring_tool] should track and where the [backlog_threshold] alert fires. Expect organized code snippets per numbered concern, not a single file.

Pro tips

  • Match [soft_limit] and [hard_limit] to your real task durations — too tight and legitimate work gets killed, too loose and hung tasks tie up workers
  • Be specific in [rate_limited_tasks] so only external-API tasks get throttled, not internal fast ones
  • Define [queue_definitions] by resource profile (critical, default, bulk, low) so routing actually isolates blast radius
  • Set [backlog_threshold] based on observed throughput, not a guess, so alerts are meaningful
  • Keep task_acks_late=True only for idempotent critical tasks; for non-idempotent work it can cause duplicate execution on retry
  • Tune [worker_type] concurrency to the workload: CPU-bound resizing wants fewer processes than IO-bound API calls
  • If the workflow section is vague, re-prompt with the exact ordering of [complex_workflow] steps spelled out

Frequently Asked Questions

Which message broker does this prompt assume?
The broker is set by the `[broker_type]` variable, with Redis as the default example. You can change it to RabbitMQ or another supported broker and the generated `celery.py` configuration will reference your choice instead.
How does it handle tasks that fail because of a temporary network error?
It builds a base task class that automatically retries transient errors like ConnectionError and Timeout, with exponential backoff and a cap of `[max_retries]`. This keeps a brief downstream hiccup from permanently failing the task.
Can it schedule recurring jobs, or only handle one-off tasks?
It covers both. Periodic jobs defined in `[periodic_tasks]` run through Celery Beat with django-celery-beat and proper timezone handling, so you get hourly, daily, or weekly schedules alongside on-demand async tasks.
Does it prevent a slow third-party API from exhausting all my workers?
It addresses this through rate limiting (`[rate_limit]` on `[rate_limited_tasks]`) and priority queues from `[queue_definitions]`, so heavy or throttled work is isolated. Proper queue routing is what keeps a bad downstream from consuming the workers your critical tasks need.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Django & Flask Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support