What happens to fetched messages when RabbitMQ consumer crashes?

谁都会走 提交于 2020-01-14 09:53:06

问题


If I have a RabbitMQ consumer that retrieves 100 messages in bulk, but then it crashes before it can mark those messages as processed, are those messages lost? I want every message in the queue to be processed at least once. What's the recommended approach to deal with consumers that crash before they've acknowledged messages?

Does RabbitMQ put them back on the queue somehow, or what do I need to do to make it happen?


回答1:


What's the recommended approach to deal with consumers that crash before they've acknowledged messages?

Let the rabbitmq do everything for you - messages that are not acknowledged are re-queued and will be delivered again to another (or even same) consumer.

Does RabbitMQ put them back on the queue somehow, or what do I need to do to make it happen?

See answer to first question. Simply don't acknowledge the messages before they're processed. This means also making sure that auto_ack flag is not set!

If I have a RabbitMQ consumer that retrieves 100 messages in bulk, but then it crashes before it can mark those messages as processed, are those messages lost?

See answer above - they are lost if they're automatically acknowledged.
Just a bit of reference, quoting from second tutorial:

If a consumer dies (its channel is closed, connection is closed, or TCP connection is lost) without sending an ack, RabbitMQ will understand that a message wasn't processed fully and will re-queue it. If there are other consumers online at the same time, it will then quickly redeliver it to another consumer. That way you can be sure that no message is lost, even if the workers occasionally die.




回答2:


If the consumer has to acknowledge the message received, and it dies before it does that, the following things will happen:

  1. RabbitMQ broker will know that the consumer is not going to acknowledge, because the channel is closed.
  2. It will mark the message as undelivered.
  3. The order of message in the queue is preserved.
  4. The messages in the queue will be sent to the consumer, when it connects next time.

Point 3 will be void if you either have multiple consumers for the same queue or your RabbitMQ broker version is less than 2.7.0.

Hope it helps.



来源:https://stackoverflow.com/questions/42375034/what-happens-to-fetched-messages-when-rabbitmq-consumer-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!