# RabbitMQ Tutorial: An Order Queue That Never Loses Work

Originally published at https://practicaldevblog.blogspot.com/2026/11/rabbitmq-python-order-queue.html

## Quick answer

RabbitMQ is a message broker: one program puts a message into a queue, and another takes it out later, so slow work does not block the user. This post publishes three shop orders to a real queue with Python and pika, acknowledges the two good ones, and rejects the broken one into a dead-letter queue. It fits work that must survive a crash, and is overkill for a simple in-process job.

Picture a small online shop. A customer clicks buy, and the checkout code charges the card and updates stock, all inside one web request. Then the payment gateway stalls, the request times out, the customer clicks buy again, and the card is charged twice. A queue breaks the chain. The web request only has to record the order and hand it to RabbitMQ, and a separate worker takes orders one at a time and confirms each one only when the work is done. If the worker crashes halfway, the order is still in the queue, waiting for the next worker.

## What I would check before using it

My first check: must the work survive a crash? If checkout dies halfway, can someone else pick up the order and finish it? When the answer is yes, a durable queue earns its place. When the answer is no, because the job can simply run again from scratch, a queue is ceremony, and a short list in [Redis](https://practicaldevblog.blogspot.com/2026/10/redis-explained-in-memory-store-that.html) may do.

My second check is who runs the broker. RabbitMQ is one more service to install, watch, and back up. It needs free disk space, and stops accepting messages when the disk fills up. If nobody owns that job, a managed queue from your cloud provider is the honest alternative.

My third check is scale. RabbitMQ handles tens of thousands of messages a second on ordinary hardware, enough for most shops and internal tools. It is not the tool for replaying a year of events at massive volume. That is the job [Kafka](https://practicaldevblog.blogspot.com/2026/10/kafka-explained-event-backbone-of-big.html) was built for.

My fourth check is how failures are handled. Every queue needs an answer for the message that can never be processed: a dead-letter queue to park it in, an alert, and a way to fix and replay it. Without that plan, bad messages vanish silently or loop forever.

My verdict, as judgement: adopt RabbitMQ when background work must not be lost, must be retried, and must be spread across workers. Skip it for a single small script or a prototype with no real users. The recommendation flips the moment nobody owns the broker, or the workload turns into an event stream that needs replay.

## Three orders through a real queue

The example is a small shop selling clothes and accessories. Three orders arrive: Ayesha buys a kurta and a dupatta for Rs 12,400, Bilal buys sneakers for Rs 7,800, and Hina buys a handbag and a wallet. Hina's order has a problem: the total was never filled in. That is the kind of bad record that sails through a demo and breaks the first real order.

The script connects to a broker on localhost, declares a durable orders queue with a dead-letter exchange for rejects. Each order is published as JSON, persistent with delivery_mode set to 2. The worker takes orders one at a time and acks each one only after charging it.

```python
"""Order queue demo for The Practical Developer (RabbitMQ, pika).

A small online shop publishes orders to a RabbitMQ queue. A worker
takes them one at a time, charges the customer, and acknowledges each
order. One order arrives without a total, so the worker rejects it and
the broker moves it to a dead-letter queue. A corrected copy of that
order is published and processed cleanly.

Run: python3 rabbitmq_demo.py (RabbitMQ must be listening on localhost:5672)
"""
import json
import pika

QUEUE = "orders"
DLX = "orders_dlx"
DLQ = "orders_dead"


def connect():
    connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
    channel = connection.channel()
    channel.exchange_declare(exchange=DLX, exchange_type="fanout", durable=True)
    channel.queue_declare(queue=DLQ, durable=True)
    channel.queue_bind(exchange=DLX, queue=DLQ, routing_key="")
    channel.queue_declare(
        queue=QUEUE,
        durable=True,
        arguments={"x-dead-letter-exchange": DLX},
    )
    return connection, channel


def queue_depth(name):
    # A fresh connection gives the true count. Checking depth on the same
    # channel that just published returns a stale number in pika 1.4.4
    # (it lagged by one in testing: 1 after three publishes, 2 after the
    # next check), while rabbitmqctl and a new connection both said 3.
    connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
    channel = connection.channel()
    count = channel.queue_declare(queue=name, passive=True).method.message_count
    connection.close()
    return count


def publish(channel, order):
    channel.basic_publish(
        exchange="",
        routing_key=QUEUE,
        body=json.dumps(order),
        properties=pika.BasicProperties(delivery_mode=2),
    )
    print(f"Published order {order['order_id']} for {order['customer']}")


def charge(order):
    if "total" not in order:
        raise ValueError(f"order {order['order_id']} has no total to charge")
    print(f"Charged Rs {order['total']} for order {order['order_id']}")
```

The second half drains the queue, acking each good order and rejecting the broken one without requeueing. Then the corrected order is published and processed.

```python
def drain(channel):
    while True:
        method, _props, body = channel.basic_get(queue=QUEUE, auto_ack=False)
        if method is None:
            return
        order = json.loads(body)
        try:
            charge(order)
            channel.basic_ack(delivery_tag=method.delivery_tag)
            print(f"Acked order {order['order_id']}")
        except ValueError as exc:
            print(f"Rejected order {order['order_id']}: {exc}")
            channel.basic_nack(delivery_tag=method.delivery_tag, requeue=False)


def main():
    connection, channel = connect()
    print("Connected. Queue 'orders' declared (durable, dead-letter exchange set).")

    print("Phase 1: three orders published, one is broken")
    orders = [
        {"order_id": 101, "customer": "Ayesha", "items": ["kurta", "dupatta"], "total": 12400},
        {"order_id": 102, "customer": "Bilal", "items": ["sneakers"], "total": 7800},
        {"order_id": 103, "customer": "Hina", "items": ["handbag", "wallet"]},
    ]
    for order in orders:
        publish(channel, order)
    print(f"Orders waiting: {queue_depth(QUEUE)}")
    drain(channel)
    print(f"After phase 1: orders={queue_depth(QUEUE)}, "
          f"dead-lettered={queue_depth(DLQ)}")

    print("Phase 2: corrected order 103 published and processed")
    publish(channel, {"order_id": 103, "customer": "Hina",
                      "items": ["handbag", "wallet"], "total": 15300})
    drain(channel)
    print(f"After phase 2: orders={queue_depth(QUEUE)}, "
          f"dead-lettered={queue_depth(DLQ)}")
    print("Done. The broken copy of order 103 stays in the dead-letter queue.")
    connection.close()


if __name__ == "__main__":
    main()
```

Real output from the run against RabbitMQ 3.12.1:

```
Connected. Queue 'orders' declared (durable, dead-letter exchange set).
Phase 1: three orders published, one is broken
Published order 101 for Ayesha
Published order 102 for Bilal
Published order 103 for Hina
Orders waiting: 3
Charged Rs 12400 for order 101
Acked order 101
Charged Rs 7800 for order 102
Acked order 102
Rejected order 103: order 103 has no total to charge
After phase 1: orders=0, dead-lettered=1
Phase 2: corrected order 103 published and processed
Published order 103 for Hina
Charged Rs 15300 for order 103
Acked order 103
After phase 2: orders=0, dead-lettered=1
Done. The broken copy of order 103 stays in the dead-letter queue.
```

The thing that went wrong is order 103. The worker tried to charge it, found no total, and rejected it with basic_nack, requeue False, so the broker moved it to the dead-letter queue. A bad message is parked for inspection, not retried forever. The data needs its own fix: the corrected order, with the missing total of Rs 15,300 added, was published again in phase 2 and processed cleanly. The broken copy stays in the dead-letter queue for inspection. The shop API could be built with [FastAPI](https://practicaldevblog.blogspot.com/2026/10/fastapi-explained-python-apis-at-full.html), and the checkout endpoint follows the [Node.js REST API](https://practicaldevblog.blogspot.com/2026/10/build-your-first-rest-api-with-nodejs.html) post.

## Common mistakes and how to fix them

First, acknowledging messages automatically. With auto_ack set to True, RabbitMQ deletes the message the moment it is delivered, so a worker crash mid-charge loses the order for good. The fix is auto_ack set to False and a manual ack only after the work succeeds.

Second, a queue that is not durable, or messages that are not persistent. The symptom is an empty queue after a broker restart, with no error anywhere. The fix is durable set to True on the queue and delivery_mode set to 2 on every publish. Both halves matter.

Third, requeueing a message that can never succeed. Calling basic_nack with requeue set to True on a poison message sends it back to the queue, so the worker fails it again and again while good orders wait behind it. The fix is requeue set to False plus a dead-letter exchange, as the demo does.

Fourth, declaring a plain queue on RabbitMQ 4. The broker closes the connection with pika.exceptions.ConnectionClosedByBroker: (541, 'INTERNAL_ERROR - Feature transient_nonexcl_queues is deprecated'). The fix is durable set to True, or exclusive set to True for a private queue.

## Frequently Asked Questions

Is RabbitMQ free to use? Yes. RabbitMQ is open source under the Mozilla Public License, and running it yourself costs nothing. Only managed hosting costs money.

How is RabbitMQ different from Kafka? RabbitMQ is a queue: a message goes to one worker, then is removed. Kafka is an event log: messages stay for replay by many readers. Queues fit background jobs; Kafka fits large-scale event streaming.

Do I need Docker to run RabbitMQ? No. This demo ran on a broker installed with apt on Ubuntu, with docs for Linux, macOS, and Windows. Docker is optional, not required.

What happens to messages if the broker restarts? A durable queue with persistent messages keeps them. Anything else is lost on restart.

## What to remember

- Declare the queue durable and publish persistent messages, or a restart wipes the queue.
- Acknowledge only after the work succeeds, never before.
- Give every queue a dead-letter exchange, so bad messages are parked, not lost or looped.
- Check queue depth with rabbitmqctl or a fresh connection when the numbers look wrong.
- Own the broker: watch disk space and set an alert before it fills up.

## Where this leaves you

RabbitMQ earns its place the day an order, a payment, or an email must not be lost because a process died at the wrong moment. The demo proved the three things that matter: good orders were charged and acknowledged, the broken order was rejected into a dead-letter queue instead of vanishing, and the corrected order went through cleanly. That is the whole job. Start with one durable queue, one worker, and a dead-letter queue, and watch the dead-letter queue after your first deploy. If it stays empty, your data is clean. If it fills up, you have a list of real problems, which is the better outcome.

## Further reading

- RabbitMQ tutorial, "Hello world!" with Python and pika, https://www.rabbitmq.com/tutorials/tutorial-one-python
- RabbitMQ docs, Dead Letter Exchanges, https://www.rabbitmq.com/docs/dlx
- pika documentation, the Python client for RabbitMQ, https://pika.readthedocs.io/
- Kafka Explained: The Event Backbone of Big Systems, https://practicaldevblog.blogspot.com/2026/10/kafka-explained-event-backbone-of-big.html
- Redis Explained: The In-Memory Store That Speeds Things Up, https://practicaldevblog.blogspot.com/2026/10/redis-explained-in-memory-store-that.html
- FastAPI Explained: Python APIs at Full Speed, https://practicaldevblog.blogspot.com/2026/10/fastapi-explained-python-apis-at-full.html
- Build Your First REST API With Node.js and Express, https://practicaldevblog.blogspot.com/2026/10/build-your-first-rest-api-with-nodejs.html

Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.
