concept

The first thing to understand is what a dead letter queue is.

The official line on the private message queue is:

Messages from a queue can be “dead-lettered”; that is, republished to an exchange when any of the following events occur

  • The message is negatively acknowledged by a consumer using basic.reject or basic.nack with requeue parameter set to false.
  • The message expires due to per-message TTL; or
  • The message is dropped because its queue exceeded a length limit

Note that expiration of a queue will not dead letter the messages in it.

Dead letter exchanges (DLXs) are normal exchanges. They can be any of the usual types and are declared as usual.

For any given queue, a DLX can be defined by clients using the queue’s arguments, or in the server using policies. In the case where both policy and arguments specify a DLX, the one specified in arguments overrules the one specified in policy.

A message with a dead state will be sent to a specified queue, this queue is a normal queue, according to its function, we call it a dead letter queue.

A message is sent to a dead-letter queue when:

  1. The message is received by the consumer and marked reject or nack, rejected or unconsumed.
  2. The queue sets the message lifetime. If the message is not consumed after the lifetime, it is automatically sent to the dead letter queue.
  3. When the queue is full, messages sent to the queue are sent to the dead letter queue.

The official introduction and usage page of the dead letter queue is:

Direct messages queue introduction page: www.rabbitmq.com/dlx.html

Message to refuse mechanism: www.rabbitmq.com/confirms.ht…

Survival time introduce page: www.rabbitmq.com/ttl.html

Queue length is introduced page: www.rabbitmq.com/maxlength.h…

Dead letter queue binding

The first thing to note, including the official queue, is that the dead letter queue is a normal queue, and you can create, connect, and consume messages in the queue in the same way that normal queues are created.

So you create a queue, you create this exchange of this queue, and then you create a dead letter queue that works. You can mark it as a dead-letter queue with names such as _dlx,.dlx, etc.

Then create a queue that you use as a message queue in the schema, or as a broker. During the creation process, set parameters to bind this queue to a dead-letter queue that will automatically send dead-letter messages when they are generated.

When creating the queue, set two parameters in the figure, one is exchange bound to the dead-letter queue, and the other is the routing key bound to the dead-letter queue

Dead letter queue use

The piKA library used by RabbitMQ is connected to Python, and the queue and connection can be consumed. Either channel.basic_rejact(delivery_tag, requeue=False) or channel.basic_nack(delivery_tag=None, Multiple =False, Requeue =False) methods send messages to the dead-letter queue.

For messages in a dead-letter queue, a separate connection can be used for processing. Consumption failure messages may be messages with data problems. The problems may be diverse and cannot be processed with the agreed logic. At this time, it is necessary to check and confirm the causes of message consumption failure, and analyze the specific problems one by one.

The part of the PIKA document sent to the private message queue:

Rejact: pika. Readthedocs. IO/en/stable/m…

Nack: pika. Readthedocs. IO/en/stable/m…