Fork me 🍴

Willian Antunes

Prevent Message Loss in RabbitMQ: Learn Alternate Exchanges by practice

• 2 minute read

rabbitmq, amqp, pika

CloudAMQP has a very interesting diagnostics tool. It informs you if how you deal with your broker is healthy or not, and it gives you suggestions for things to improve. One crucial rule to pay attention to is that all published messages are routed successfully. If your system is complex, it might be challenging to identify which application is sending messages without routing, causing the message to be discarded. To avoid that, we can use Alternate Exchange. Let's learn it by practice? Download this project and execute the following:

docker compose up create-alternate-exchange

The script creates a policy, followed by the alternate exchange, a queue, and its binding. It creates 4 exchanges, though these two are the ones we should pay attention:

Two exchanges are highlighted: unrouted.xpto.exchange, and xpto.exchange. It image highlights the applied policy.

The exchange named xpto.exchange is the one the application uses in production to send messages to. The unrouted.xpto.exchange is the alternate exchange for it. Now execute the following:

docker compose up publish-messages-indefinitely

It publishes to the exchange xpto.exchange using the routing key order_created. You'll see the queue listener.xpto.exchange receiving messages:

The queue listener.xpto.exchange is highlighted. It has 224 messages and continues receiving more messages.

Let's delete this queue. The idea is to force the messages to be routed to the alternate exchange. Open another terminal and now execute the following:

docker compose up delete-queue

Now are the published messages are being routed to the queue listener.unrouted.xpto.exchange.

The queue listener.xpto.exchange has been deleted, now the queue listener.unrouted.xpto.exchange is receiving the messages.

I hope this may help you. See you! 😄


Have you found any mistakes 👀? Feel free to submit a PR editing this blog entry 😄.