activemq.org › Handbook › Dead letter queue (DLQ): how ActiveMQ handles messages it cannot deliver
HandbookDead letter queue (DLQ): how ActiveMQ handles messages it cannot deliver
A dead letter queue (DLQ) is a queue where a broker moves messages it could not deliver successfully, so a message that keeps failing stops being retried and can be inspected later. In ActiveMQ Classic the default DLQ is a single queue named ActiveMQ.DLQ, and a message is sent there once its redelivery count passes the redelivery policy's maximumRedeliveries, which defaults to 6.
This page covers the general idea, then the ActiveMQ Classic details: when redelivery happens, the redelivery policy and its defaults, per-queue DLQs with individualDeadLetterStrategy, and the other dead letter options in activemq.xml. It ends with the equivalent settings in Apache Artemis (formerly ActiveMQ Artemis) and RabbitMQ. The official references are on activemq.apache.org and artemis.apache.org.
What is a dead letter queue?
Some messages fail every time: the payload is malformed, a field the consumer needs is missing, or the code hits a bug. If the broker simply put such a message back on its queue after each failure, it would be delivered again and again, wasting CPU and network and getting in the way of healthy messages. The Artemis documentation describes exactly this risk of a message remaining in its queue indefinitely and clogging the system.
A dead letter queue breaks the loop. The broker counts failed deliveries, and after a set limit it moves the message out of the normal flow into a separate destination. Nothing is lost, the main queue keeps moving, and you can examine the failed message, fix the cause and resend it, or discard it.
Brokers name the idea differently:
- ActiveMQ Classic: dead letter queue, by default
ActiveMQ.DLQ. - Apache Artemis: dead letter address, configured per address.
- RabbitMQ: dead letter exchange, where dead-lettered messages are republished.
When does ActiveMQ send a message to the DLQ?
In ActiveMQ Classic, a message is redelivered to a client when any of these happens:
- A transacted session calls
rollback(). - A transacted session is closed before
commit()is called. - A session using
CLIENT_ACKNOWLEDGEcallsSession.recover(). - The client connection times out, for example because processing takes longer than the configured timeout.
Redelivery is counted on the client side. The broker sends its preferred default redelivery policy to each client connection, and the client can override it. Once a message's redelivery attempts exceed maximumRedeliveries, the client sends a "poison ack" back to the broker, and the broker moves the message to the dead letter queue.
By default every undeliverable message from every destination goes to the one queue, ActiveMQ.DLQ, which the ActiveMQ documentation itself notes can be hard to manage. Three defaults are worth knowing:
- Expired messages are sent to the DLQ unless you tell the dead letter strategy to discard them.
- Non-persistent messages are not placed on the DLQ when they cannot be delivered.
- Messages in the DLQ never expire.
The DLQ is an ordinary queue, so you watch its depth like any other queue. See ActiveMQ monitoring and the web console.
Redelivery policy and the default maximum redeliveries
The redelivery policy decides how many times, and how quickly, a failed message is retried before it becomes a poison message. You set it on the ActiveMQConnectionFactory or ActiveMQConnection, in Java code, in Spring, or in the connection URI. These are the documented properties and defaults:
| Property | Default | Meaning |
|---|---|---|
maximumRedeliveries | 6 | Redeliveries before the message is treated as a poison pill and returned to the broker for the DLQ; -1 means unlimited |
initialRedeliveryDelay | 1000 ms | Delay before the first redelivery |
redeliveryDelay | 1000 ms | Delay used when initialRedeliveryDelay is 0 (from 5.4) |
useExponentialBackOff | false | Grow the delay after each attempt |
backOffMultiplier | 5 | Factor for exponential back-off |
maximumRedeliveryDelay | -1 | Cap on the delay with exponential back-off; -1 means no cap (from 5.5) |
useCollisionAvoidance | false | Randomise delays so consumers do not retry in lockstep |
collisionAvoidanceFactor | 0.15 | Range of that randomisation |
From the ActiveMQ Classic "Message Redelivery and DLQ Handling" page, overriding the policy in Java on a connection:
RedeliveryPolicy policy = connection.getRedeliveryPolicy();
policy.setInitialRedeliveryDelay(500);
policy.setBackOffMultiplier(2);
policy.setUseExponentialBackOff(true);
policy.setMaximumRedeliveries(2);
From the ActiveMQ Classic "Connection Configuration URI" page, the same kind of setting in a broker URL, using the jms.redeliveryPolicy. prefix:
tcp://localhost:61616?jms.prefetchPolicy.all=100&jms.redeliveryPolicy.maximumRedeliveries=5
Since ActiveMQ 5.7.0 the connection factory also exposes a redeliveryPolicyMap, so you can give different destinations, or wildcards such as all queues and all topics, their own policy.
One DLQ per queue: individualDeadLetterStrategy
To stop everything piling into ActiveMQ.DLQ, set an individualDeadLetterStrategy in the destination policy map of activemq.xml. It gives each queue or topic its own dead letter queue, named with a prefix you choose, and a wildcard lets one entry cover every queue. This is the example from the ActiveMQ Classic "Message Redelivery and DLQ Handling" page:
<broker>
<destinationPolicy>
<policyMap>
<policyEntries>
<!-- Set the following policy on all queues using the '>' wildcard -->
<policyEntry queue=">">
<deadLetterStrategy>
<!--
Use the prefix 'DLQ.' for the destination name, and make
the DLQ a queue rather than a topic
-->
<individualDeadLetterStrategy queuePrefix="DLQ." useQueueForQueueMessages="true"/>
</deadLetterStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
</broker>
With this policy, failed messages from a queue called orders go to DLQ.orders, and useQueueForQueueMessages="true" makes that DLQ a queue rather than a topic.
The conf/activemq.xml shipped with ActiveMQ 6.4.0 already contains a <destinationPolicy> block with a policyEntry for all topics (topic=">"). Add your queue entry to that existing <policyEntries> list rather than creating a second <destinationPolicy>.
Other dead letter strategy options
The same <deadLetterStrategy> element accepts more settings, all documented on the same ActiveMQ page. Its examples set the first two on a sharedDeadLetterStrategy element.
| Setting | Effect |
|---|---|
processExpired="false" | Discard expired messages instead of sending them to the DLQ |
processNonPersistent="true" | Also send undeliverable non-persistent messages to the DLQ |
expiration="300000" (from 5.12) | Expire messages in the DLQ after the given milliseconds; apply it only to specific queues, never through a default or catch-all wildcard entry, or a DLQ entry can loop between DLQs |
enableAudit="false" | Turn off the message audit, which is on by default and stops duplicates being added to the DLQ; from 5.15.0 its limits are set with maxProducersToAudit and maxAuditDepth |
<discarding/> (from 5.9) | Drop dead letters for that destination instead of storing them |
For dropping dead letters by regular expression across many destinations, ActiveMQ also has the discardingDLQBrokerPlugin, configured under <plugins>.
Broker-side redelivery
Normally the consumer handles redelivery, which preserves message order but keeps the retries on that one consumer while the message shows as in flight. Since 5.7 you can instead have the broker resend a failed message after a delay, through a redelivery broker plugin that uses the scheduler. The ActiveMQ documentation positions it for cases where total message order matters less than throughput and spreading load across consumers. It needs schedulerSupport="true" on the broker. Its sendToDlqIfMaxRetriesExceeded and fallbackToDeadLetter options both default to true, so normal DLQ processing still applies when retries run out or no policy matches.
Dead letter address in Apache Artemis
Apache Artemis uses a dead letter address instead of a DLQ, set in an address-setting in broker.xml. After the configured number of unsuccessful delivery attempts the message is removed from its queue and sent to that address. From the Artemis "Message Redelivery and Undelivered Messages" page:
<!-- undelivered messages in exampleQueue will be sent to the dead letter address
deadLetterQueue after 3 unsuccessful delivery attempts -->
<address-setting match="exampleQueue">
<dead-letter-address>deadLetterAddress</dead-letter-address>
<max-delivery-attempts>3</max-delivery-attempts>
</address-setting>
Key Artemis defaults from that page:
- Messages are redelivered at most 10 times by default;
max-delivery-attemptsof -1 means unlimited. - If no
dead-letter-addressis set, messages are removed after the maximum attempts. redelivery-delayis 0 by default, so there is no wait between attempts unless you set one.auto-create-dead-letter-resources(false by default) makes the broker create one dead letter queue per original address, with the prefixDLQ.unless you change it.
The broker.xml template in the Artemis 2.57.0 source code sets <dead-letter-address>DLQ</dead-letter-address> in its catch-all match="#" address setting and declares a DLQ address with a DLQ queue. For the full option list, see the Artemis documentation on artemis.apache.org.
RabbitMQ: dead letter exchanges
RabbitMQ applies the same idea at the exchange level. A message is dead-lettered, meaning republished to a dead letter exchange, when a consumer rejects it without requeueing, when it expires through per-message TTL, when its queue exceeds a length limit, or when a quorum queue returns it more times than its delivery limit. You set the exchange with a policy or queue argument named dead-letter-exchange. For how RabbitMQ compares with ActiveMQ more broadly, see ActiveMQ vs RabbitMQ.
Common problems
- Everything lands in ActiveMQ.DLQ and you cannot tell where it came from. Switch to
individualDeadLetterStrategyso each queue gets its own DLQ. - The DLQ fills with expired messages. Set
processExpired="false"on the dead letter strategy so expired messages are discarded. - Failed non-persistent messages vanish. That is the default; set
processNonPersistent="true"if you want them kept. - The DLQ only grows. DLQ messages never expire by default. Consume, resend or purge them on a schedule, or set
expirationon specific DLQ policies. - Retries hammer a failing database. Raise
initialRedeliveryDelay, turn onuseExponentialBackOffand cap it withmaximumRedeliveryDelay. - Messages in Artemis disappear after a few failures. Check that a
dead-letter-addressmatches the address; without one, messages are dropped aftermax-delivery-attempts.
Messages that are processed twice are a related but separate problem; see acknowledgement modes and XA transactions.
Frequently asked questions
What is a dead letter queue?
A dead letter queue is where a broker moves messages it could not deliver after a set number of attempts. The failed message stops being retried and can be inspected, fixed and resent, or discarded.
What is ActiveMQ.DLQ?
ActiveMQ.DLQ is the default dead letter queue in ActiveMQ Classic. Unless you configure an individual dead letter strategy, undeliverable messages from every destination are sent to this one queue.
How many times does ActiveMQ redeliver a message before sending it to the DLQ?
The redelivery policy's maximumRedeliveries defaults to 6. When a message's redelivery attempts exceed it, the client sends a poison ack and the broker moves the message to the DLQ. Setting it to -1 means unlimited redeliveries.
How do I give each queue its own DLQ in ActiveMQ?
Add an individualDeadLetterStrategy to a policyEntry in the destinationPolicy section of activemq.xml, for example with queuePrefix DLQ. and useQueueForQueueMessages true for all queues. Each queue then gets a DLQ named with that prefix.
Why are failed messages not reaching the DLQ?
ActiveMQ Classic does not put undeliverable non-persistent messages on the DLQ by default; set processNonPersistent to true to change that. In Apache Artemis, messages are removed after max-delivery-attempts if no dead-letter-address is configured.
Related guides
- What is a message queue? Queues, topics and publish/subscribe
- ActiveMQ monitoring with JMX, Jolokia and Prometheus
- ActiveMQ web console: URL, login and password
- ActiveMQ Artemis vs ActiveMQ Classic: differences and migration
- What is an XA transaction? Two-phase commit with JMS and ActiveMQ
- All handbook guides
Sources
- ActiveMQ: Message Redelivery and DLQ Handling activemq.apache.org
- ActiveMQ: Redelivery Policy activemq.apache.org
- ActiveMQ: Connection Configuration URI activemq.apache.org
- ActiveMQ 6.4.0 default conf/activemq.xml (GitHub) raw.githubusercontent.com
- Apache Artemis: Message Redelivery and Undelivered Messages artemis.apache.org
- Apache Artemis 2.57.0 broker.xml template (GitHub) raw.githubusercontent.com
- RabbitMQ: Dead Letter Exchanges www.rabbitmq.com
- Apache Artemis news artemis.apache.org