Quick revision
The short answer
When the downstream payment API is down, the listener should stop asking it. We pause the listener container through KafkaListenerEndpointRegistry when a circuit breaker opens and resume it when the breaker closes; pausing keeps the consumer in its group because the container keeps polling. Around that we use DefaultErrorHandler with exponential back-off for transient failures and a dead-letter topic for permanent ones, keep each poll within max.poll.interval.ms, and catch up with enough concurrency, batch writes and a rate limit, all watched through lag and container metrics.
What to remember
In this answer, we saw how to turn a noisy failure storm into a quiet pause. A circuit breaker opening pauses the listener container, which keeps polling and stays in the group, and closing resumes it. DefaultErrorHandler with exponential back-off handles short failures, non-retryable exceptions go straight to the dead-letter topic, and we keep retries and batches within max.poll.interval.ms. For recovery we use concurrency up to the partition count, batch calls and a rate limit, and we watch lag, paused containers and breaker state so the pause never goes unnoticed.
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.
When the downstream payment API is down, the listener should stop asking it. We pause the listener container through KafkaListenerEndpointRegistry when a circuit breaker opens and resume it when the breaker closes; pausing keeps the consumer in its group because the container keeps polling. Around that we use DefaultErrorHandler with exponential back-off for transient failures and a dead-letter topic for permanent ones, keep each poll within max.poll.interval.ms, and catch up with enough concurrency, batch writes and a rate limit, all watched through lag and container metrics.
Why this question is asked
Downstream outages are a normal part of running consumers in production, and how a service behaves during the outage decides whether recovery takes minutes or hours. If every record fails, retries and fills the logs, we burn CPU, flood the dead-letter topic with records that were never really bad, and may even trigger rebalances. Interviewers ask this scenario because it tests whether we know the container features Spring Kafka gives us, not only how to write a listener method. In this answer, we will recap what the container does on a failure, then look at pausing with a circuit breaker, error handling with back-off and dead-lettering, the poll interval limit, the catch-up phase, and the monitoring that makes all of it visible.
1A Quick Recap: What Happens When a Listener Throws
A listener container runs a loop: poll a batch of records from the KafkaConsumer, call our method for each record, commit offsets. When our method throws, the container hands the failed record to its error handler. In current Spring Kafka this is DefaultErrorHandler. By default it retries the record about nine more times with no delay, then logs it and moves on, which is exactly the noisy behaviour described in the question: each of hundreds of records fails ten times in quick succession, and the logs fill with stack traces.
The real problem is that the failure has nothing to do with the records. The payment API is down for everyone, so retrying record by record is pointless. What we want is to stop consuming while the API is down, and to retry gently only for short blips.
2Pausing the Container When the Circuit Breaker Opens
A circuit breaker, for example from Resilience4j, watches calls to the payment API. When the failure rate passes a threshold, say 50 percent over the last 20 calls, it opens and rejects calls immediately for a wait time. We connect the breaker's state transitions to the listener container: when it opens we pause the container, and when it moves to half-open or closed we resume it.
8 messages in order. Play them, or step through them one at a time.
The consumer never leaves the group, so no rebalance happens during the outage.
Notice what pause() really does. The container pauses the consumer's partitions but keeps calling poll(), and those polls return no records. Because the consumer keeps polling, it stays a healthy member of the group, so no rebalance happens and no other instance takes over its partitions. The pause takes effect before the next poll, so records already fetched in the current batch are still processed first, which is one reason a small max.poll.records helps here.
With a 20 minute outage, the container now sits quietly for 20 minutes instead of failing tens of thousands of times. Records simply wait in Kafka, which is what Kafka is good at, and the lag grows.
3Back-Off and Dead-Lettering for Individual Failures
Pausing handles the long outage. For short failures, such as one timeout, we still want retries, but spaced out. We configure DefaultErrorHandler with an exponential back-off and a recoverer that publishes to a dead-letter topic once retries run out. We also classify exceptions: a validation error or a 400 Bad Request from the API will fail every time, so retrying it only wastes time.
Here the four retries take 15 seconds in total. Notice the danger with longer back-offs: the retries for one record happen inside the poll loop, and Kafka expects the consumer to call poll() again within max.poll.interval.ms, five minutes by default. If one record spends longer than that in retries, the broker decides the consumer is stuck, removes it from the group and starts a rebalance. So the total back-off for a record must stay well below the poll interval, or we move longer retries out of the loop with Spring Kafka's non-blocking retry topics.
4Keeping Each Poll Within max.poll.interval.ms
The same limit applies to normal processing. If max.poll.records is 500 and each payment call takes 400 ms when the API is slow, one batch takes 200 seconds, uncomfortably close to the five minute limit. When the API is recovering, calls are often slower than usual, so this is exactly when consumers start leaving the group, rebalancing and reprocessing records. We size max.poll.records so that records per poll times the worst realistic processing time stays well under the limit, for example 100 records at 400 ms is 40 seconds, and only raise max.poll.interval.ms when the work genuinely needs it.
5Catching Up After the API Recovers
After 20 minutes the backlog might be 240,000 records if we normally receive 200 per second. Two things decide how quickly we catch up. The first is parallelism: a consumer group can use at most one consumer per partition, so with 12 partitions we can raise the container concurrency, or the number of instances, up to 12 consumers in total; more would sit idle. The second is the cost per call: if the payment API accepts bulk requests, a batch listener that receives a List of records and sends them in groups of 50 cuts the number of HTTP calls by fifty.
But there is a catch: the API has just recovered, and a flood of catch-up traffic can knock it over again. So we put a rate limit, again from Resilience4j or similar, in front of the client, for example 500 calls per second shared across instances, and let the backlog drain at a speed the API can take. A slightly slower catch-up is much better than a second outage.
6Making Pauses and Lag Visible
A paused container is silent by design, so we must make sure people notice it. Spring Kafka records Micrometer timers for listener calls when observation or micrometer is enabled on the container, and the Kafka client exposes consumer metrics such as records-lag-max that the Micrometer binder publishes; check your version for the exact metric names. We alert on lag that keeps growing, on containers that have been paused longer than, say, 10 minutes, and on the circuit breaker state. Spring also publishes ConsumerPausedEvent and ConsumerResumedEvent, which are handy for logging one clear line instead of thousands of stack traces.
7Follow-up questions
What is the difference between pausing the container and stopping it?
Pausing keeps the consumer open and polling, so it keeps its group membership and its partitions; it simply receives no records until we resume, and resuming is instant. Stopping closes the consumer, so it leaves the group and a rebalance moves its partitions to other members, if there are any. With stop, a restart needs to rejoin the group and triggers another rebalance. For a temporary downstream outage, pause is the right tool, and stop is for shutting a listener down or replacing its configuration.
Why not just let DefaultErrorHandler retry forever?
Because infinite retries of one record block the partition, keep the thread busy, can exceed max.poll.interval.ms and still give no signal that the whole downstream is down. A circuit breaker treats the outage as one event and pauses everything at once, while a bounded back-off handles the occasional single failure.
8Conclusion
In this answer, we saw how to turn a noisy failure storm into a quiet pause. A circuit breaker opening pauses the listener container, which keeps polling and stays in the group, and closing resumes it. DefaultErrorHandler with exponential back-off handles short failures, non-retryable exceptions go straight to the dead-letter topic, and we keep retries and batches within max.poll.interval.ms. For recovery we use concurrency up to the partition count, batch calls and a rate limit, and we watch lag, paused containers and breaker state so the pause never goes unnoticed.
Open with
The key idea is that during an outage the records are fine and the dependency is broken, so I would stop consuming instead of failing every record. I put a circuit breaker around the payment client and listen to its state transitions: when it opens, I pause the listener container through KafkaListenerEndpointRegistry, and when it goes half-open or closed, I resume it.
Then, if they want more
Pause is different from stop: the container keeps polling, so the consumer keeps its group membership and there is no rebalance; the records just wait in Kafka.
For individual failures I configure DefaultErrorHandler with an exponential back-off, say one, two, four and eight seconds, and a DeadLetterPublishingRecoverer, and I mark exceptions like validation errors as not retryable so they go to the dead-letter topic immediately.
I keep the total retry time and the batch processing time under max.poll.interval.ms, by tuning max.poll.records, otherwise the consumer gets kicked out of the group just when the API is slow.
To catch up, I raise concurrency up to the partition count, use a batch listener if the API supports bulk calls, and put a rate limit in front of the API so the backlog does not cause a second outage. Finally I alert on consumer lag, paused containers and breaker state.
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
- Apache Kafka documentation, Spring for Apache Kafka reference
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 Spring Kafka listener writes to a downstream payment API. When that API is down for 20...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/8b04083375/