# Using RabbitMQ as a Broker for Celery

RabbitMQ is the default and most feature-complete broker for Celery. Unlike Redis, which is a key-value store used as a broker, RabbitMQ implements the Advanced Message Queuing Protocol (AMQP), providing robust messaging capabilities.

## RabbitMQ vs. Redis

| Feature | Redis | RabbitMQ |
| :--- | :--- | :--- |
| **Type** | In-memory Key-Value Store | Message Broker (AMQP) |
| **Speed** | Extremely Fast | Fast (but slightly slower than Redis) |
| **Reliability** | Good (depends on persistence config) | Excellent (Guaranteed Delivery) |
| **Complexity** | Low (Easy to set up) | Medium (More concepts to learn) |
| **Persistence** | Optional (AOF/RDB) | Native (Durable Queues) |
| **Best For** | Quick tasks, caching, simple setups | Critical tasks, complex routing, enterprise |

**Key Difference:** RabbitMQ guarantees that a message is not lost even if the worker crashes mid-task (via acknowledgments and durable queues). Redis relies on a "visibility timeout" mechanism which can lead to tasks being re-executed if a worker takes too long, or lost if Redis crashes without persistence.

## 1. Installation

You need to install Celery. No extra bundle is strictly required for RabbitMQ as it uses `amqp` (which is a dependency of Celery), but it's good practice to ensure dependencies are met.

```bash
pip install celery
```

## 2. Configuration

To use RabbitMQ, configure the `broker_url` using the `amqp://` scheme.

### Basic Setup

```python
from celery import Celery

# Format: amqp://userid:password@hostname:port/virtual_host
# Default RabbitMQ credentials are guest/guest
app = Celery('tasks', broker='amqp://guest:guest@localhost:5672//')

@app.task
def add(x, y):
    return x + y
```

### Result Backend

RabbitMQ is designed to be a *broker* (transport), not a result store. While you *can* use the `rpc://` backend to send results back as messages, it is often better to pair RabbitMQ (broker) with Redis or a Database (result backend).

```python
app = Celery('tasks',
             broker='amqp://guest:guest@localhost:5672//',
             backend='redis://localhost:6379/0')
```

## 3. Running RabbitMQ (via Docker)

The easiest way to run RabbitMQ locally is using Docker. The `management` tag includes a web UI.

```bash
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
```

*   **Port 5672**: Used by Celery to connect.
*   **Port 15672**: Web Management UI.

### Accessing the Management UI

Open your browser to `http://localhost:15672`.
*   **Username**: `guest`
*   **Password**: `guest`

Here you can see connections, queues, and message rates in real-time.

## 4. Advanced Features

### Durable Queues
RabbitMQ supports durable queues, meaning if the broker restarts, the queue (and its messages) survives. Celery enables this by default (`task_default_delivery_mode = 'persistent'`).

### Complex Routing
RabbitMQ excels at routing. You can use different **Exchanges** (Direct, Topic, Fanout) to route tasks to specific queues based on routing keys.

[[programming/python/celery]]