What is message acknowledgement (ack/nack) in RabbitMQ and why does it matter?
Learn what message acknowledgement (ack/nack) means in RabbitMQ, auto-ack vs manual ack, requeue and dead-letter behavior, with code examples.
Expected Interview Answer
Message acknowledgement is the signal a consumer sends back to RabbitMQ to confirm a message was successfully processed (ack) or could not be processed (nack/reject), so the broker knows whether to remove the message or requeue it.
When acknowledgements are enabled (manual ack mode), RabbitMQ holds a delivered message as unacknowledged until it receives an ack, then it deletes it. If the consumer sends a nack or reject (with requeue), or dies before acking, the broker requeues or redelivers the message so it is not lost. This guarantees at-least-once delivery. With auto-ack, messages are considered delivered the instant they leave the broker, which is faster but risks losing messages if the consumer crashes mid-processing.
- Prevents message loss if a consumer crashes
- Guarantees at-least-once processing
- Requeues failed messages for retry
- Lets slow consumers control their own pace
- Combined with prefetch, enables safe load balancing
AI Mentor Explanation
Acknowledgement is like the umpire signaling that a run is confirmed only after both batters complete it. Until the signal, the run is pending. If a batter is run out mid-way (a nack), the run is cancelled and the situation reset. Only the umpire's confirmed signal (ack) lets the scorer permanently record it and move on to the next ball.
Step-by-Step Explanation
Step 1
Enable manual ack
Consume with noAck set to false so RabbitMQ waits for an explicit acknowledgement.
Step 2
Deliver as unacknowledged
The broker sends the message and marks it unacknowledged, keeping a copy until it hears back.
Step 3
Process the message
The consumer does its work — writing to a database, calling an API, etc.
Step 4
Ack on success
The consumer calls ack, and the broker permanently removes the message from the queue.
Step 5
Nack/reject on failure
On error the consumer nacks or rejects; with requeue the message is redelivered, otherwise it is dropped or dead-lettered.
What Interviewer Expects
- Difference between ack, nack, and reject
- Understanding auto-ack vs manual ack trade-offs
- How acks provide at-least-once delivery
- What happens when a consumer dies before acking
- Awareness of requeue and dead-letter behavior
Common Mistakes
- Thinking auto-ack is safe for critical work
- Forgetting to ack, causing unacked messages to pile up
- Acking before processing finishes, risking loss on crash
- Assuming nack always requeues even when requeue is false
- Confusing acknowledgement with confirms (publisher confirms are separate)
Best Answer (HR Friendly)
“Acknowledgement is the way a worker tells RabbitMQ 'I finished this message, you can delete it,' or 'I failed, please try again.' It matters because it stops messages from being lost when a worker crashes, so important work always gets done at least once.”
Code Example
const amqp = require('amqplib')
async function consume() {
const conn = await amqp.connect('amqp://localhost')
const ch = await conn.createChannel()
await ch.assertQueue('tasks', { durable: true })
ch.prefetch(1) // one unacked message at a time
ch.consume('tasks', async (msg) => {
try {
await handle(msg.content.toString())
ch.ack(msg) // success: remove from queue
} catch (err) {
ch.nack(msg, false, true) // failure: requeue for retry
}
}, { noAck: false })
}
consume()Follow-up Questions
- What is the difference between nack and reject?
- How does prefetch (QoS) interact with acknowledgements?
- What are dead-letter queues and when are messages sent there?
- How do publisher confirms differ from consumer acks?
- What happens to unacknowledged messages when a consumer connection drops?
MCQ Practice
1. What does an ack tell RabbitMQ?
An ack confirms successful processing, so the broker deletes the message from the queue.
2. What is the risk of using auto-ack?
Auto-ack removes messages the moment they are delivered, so a crash before processing loses them.
3. What happens when a consumer nacks a message with requeue set to true?
A nack with requeue=true returns the message to the queue so it can be redelivered and retried.
Flash Cards
What is a message ack? — A signal from the consumer confirming successful processing so the broker removes the message.
What is a nack/reject? — A signal that processing failed; with requeue the message is redelivered, otherwise dropped or dead-lettered.
Auto-ack vs manual ack? — Auto-ack removes messages on delivery (fast, risky); manual ack waits for confirmation (safe, at-least-once).
What happens if a consumer dies before acking? — The message stays unacknowledged and RabbitMQ redelivers it to another consumer.