Retrieve queue length with Celery (RabbitMQ, Django)
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Celery is an open-source asynchronous task queue or job queue which is 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 multiple worker nodes using multiprocessing, Eventlet, or gevent. When it comes to Django, Celery is a common choice for handling background tasks, and RabbitMQ often serves as its broker, managing the queue of tasks to be processed.
Understanding the Setup
Before diving into how to retrieve the queue length in Celery when using RabbitMQ as a broker, it is crucial to understand the components involved:
- Celery: The task queue system that integrates with Django and other Python web frameworks.
- RabbitMQ: This is the message broker, an intermediary for messaging that receives and sends messages to the Celery workers.
- Django: The web framework where tasks are defined and from where they are dispatched.
Getting Started with RabbitMQ as a Broker
Firstly, ensure RabbitMQ is installed and running on your machine. You can install RabbitMQ on various operating systems, and detailed instructions are provided in the RabbitMQ documentation.
Integration of Celery with Django requires the following steps:
- Install Celery:
- Create a
celery.pyfile in your Django project’s main module to setup Celery:
- Ensure that your Django settings include the configuration for the Celery broker URL pointing to RabbitMQ:
Retrieving Queue Length
Accessing RabbitMQ Management API
To retrieve the queue length, meaning the number of messages waiting to be executed, one effective way can be to use the RabbitMQ Management HTTP API. It provides comprehensive information about queues.
First, you need to enable the RabbitMQ management plugin if it’s not already enabled:
The relevant endpoint for fetching queue details including the queue length is:
Here, vhost is your RabbitMQ Virtual Host, and queue_name is the name of your queue.
Using Python to Fetch Queue Length
You can retrieve the queue length programmatically in Python using the requests library to call the RabbitMQ API:
This function fetches the number of messages in the specified queue.
Summary Table
| Component | Role | Interaction with Celery |
| Celery | Asynchronous task queue | Executes tasks concurrently |
| RabbitMQ | Message broker | Manages and stores messages as tasks |
| Django | Web framework | Dispatches tasks and integrates with Celery |
Additional Considerations
- Security: When using the RabbitMQ Management API, pay careful attention to securing your endpoints and using proper authentication methods to protect your data.
- Performance: Continuously polling the queue size, especially for heavily loaded systems, might affect the performance of your RabbitMQ server. It's advisable to poll at a reasonable interval.
In summary, retrieving the queue length in a Django application using Celery and RabbitMQ involves understanding the components involved, setting up RabbitMQ and Celery, and utilizing the RabbitMQ Management API to programmatically fetch the queue size. With these tools, developers can effectively manage task loads and monitor the health and performance of their applications.

