A bad record that blocks the topic
A hard Java puzzle in the messaging and kafka mistakes family. One line below contains a real bug. Try to spot it first, then read why it fails and how to fix it.
Highlighted: line 8. Play this one as a puzzle without the answer.
What is wrong
With default error handling, the container retries the failing record over and over (or, depending on version, skips it with only a log line). One malformed message blocks the partition or is lost without trace. There is no dead-letter topic to inspect it later.
The fix
How to avoid it next time
Plan for poison messages: limited retries, then a dead-letter topic with alerting.
Want the background? Read Retries and dead-letter topics in the Java concepts library.
About messaging and kafka mistakes
Message handling combines retries, offsets and transactions, and the order of those steps decides whether a failure loses data or processes it twice.
In code review: Trace what happens if the process dies after each line. Commit an offset only after the work is done, and make handlers idempotent.
More puzzles in this family
Frequently asked questions
What is the bug in "A bad record that blocks the topic"?
With default error handling, the container retries the failing record over and over (or, depending on version, skips it with only a log line). One malformed message blocks the partition or is lost without trace. There is no dead-letter topic to inspect it later. The faulty line is line 8: Order order = new com.fasterxml.jackson.databind.ObjectMapper().readValue(payload, Order.class); // throws on bad JSON
How do I fix it?
Replace it with: Configure a DefaultErrorHandler with a DeadLetterPublishingRecoverer and a bounded back-off, and treat deserialization errors as non-retryable.. Plan for poison messages: limited retries, then a dead-letter topic with alerting.
How do I catch messaging and kafka mistakes in code review?
Trace what happens if the process dies after each line. Commit an offset only after the work is done, and make handlers idempotent.
Is there a new puzzle every day?
Yes. The daily Spot the Bug puzzle picks one of these snippets for each UTC day, with the difficulty set by the weekday. Your streak is stored only in your browser.