rabbitmqrabbitmq-exchange

RabbitMQ: fanout exchange with no message loss?


RabbitMQ:  version 3.11.2

I wish to configure a fanout exchange for which there will be two consumers and a single producer. Each of the two consumers can go offline for several minutes at a time; in the worst case scenario, one of the consumers could go offline for hours.

QUESTION: how should the fanout exchange and/or consumer queues be configured such that no message is ever lost, even if and when one of the consumers goes offline for several minutes or hours?


Solution

  • It is enough to bind queues to your exchange.

    See here for more details https://www.rabbitmq.com/tutorials/tutorial-three-python.html

    result = channel.queue_declare(queue='') At this point result.method.queue contains a random queue name. For example it may look like amq.gen-JzTY20BRgKO-HjmUJj0wLg.

    Secondly, once the consumer connection is closed, the queue should be deleted. There's an exclusive flag for that:

    result = channel.queue_declare(queue='', exclusive=True) channel.queue_bind(exchange='logs', queue=result.method.queue)

    Note: If you need to handle the offline consumers, you need persistent queues and persistent messages instead of exclusive and auto-delete queues