Quick revision
The short answer
The record on that partition cannot be deserialized, and because JsonDeserializer runs inside poll(), the failure happens before Spring's error handler ever sees a record, so the consumer fetches the same bad record forever and the partition stops moving. The fix is to wrap JsonDeserializer in ErrorHandlingDeserializer, which turns the failure into a record-level error that DefaultErrorHandler can send to a dead-letter topic before moving on. Then we find the producer that wrote the bad data and add alerts on DLT arrivals and per-partition lag.
What to remember
In this answer, we saw that a deserializer runs inside poll(), so a record that JsonDeserializer cannot read fails before Spring's error handler sees it, and the consumer fetches the same record again and again while lag grows on that one partition. Wrapping the deserializer in ErrorHandlingDeserializer turns the failure into a record-level DeserializationException, which DefaultErrorHandler sends straight to a DLT before committing past it. We then fix the producer with schemas or contracts, and alert on DLT arrivals and per-partition lag.
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.
The record on that partition cannot be deserialized, and because JsonDeserializer runs inside poll(), the failure happens before Spring's error handler ever sees a record, so the consumer fetches the same bad record forever and the partition stops moving. The fix is to wrap JsonDeserializer in ErrorHandlingDeserializer, which turns the failure into a record-level error that DefaultErrorHandler can send to a dead-letter topic before moving on. Then we find the producer that wrote the bad data and add alerts on DLT arrivals and per-partition lag.
Why this question is asked
A record that a consumer can never process is often called a poison pill. With the wrong configuration, one such record stops a whole partition until someone intervenes, and the service may look healthy everywhere else. Interviewers like this question because it tests whether we know where deserialization happens in a Kafka consumer and how Spring Kafka's error handling fits around it. In this answer, we will recap where deserializers run, see why the error repeats forever, apply the ErrorHandlingDeserializer fix, and then deal with the source of the bad data and the monitoring that would have caught it sooner.
1A Quick Recap of Where Deserialization Happens
Kafka stores keys and values as plain bytes. A deserializer is the class that turns those bytes back into objects on the consumer side. JsonDeserializer, from Spring Kafka, reads the bytes as JSON and maps them into a Java type with Jackson.
The important detail is where the deserializer runs. It is configured on the KafkaConsumer itself, and the consumer calls it inside poll(), while it is building the batch of records to return. Our @KafkaListener method and Spring's error handler come later, once poll() has returned. So if deserialization fails, the failure happens inside the Kafka client, before there is any record object for Spring to hand to an error handler.
- KafkaConsumer.poll()fetches bytes for each assigned partition
- Deserializerbytes -> objects;
JsonDeserializerfails here - Listener containerreceives the
ConsumerRecords - Error handler
DefaultErrorHandler, only sees records that exist - @KafkaListener methodour business code
5 layers, from the top down. Step through them one at a time.
A failure in the second layer never reaches the fourth.
2Why the Same Error Repeats Forever
Suppose partition 5 contains, at offset 8812, a record that a producer wrote as plain text, or as JSON with a field of the wrong type. The consumer fetches from offset 8812, the deserializer throws, and poll() fails with an exception such as RecordDeserializationException that names the partition and offset. The consumer's position does not move past the bad record, because the record was never returned.
The container logs the error and polls again. The consumer still stands at offset 8812, fetches the same bytes, and fails the same way. That loop is what we see in the logs: the same deserialization error, over and over. Other partitions are fine because they hold no bad record, so lag grows only on partition 5. Exactly how the container reacts to an exception thrown from poll() differs between Spring Kafka versions, so check your version, but with JsonDeserializer used directly there is no record to retry or recover.
3The Fix: ErrorHandlingDeserializer
ErrorHandlingDeserializer is a small wrapper from Spring Kafka. We configure it as the consumer's deserializer and tell it which real deserializer to delegate to. When the delegate succeeds, the wrapper returns its result unchanged. When the delegate throws, the wrapper catches the exception and returns null as the value, attaching the exception and the original bytes to the record in a header.
Now poll() succeeds and returns the bad record like any other. The listener container notices the deserialization header, does not call our method with a null, and instead passes the failure to the error handler as a DeserializationException. DefaultErrorHandler treats DeserializationException as not retryable, so it goes straight to the recoverer.
Notice that we wrap the key deserializer too, because a bad key is just as much a poison pill as a bad value. In Spring Kafka 4.0, the Jackson 3 based JsonDeserializer has a new name, JacksonJsonDeserializer, with the older class deprecated, so check your version for the class to delegate to.
4Sending the Bad Record to a Dead-Letter Topic
With the failure now visible as a record-level error, we configure DefaultErrorHandler with a DeadLetterPublishingRecoverer. It publishes the original bytes to orders.DLT with headers that hold the original partition and offset and the exception message. Then the handler commits offset 8813 and partition 5 starts moving again.
6 messages in order. Play them, or step through them one at a time.
Instead of looping, the bad record is parked once and the partition moves on.
Notice the template: the value never became an object, so the recoverer has the raw bytes, and the template that publishes them should use ByteArraySerializer. Spring Kafka also lets the recoverer pick a template per value type when one service needs both kinds.
5Finding the Source and Catching It Sooner
The DLT stops the bleeding, but somebody wrote bad data into a topic other teams depend on. The DLT headers and the record itself usually make it easy to identify the producer, for example by a source field, a client id in the broker logs, or a field format only one service uses. The real fix is a contract: a schema registry with compatibility checks, or at least JSON Schema validation in the producer and contract tests between producer and consumer in CI, so that incompatible data cannot be published in the first place.
Finally, this incident was noticed late because nothing alerted on it. Two signals would have caught it within minutes. The first is per-partition lag: a single partition whose lag keeps growing while the others stay flat is the classic signature of a stuck record. The second, once the DLT exists, is an alert on any arrival in the DLT, since every record there is either bad data or a bug.
6Follow-up questions
Can't DefaultErrorHandler handle this without ErrorHandlingDeserializer?
Not in the way we want. The error handler works on records the consumer has returned, and a record that fails to deserialize was never returned. Newer client and Spring Kafka versions report the failing partition and offset more clearly, and some versions can react to it, so check your version, but ErrorHandlingDeserializer is the documented way to turn it into a normal record-level failure.
Could we just skip the record by moving the offset manually?
Yes, as an emergency measure: stop the group, reset partition 5 to offset 8813 with kafka-consumer-groups.sh, and restart. But the record is then lost unless we copied it first, and the next bad record will stop the partition again. The wrapper fixes the class of problem rather than one instance.
Why wrap the key deserializer as well?
Because a key that cannot be deserialized causes exactly the same loop. Wrapping both costs nothing, and it means any malformed record, whatever part is broken, ends up in the DLT instead of stopping the partition.
7Conclusion
In this answer, we saw that a deserializer runs inside poll(), so a record that JsonDeserializer cannot read fails before Spring's error handler sees it, and the consumer fetches the same record again and again while lag grows on that one partition. Wrapping the deserializer in ErrorHandlingDeserializer turns the failure into a record-level DeserializationException, which DefaultErrorHandler sends straight to a DLT before committing past it. We then fix the producer with schemas or contracts, and alert on DLT arrivals and per-partition lag.
Open with
This is a poison pill. The key detail is that deserializers run inside KafkaConsumer.poll, before Spring has a record to give to the listener or the error handler.
Then, if they want more
So when JsonDeserializer fails on a record, poll throws, the consumer's position stays on that offset, and the next poll fetches the same bytes and fails again. That is why the same error repeats forever and only that one partition's lag grows. The fix is to configure ErrorHandlingDeserializer as the key and value deserializer and set JsonDeserializer as its delegate.
When the delegate fails, the wrapper returns null and puts the exception and the raw bytes in a header, so poll succeeds. The container then reports a DeserializationException to DefaultErrorHandler, which treats it as not retryable and calls the recoverer.
With a DeadLetterPublishingRecoverer using a byte-array template, the raw record goes to the DLT with its original partition, offset and exception, and the consumer commits past it. After that I would find the producer that wrote the incompatible data, using the DLT record and headers, and push for a schema registry or JSON Schema validation and contract tests.
And I would add alerts on any DLT arrival and on lag per partition, because a single partition with growing lag is exactly how this shows up.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Kafka 4.0
- 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 consumer is stuck: lag on one partition grows while others are fine, and the logs show...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/1786220906/