# What RabbitMQ Is For — RabbitMQ

Source: https://www.skillbyai.com/en/rabbitmq/f-what

> Explain what a message broker does and when RabbitMQ is a good fit.

## A post office for your services

**RabbitMQ** is an open-source **message broker**: applications hand it messages, it stores them safely in **queues**, and delivers them to consuming applications. This decouples senders from receivers in **time** (the consumer can be offline), in **load** (a queue absorbs bursts that workers drain at their own pace) and in **knowledge** (the sender does not need to know which or how many services consume). Typical uses: **background jobs** (send email, resize images, generate PDFs), **work distribution** across many workers, **integration** between services through commands and events, and **request buffering** in front of slow systems. RabbitMQ's native protocol is **AMQP 0-9-1**; it also speaks **AMQP 1.0** (a core protocol since RabbitMQ 4.0), **MQTT** and **STOMP** through plugins, and has its own **stream protocol** for streams. It is written in Erlang, known for reliability, and offers flexible routing, acknowledgements, dead-lettering and replicated queues.

## Producers, broker and consumers

Producers hand messages to the broker; consumers take them when ready.

![Three sender boxes on the left with arrows into a central rounded broker containing stacked message bars, and arrows out to three worker boxes on the right.](assets/figures/rabbitmq/section-1-map.svg) — Figure 1.1 — A broker decoupling producers from consumers.

## Without and with a queue

The web request returns immediately; a worker sends the email later.

```python
# without a broker: the user waits for the slow SMTP call
def sign_up(user):
    save(user)
    send_welcome_email(user)          # 2 seconds, and fails if SMTP is down
    return "ok"

# with RabbitMQ: publish a message and return
def sign_up_queued(user, channel):
    save(user)
    channel.basic_publish(
        exchange="",
        routing_key="emails.welcome",
        body=json.dumps({"userId": user.id, "email": user.email}),
    )
    return "ok"                       # a worker consumes emails.welcome separately
```

## A queue is not a database

Brokers are built to move messages, not to store them for months or query them. Keep queues short in normal operation and keep business records in a database.

**Quiz:** Which is a typical use of RabbitMQ?

- [x] Distributing background jobs across many workers
- [ ] Long-term analytical storage
- [ ] Serving static web pages
- [ ] Replacing a relational database

*Answer:* Distributing background jobs across many workers. Work queues that spread jobs across workers are a classic RabbitMQ use case.
