问题
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:
- RabbitMQ broker will know that the consumer is not going to acknowledge, because the channel is closed.
- It will mark the message as undelivered.
- The order of message in the queue is preserved.
- 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