Orders for one customer in random order
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.
Highlighted: line 6. Play this one as a puzzle without the answer.
What is wrong
A record without a key is spread across partitions. Kafka orders records only within a partition, so events for one customer can be consumed out of order by different consumers, for example a cancel before the create.
The fix
How to avoid it next time
Use the entity id as the record key whenever order per entity matters.
Want the background? Read Kafka producer 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 "Orders for one customer in random order"?
A record without a key is spread across partitions. Kafka orders records only within a partition, so events for one customer can be consumed out of order by different consumers, for example a cancel before the create. The faulty line is line 6: producer.send(new ProducerRecord<>("orders", payload));
How do I fix it?
Replace it with: producer.send(new ProducerRecord<>("orders", customerId, payload));. Use the entity id as the record key whenever order per entity matters.
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.