Quick revision
The short answer
Prefetch, set with basic.qos, limits how many unacknowledged messages the broker pushes to one consumer, and Spring AMQP's default is 250. With slow jobs, a high prefetch parks hundreds of messages on one consumer, where other pods cannot take them, so when that pod restarts they are redelivered late while idle pods had nothing to do. We lower the prefetch, even to 1 for long jobs, accepting a little throughput cost for fast messages, make consumers idempotent because unacknowledged messages are redelivered when the channel closes, and shut down gracefully so in-flight work finishes and no new messages are fetched.
What to remember
In this answer, we saw that prefetch caps the unacknowledged messages per consumer, 250 by default in Spring AMQP, and that with slow jobs it reserves minutes of work on one pod where idle pods cannot reach it. A restart then releases hundreds of redelivered messages late. Lowering the prefetch, even to 1 for long jobs, keeps waiting work in the queue for any free consumer at a small cost for fast messages, idempotent consumers absorb the unavoidable redeliveries, and graceful shutdown with suitable timeouts lets in-flight jobs finish.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Prefetch, set with basic.qos, limits how many unacknowledged messages the broker pushes to one consumer, and Spring AMQP's default is 250. With slow jobs, a high prefetch parks hundreds of messages on one consumer, where other pods cannot take them, so when that pod restarts they are redelivered late while idle pods had nothing to do. We lower the prefetch, even to 1 for long jobs, accepting a little throughput cost for fast messages, make consumers idempotent because unacknowledged messages are redelivered when the channel closes, and shut down gracefully so in-flight work finishes and no new messages are fetched.
Why this question is asked
Prefetch is a setting most teams never change, and for fast messages the default works well. With slow jobs it quietly turns a fair work queue into an unfair one: one consumer hoards messages while others wait. The problem becomes visible during restarts and deployments, when a pod's hoard is suddenly released. Interviewers ask this scenario to see whether we understand flow control between broker and consumer, and how acknowledgements interact with restarts. In this answer, we will recap prefetch, work through the numbers of the incident, choose a better value, look at redelivery and idempotency, and finish with graceful shutdown.
1A Quick Recap of Prefetch
RabbitMQ pushes messages to consumers rather than waiting to be asked. Without a limit, it would push the entire queue to the first consumer. The prefetch count, set per consumer with the AMQP basic.qos command, caps the number of messages delivered to a consumer and not yet acknowledged. With prefetch 250, the broker sends up to 250 messages to the consumer, and sends another one each time the consumer acks one.
Prefetched messages sit in the consumer's local buffer, inside our application. The broker still counts them as unacknowledged in the queue, but they are reserved for that consumer: no other consumer can receive them while the channel is open. Spring AMQP's listener containers use a default prefetch of 250, set in Spring Boot with spring.rabbitmq.listener.simple.prefetch.
2Working Through the Numbers
Suppose each job takes 2 seconds and a pod runs one consumer. With prefetch 250, that consumer has up to 250 jobs reserved, which is 500 seconds, more than 8 minutes of work. If 1,000 jobs arrive in a burst and four pods are running, the first pods to receive messages can take 250 each. A pod that started later, or finished its batch early, may find the queue's ready count at zero, because everything is reserved elsewhere. It sits idle while another pod still has 7 minutes of work in its buffer.
| Consumer | Prefetch 250 | Prefetch 1 |
|---|---|---|
| Pod A (connected first) | 250 reserved, 500 s of work | 1 in progress, takes next when done |
| Pod B | 250 reserved | 1 in progress |
| Pod C | 250 reserved | 1 in progress |
| Pod D (connected last) | 250 reserved, or idle if none left | 1 in progress |
| Pod A restarts | up to 250 redelivered late | at most 1 redelivered |
Now the restart. When a pod is stopped, its channel closes, and every message it had received but not acknowledged returns to the queue with the redelivered flag set. Those are the hundreds of messages in the question. They are only now available to the other pods, minutes after they were published, so their latency jumps even though capacity was free the whole time.
3Choosing a Better Prefetch
The rule of thumb is that prefetch should cover the time it takes for the next message to arrive after an ack, not much more. For fast messages, a few milliseconds each, a high prefetch keeps the consumer busy without waiting for a network round trip per message, which is why 250 is a reasonable default for that case. For a job that takes 2 seconds, the round trip of perhaps 1 ms is irrelevant, so a prefetch of 1 costs almost nothing and gives perfect fairness: each consumer holds only the job it is working on, and everything else stays in the queue for whoever is free.
For mixed workloads we can split queues: fast messages on one queue with a high prefetch, slow jobs on another with prefetch 1 or a small number. If a container runs several concurrent consumers, prefetch applies to each consumer, so concurrency 4 with prefetch 1 holds at most 4 jobs per pod.
4Redelivery Means Consumers Must Be Idempotent
Messages that were prefetched but never started are harmless to redeliver. The dangerous ones are those in progress when the pod stopped: the job may have done its work, such as charging a card or sending an email, but the ack never reached the broker. RabbitMQ cannot know, so it redelivers the message, and another pod runs the job again. That is at-least-once delivery, and it is unavoidable with any setting.
So the consumer must be idempotent: processing the same message twice must have the same effect as once. Typically we give each job a unique id, record processed ids in the database in the same transaction as the job's result, and skip a message whose id is already recorded. The redelivered flag on the message is a hint that a duplicate is possible, but not proof, so we rely on the id check rather than the flag.
5Shutting Down Gracefully
A restart should not abandon work. When the pod receives SIGTERM, Spring's lifecycle stops the listener containers. The container cancels its consumers, so the broker stops sending new messages, and waits for the messages currently being processed to finish, up to a shutdown timeout, 5 seconds by default in Spring AMQP (setShutdownTimeout on the container; check your version). Messages still in the local prefetch buffer are not processed; they return to the queue when the channel closes, which with prefetch 1 is at most one per consumer.
For 2 second jobs the default timeout is enough; for longer jobs we raise it and make sure Kubernetes' terminationGracePeriodSeconds, 30 seconds by default, is longer still, so the pod is not killed in the middle of a job.
6 messages in order. Play them, or step through them one at a time.
6Follow-up questions
Does a prefetch of 1 hurt throughput?
Only when messages are very fast. Each consumer waits one network round trip between finishing a message and receiving the next, perhaps a millisecond. For a 2 second job that is a 0.05 percent overhead; for a 1 ms message it could halve throughput. So prefetch 1 is right for slow jobs and wrong for high-volume fast messages, which is why separating the two kinds of work into different queues helps.
What about consumers that never acknowledge, for example because they hang?
RabbitMQ has a consumer timeout for unacknowledged deliveries, 30 minutes by default in recent versions. If a delivery is not acknowledged in that time, the broker closes the channel and the messages are redelivered. Jobs that legitimately run longer need the timeout raised for their queue, and hanging consumers should be found through monitoring rather than relying on the timeout.
7Conclusion
In this answer, we saw that prefetch caps the unacknowledged messages per consumer, 250 by default in Spring AMQP, and that with slow jobs it reserves minutes of work on one pod where idle pods cannot reach it. A restart then releases hundreds of redelivered messages late. Lowering the prefetch, even to 1 for long jobs, keeps waiting work in the queue for any free consumer at a small cost for fast messages, idempotent consumers absorb the unavoidable redeliveries, and graceful shutdown with suitable timeouts lets in-flight jobs finish.
Open with
Prefetch, which is basic.qos, limits how many unacknowledged messages the broker sends to a consumer, and Spring AMQP defaults to 250. Those messages sit in the consumer's buffer and are reserved for it, so no other consumer can take them.
Then, if they want more
With fast messages that is good for throughput, but with slow jobs it is a problem: at 2 seconds per job, 250 prefetched messages are more than 8 minutes of work on one pod, while other pods can be idle because the queue looks empty to them.
When that pod restarts, its channel closes and all its unacknowledged messages are redelivered, which is why they show up late. So for slow jobs I would lower the prefetch, often to 1, and use container concurrency to control how many jobs a pod runs in parallel.
The round trip cost is negligible for long jobs; for fast messages I would keep a higher prefetch, ideally on a separate queue. Because anything unacknowledged is redelivered when the channel closes, including a job that finished its work but did not ack yet, consumers must be idempotent, for example by recording processed job ids in the same transaction.
Finally, I would make shutdown graceful: on SIGTERM the container cancels its consumers so nothing new arrives, finishes in-flight jobs within a shutdown timeout, and I make sure the Kubernetes grace period is longer than that.
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 RabbitMQ consumer with prefetch 250 processes slow jobs. When one pod is restarted...": https://scrivio-interview-library.netlify.app/spring-boot/rabbitmq-and-jms/450d9bb6be/