Celery task schedule (Celery, Django and RabbitMQ)
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation but supports scheduling as well. The execution units, called tasks, are executed concurrently on one or more worker nodes using multiprocessing, Eventlet, or gevent. Celery is used in production systems to process millions of tasks a day.
How Celery Works with Django and RabbitMQ
Celery can be integrated into Django applications to handle background tasks. RabbitMQ, often used as the messaging system, acts as the message broker for Celery. A message broker is a component that translates a message from the messaging protocol of the sender to the messaging protocol of the receiver. Celery communicates via messages, usually using a broker to mediate between clients and workers.
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Thanks to Django's scalability, Celery works well for handling background tasks in Django applications.
RabbitMQ is one of the most popular open source message brokers. It supports multiple messaging protocols. RabbitMQ can handle large volumes of messages and has excellent support for clustering and reliability.
Setting Up Celery with Django and RabbitMQ
Step 1: Install Celery
Install Celery and its dependencies with:
Step 2: Configure Celery in Django
Add a new module called celery.py in your Django project’s main module.
Step 3: Set Up the RabbitMQ Broker
Ensure RabbitMQ is installed and running on your machine or on your designated server. Configure the BROKER_URL in your Django settings:
Example of a Simple Task
Create a tasks.py file in one of your Django apps and define a task:
This task can be called in the background in various parts of your Django application:
Scheduling Tasks with Celery Beat
Celery Beat is a scheduler that kicks off tasks at regular intervals, which are then executed by available worker nodes in the cluster. Tasks can be scheduled to execute periodically or at a specific time.
Simple Periodic Tasks
To use Celery Beat, you need to setup a periodic tasks schedule:
Key Concepts Table
| Concept | Description |
| Task | A unit of work or operation that Celery should perform. |
| Worker | A process that executes the tasks provided by the broker. |
| Broker | A message queue that holds tasks and their related information until they are fetched by workers. RabbitMQ, Redis, and Amazon SQS are popular choices. |
| Celery Beat | A scheduler that sends tasks to the worker nodes at regular intervals based on a predefined schedule. |
| Task Queue | A queue in the broker where tasks are held before processing. |
By implementing Celery with Django and RabbitMQ, developers can effectively manage background tasks and periodic tasks to enhance web application performance. This combination not only leverages Django's rapid development features but also ensures reliable message queuing through RabbitMQ and scalable task management with Celery.

