BackendBench
Coffee
Spot the Bug #121

Committing the work before doing it

A medium 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.

mediumMessaging and Kafka mistakesbuggy line: 8

Highlighted: line 8. Play this one as a puzzle without the answer.

What is wrong

The offsets are committed before the records are processed. If the process crashes midway, the committed offsets are already past the unprocessed records and they are never delivered again, so messages are lost. This is at-most-once delivery by accident.

The fix

for (ConsumerRecord<String, String> r : records) { process(r.value()); } consumer.commitSync();

How to avoid it next time

Commit after the work is done and make the work idempotent. Choose the delivery guarantee deliberately.

Want the background? Read Offsets and commits 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

๐Ÿž Play today's puzzleAll 132 bugsโ† #120#122 โ†’

Frequently asked questions

What is the bug in "Committing the work before doing it"?

The offsets are committed before the records are processed. If the process crashes midway, the committed offsets are already past the unprocessed records and they are never delivered again, so messages are lost. This is at-most-once delivery by accident. The faulty line is line 8: consumer.commitSync();

How do I fix it?

Replace it with: for (ConsumerRecord r : records) { process(r.value()); } consumer.commitSync();. Commit after the work is done and make the work idempotent. Choose the delivery guarantee deliberately.

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.

More tools you might need