rabbitmqmessagingfifo

RabbitMQ competing consumers processing 1 message at a time sequentially


Similar to this question, we have FIFO queues and the messages must be processed in order. We want competing consumers from different machines for redundancy and performance reasons, but only one consumer on one machine should handle a message for a given queue at a time.

I tried setting the prefetch count to 1, but I believe this will only work if used with a single machine. Is this possible by default with RabbitMQ or do we need to implement our own lock?


Solution

  • Given a single queue with multiple consumers there is no way to block one of the consumers, all of them receive the messages in round-robin fashion.

    EDIT See https://www.rabbitmq.com/consumers.html#single-active-consumer /EDIT

    You could see this plugin, https://github.com/rabbitmq/rabbitmq-consistent-hash-exchange to distribute the load using different queues.

    I tried setting the prefetch count to 1

    prefetch=1 means that the consumers take one message at a time.

    do we need to implement our own lock

    Yes, If you want one single consumer for queue avoiding other consumers.

    EDIT

    There are also the Exclusive Queues https://www.rabbitmq.com/queues.html#exclusive-queues but note:

    Exclusive queues are deleted when their declaring connection is closed or gone (e.g. due to underlying TCP connection loss). They, therefore, are only suitable for client-specific transient state.