How to use Ack or Nack in Spring AMQP
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Spring AMQP, acknowledgments decide what RabbitMQ should do with a message after delivery. An ack tells the broker processing succeeded and the message can be removed, while a nack tells the broker processing failed and the message should be requeued or discarded based on your choice.
Understand the Acknowledge Modes
Spring listener containers usually run in one of three modes:
- '
AUTO: Spring acknowledges automatically when the listener completes successfully' - '
MANUAL: your code must acknowledge or reject the message' - '
NONE: RabbitMQ treats delivery as auto-ack and no acknowledgments are expected'
For most applications, AUTO is the default because it is simple. Choose MANUAL only when you need explicit control, such as conditional requeue, partial batch handling, or integration with custom retry rules.
Configure Manual Acknowledgment
To use ack and nack directly, configure the listener container for manual mode and receive the delivery tag in the listener method.
Then use Channel.basicAck or Channel.basicNack in the listener:
The last boolean on basicNack matters. true requeues the message. false rejects it so it can be dead-lettered or dropped based on queue configuration.
When to Ack, Nack, or Reject
Use ack when processing is complete and durable enough that you do not want the message again. Use nack with requeue for failures that may succeed later, such as a temporary downstream outage. Use nack without requeue, or basicReject, for poison messages that will fail every time.
That decision should be based on failure type, not on a blanket rule. Requeuing everything often creates redelivery loops and queue churn, especially when the payload is permanently invalid.
If you do not need this level of control, keep AUTO mode and let Spring handle success and failure with the container's error strategy. Manual mode is powerful, but it also makes it easier to forget an acknowledgment path and leave messages unacked.
One good design pattern is to align acknowledgment decisions with your dead-letter setup. Temporary infrastructure failures can be requeued or retried, while invalid payloads should usually be rejected and routed to a dead-letter exchange for inspection instead of bouncing through the same queue forever.
Common Pitfalls
- Turning on
MANUALmode and forgetting to send eitherackornack. - Requeueing every failure and creating an infinite redelivery loop.
- Using manual acknowledgment when the default automatic mode would be simpler.
- Holding on to the
Channeloutside the current listener invocation. - Confusing broker acknowledgment with application-level success semantics.
Summary
- '
ackremoves a successfully processed message from the queue.' - '
nacklets you choose whether to requeue or reject a failed message.' - Use
MANUALmode only when you need explicit acknowledgment control. - Requeue only for failures that are likely to succeed on a later attempt.
- Keep acknowledgment logic aligned with your retry and dead-letter strategy.
Related reading
- How to use Apache kafka with Spring mvc ? Is it possible?
- How to use ArgumentCaptor for stubbing?
- How to use Callable with void return type?
- How to use Class<T> in Java?
- How to use Comparator in Java to sort
- How to use comparison operators like , , on BigDecimal
- How to use ConfigurationProperties with Records?
- How to use Firebase with Spring boot REST Application?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.