1. Reliable delivery of messages

When using RabbitMQ, the sender wants to prevent any message loss or delivery failure.

RabbitMQ gives us two ways to control the delivery reliability of messages.

Confirm confirmation mode: When messages are sent from producer to Exchange, the confirm method in confirmCallback is executed.

Return Return mode: After a message is sent to the Exchange, the Exchange performs ReturnCallBack if it fails to route the message to the queue.

Confirm mode returns {1. Related configuration information. 2. Failure cause}

Fallback mode returns {1. Message object,2. Error code,3. Error message,4. Switch name,5. Routing key}

2.Consumer ACK

Ack means Acknowledge. Indicates the confirmation mode of the consumer after receiving the message.

RabbitMQ provides three authentication methods:

Automatic acknowledgement: Acknowledge =” None “When a message is received by a Consumer, it is automatically acknowledged and removed from RabbitMQ’s message cache.

Manual acknowledgement: acknowledge=”manual” If an exception occurs, the channel.basicnack () method is called to resend the message automatically.

Acknowledge anomalies: Acknowledge =”auto”

3. Message Expiration (TTL)

TTL Full name Time To Live (Live Time/Expiration Time). When a message reaches the lifetime, it is automatically cleared if it has not been consumed.

RabbitMQ can set expiration times for messages and entire queues.

Message expiration, which can be done uniformly or as a separate message.Copy the code

4. Dead-letter queues

Dead letter queue (DLX). Dead Letter Exchange. When a message becomes a Dead Message, it can be re-sent to another switch, the DLX.

There are three ways a message can become dead-letter:

1. The length of queue messages reaches the limit.

2. The consumer rejects the consumption message and does not put the message back into the original target queue;

3. The original queue has message expiration Settings, and the message expiration time is not consumed.

Dead letter queues and dead letter switches:

Dead letter queues and dead letter switches are exactly the same as normal queues and switches, without any difference!!Copy the code

How to bind a queue to a dead-letter switch by setting the following parameters for the queue:

  • X-dead-letter-exchange: Sets the dead-letter exchange

  • X-dead-letter-routing-key: sets the dead-letter routing key

5. Delay queue

Messages are not consumed immediately after they are queued, but only after a specified time has passed. Such as:

Delay queuing is a powerful feature, but it is not provided in RabbitMQ.

You can use the TTL(message expired)+ dead-letter queue combination to achieve the effect of delay queue.

The realization flow chart is as follows:

6. Traffic limiting on the consumer end

When the system peaks are high, we can use RabbitMQ to load the peaks and valleys so that our system can handle requests more smoothly

Implementation steps

  1. Set the AKC mechanism to manual confirmation

  2. Configuring the Listening Container

7.RabbitMQ application problems (message compensation mechanism)

With the previous message reliability delivery, ACK acknowledgement mechanism, and dead-letter queue, we have almost guaranteed message delivery success!

Why a message compensation mechanism? Will the message be lost, yes, the system is in a complex environment, do not think too simple, although the above three schemes, the basic can ensure the high availability of the message is not lost, but as a programmer with pursuit, to absolutely ensure the stability of my system, there is a sense of crisis.

For example, when a persistent message is saved to a disk, the current queue node hangs up and the disk of the storage node fails again, and the message is lost. What can I do?

Production line network environment is too complex, so I do not know too many, so to do message compensation mechanism!

The message compensation mechanism needs to be built on top of the business database and MQ database, and when we send messages, the message data needs to be stored in the database at the same time, and the state of both must be recorded. Then the business database is compared with the MQ database to check whether the consumption is successful. If not, the message compensation measures are taken and the message processing is resend