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:
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:
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
.
I hope this may help you. See you! 😄