Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A @RabbitListener throws an exception for one malformed message and the service's CPU goes to 100 percent while the same message is logged as failing thousands of times per second. Explain why and how to fix it.

In Spring AMQP's default AUTO acknowledge mode, an exception from the listener makes the container reject the message, and by default a rejected message is requeued. It goes back into the queue, is delivered again at once, fails again, and the loop runs thousands of times a second: a poison message. We fix it by not requeuing permanent failures, with default-requeue-rejected=false or AmqpRejectAndDontRequeueException, by giving the queue a dead-letter exchange so rejected messages are kept, by retrying transient errors with back-off, and, on quorum queues, by relying on a delivery limit.

Why this question is asked

A single bad message that takes a service to 100 percent CPU is one of the most common RabbitMQ incidents, and it often appears right after a producer change sends a field in a new format. The service is not down, but it does no useful work, the logs grow by gigabytes, and every other message in the queue waits. Interviewers ask this because the explanation shows whether we understand acknowledgements, and the fix shows whether we know Spring AMQP's error handling. In this answer, we will recap acknowledgement modes, follow the loop step by step, fix it in configuration and code, add a dead-letter exchange and retries, and finish with the quorum queue delivery limit.

1A Quick Recap of Acknowledgements in Spring AMQP

When RabbitMQ delivers a message to a consumer, the message stays in the queue as unacknowledged until the consumer answers. An ack removes it. A reject, or a nack, tells the broker the consumer did not process it, and comes with a requeue flag: requeue true puts the message back in the queue to be delivered again, requeue false discards it or, if the queue has a dead-letter exchange, dead-letters it.

Spring AMQP listener containers default to AUTO acknowledge mode. The container calls our @RabbitListener method; if it returns normally the container acks, and if it throws the container rejects. Whether that reject requeues is decided by the container's defaultRequeueRejected setting, which is true by default.

2Following the Loop

Suppose a producer starts sending amounts as "12,50" instead of 12.50, and our listener's own parsing code throws a NumberFormatException for one message. The container rejects it with requeue true. RabbitMQ puts the message back into the queue, for a classic queue at its original position near the head, and since our consumer is idle and has prefetch capacity, the broker delivers it again within a millisecond. It fails again, is requeued again, and so on.

Java
@RabbitListener(queues = "orders")
void onOrder(String json) {
OrderDto dto = mapper.readValue(json, OrderDto.class);
BigDecimal amount = new BigDecimal(dto.amount()); // "12,50" -> NumberFormatException
orders.save(dto.toOrder(amount));
}
// log: Execution of Rabbit message listener failed ... NumberFormatException (x 5,000 per second)
// container: basic.reject(requeue=true) -> message redelivered immediately
Step throughThe poison message loop
  1. Broker delivers the messageredelivered flag set after the first time
  2. Listener throwssame exception every time
  3. Container rejectsdefaultRequeueRejected = true
  4. Broker requeues itand the consumer has prefetch capacity
  5. Anything different next time?
    YesWould succeedNoLoop again, thousands per second

5 steps in order. Play them, or step through them one at a time.

Nothing in the loop waits, so it runs as fast as the CPU and network allow, and each iteration writes a stack trace to the log. Depending on queue type and timing, other messages may be delivered in between, but the poison message keeps consuming most of the capacity forever, because nothing ever changes on the next attempt.

3Stopping Requeue for Permanent Failures

The simplest fix is to stop requeuing rejected messages, either for the whole application or for specific exceptions. In Spring Boot one property changes the container default. Alternatively, the listener throws AmqpRejectAndDontRequeueException when it knows the failure is permanent, which tells the container to reject without requeue even when the default is true.

Java
# application.properties: rejected messages are never requeued
spring.rabbitmq.listener.simple.default-requeue-rejected=false
// Or decide per failure in the listener
@RabbitListener(queues = "orders")
void onOrder(String json) {
OrderDto dto = parse(json);
try {
orders.save(dto.toOrder(new BigDecimal(dto.amount())));
} catch (NumberFormatException e) {
throw new AmqpRejectAndDontRequeueException("bad amount " + dto.amount(), e); // not requeued
}
}

Notice that Spring AMQP already treats some failures as fatal. Its ConditionalRejectingErrorHandler rejects without requeue when the exception is, for example, a MessageConversionException raised while converting the message before our method is called. So a body that cannot be converted at all usually does not loop; the loop typically comes from exceptions in our own code, as in this example. Either way, without a dead-letter exchange the rejected message is simply dropped, so the next step is to keep it.

4Keeping Rejected Messages and Retrying Transient Errors

We configure a dead-letter exchange on the orders queue, by queue argument or broker policy, and bind a queue such as orders.dlq to it. Now a rejected message with requeue false is moved to orders.dlq, with an x-death header recording why, and we can inspect it, fix the producer and replay it.

Not every failure is permanent, though. If the database is briefly unavailable, retrying a few seconds later will work. Spring Boot can add a stateless retry interceptor to the container: spring.rabbitmq.listener.simple.retry.enabled=true with, for example, max-attempts=3, initial-interval=1s and multiplier=2. The container retries inside the consumer thread with back-off, and when attempts are exhausted the default recoverer rejects without requeue, so the message goes to the dead-letter queue. Notice the trade-off: during back-off the thread is busy, so long delays belong in broker-side retry queues instead.

Where a failing message ends up
ConfigurationResult
Defaults (requeue true)redelivered at once, loops forever
default-requeue-rejected=false, no DLXrejected and dropped
requeue false + dead-letter exchangemoved to orders.dlq with x-death
retry 3 attempts + DLX1 s, 2 s, then orders.dlq
quorum queue with delivery limitdead-lettered after the limit

5The Quorum Queue Delivery Limit

Quorum queues add a safety net at the broker. They count deliveries of each message, and when a message has been returned more times than the delivery limit, the broker dead-letters it, or drops it if there is no dead-letter exchange, instead of redelivering it. Since RabbitMQ 4.0 quorum queues have a default delivery limit of 20; on older versions we set x-delivery-limit or a policy ourselves; check your version. This turns an endless loop into at most 20 attempts, even if an application forgets to configure its container correctly. It is a good default, but it should back up proper application error handling, not replace it.

6Follow-up questions

How would you find and handle the messages already in the dead-letter queue?

I would alert on the dead-letter queue's depth so someone looks as soon as messages arrive. The x-death header tells us the original queue, the reason and the count, and the logged exception tells us the cause. After fixing the producer or the consumer, we replay the messages to the original exchange with a small tool or the shovel plugin, ideally at a controlled rate, and only after confirming the consumer is idempotent.

Why did CPU go to 100 percent and not just the log volume?

Because the loop has no delay anywhere. Each round trip is a delivery, a deserialization, an exception with a stack trace and a reject, repeated as fast as the broker can redeliver, so the consumer thread and the logging framework run flat out. A back-off or a delivery limit breaks exactly that tight loop.

7Conclusion

In this answer, we followed a poison message loop: in AUTO acknowledge mode a listener exception makes the container reject the message, defaultRequeueRejected is true, so the message is requeued and redelivered immediately, forever. Setting default-requeue-rejected=false or throwing AmqpRejectAndDontRequeueException stops the requeue, a dead-letter exchange keeps the failed messages, retry with back-off handles transient errors before dead-lettering, and the quorum queue delivery limit, 20 by default since RabbitMQ 4.0, caps the loop at the broker.

What to say in the interviewAbout 90 seconds aloud

Open with

The listener container in Spring AMQP uses AUTO acknowledge mode by default: if my method returns, it acks; if it throws, it rejects the message, and by default it rejects with requeue true.

Then, if they want more

So the broker puts the malformed message straight back into the queue and redelivers it immediately, my code throws the same exception again, and that loop runs as fast as the CPU allows, writing a stack trace every time. That is a poison message.

Spring already rejects without requeue for some fatal errors, like a message conversion failure, but an exception from my own code is requeued. To fix it, I would first stop requeuing permanent failures: either set spring.rabbitmq.listener.simple.default-requeue-rejected to false, or throw AmqpRejectAndDontRequeueException when I know the message can never succeed.

Then I make sure the queue has a dead-letter exchange, so rejected messages are moved to a dead-letter queue with an x-death header instead of being lost, and I alert on that queue. For transient errors, like the database being briefly unavailable, I enable listener retry with exponential back-off, say three attempts, after which the message is rejected and dead-lettered.

And if it is a quorum queue, the broker counts deliveries and dead-letters after the delivery limit, which defaults to 20 since RabbitMQ 4, so even a misconfigured consumer cannot loop forever.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
RabbitMQ documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A @RabbitListener throws an exception for one malformed message and the service's CPU...": https://scrivio-interview-library.netlify.app/spring-boot/rabbitmq-and-jms/718466c339/