How does RabbitMq prevent message loss?

Producer loss

① The transaction mechanism is enabled, but the performance is not very high

② The asynchronous confirm mechanism is enabled to achieve the highest performance

The RabbitMq lost

To set RabbitMq persistence, there are two steps:

  • When creating a queue, set it to persistent so that RabbitMQ can persist the queue’s metadata, but it will not persist the queue’s data.
  • The second is to send the message when the message is sentdeliveryModeA value of 2 will persist the message, and RabbitMQ will persist the message to disk.

Consumer loss

If RabbitMQ loses data, the main reason is that when you consume it, the process has just been consumed and hasn’t been processed yet, and as a result, the process is suspended or restarted. RabbitMQ assumes that you have consumed all the data and the data is lost.

In this case, use the Ack mechanism provided by RabbitMQ. In simple terms, you must disable the automatic ACK mechanism of RabbitMQ, which can be called through an API, and then ack it in the program every time you make sure it’s done in your own code. That way, if you haven’t done it yet, isn’t there no ACK? RabbitMQ will assume that you haven’t finished processing the message, and will assign it to another consumer. The message won’t be lost.

Consumer code

The average queue

  Dead-letter queue