0
0
Kafkadevops~10 mins

Error handling in streams in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Error handling in streams
Start Stream Processing
Read Message from Topic
Process Message
Error Occurs?
NoSend Output to Next Topic
Yes
Handle Error
Log Error
Continue Processing Next Message
This flow shows how a stream reads messages, processes them, checks for errors, handles errors by logging and sending to a special topic, then continues.
Execution Sample
Kafka
try {
  process(record);
  forward(record);
} catch (Exception e) {
  log(e);
  sendToDeadLetter(record);
}
This code tries to process a record, forwards it if successful, or logs and sends it to a dead letter topic if an error occurs.
Process Table
StepActionMessage ContentError Occurred?Error HandlingOutput
1Read messagevalid-data-1NoNoneProcess and forward
2Process messagevalid-data-1NoNoneForwarded to next topic
3Read messagebad-data-2YesCatch exceptionSend to dead letter topic
4Log errorbad-data-2YesLogged error detailsNo forward
5Read messagevalid-data-3NoNoneProcess and forward
6Process messagevalid-data-3NoNoneForwarded to next topic
7End of stream---Stop processing
💡 Reached end of stream, no more messages to process
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
messagenullvalid-data-1valid-data-1bad-data-2bad-data-2valid-data-3valid-data-3null
errorOccurredfalsefalsefalsetruetruefalsefalsefalse
outputTopicemptyemptyvalid-data-1valid-data-1valid-data-1valid-data-1,valid-data-3valid-data-1,valid-data-3valid-data-1,valid-data-3
deadLetterTopicemptyemptyemptybad-data-2bad-data-2bad-data-2bad-data-2bad-data-2
Key Moments - 3 Insights
Why does the bad message not get forwarded to the output topic?
Because in the execution_table at step 3 and 4, the errorOccurred is true, triggering error handling that sends the message to the dead letter topic instead of forwarding.
What happens after an error is handled?
As shown in the concept_flow and execution_table, after logging and sending to dead letter, the stream continues processing the next message without stopping.
Is the stream stopped when an error occurs?
No, the stream continues as seen in the execution_table steps 5 and 6 where valid messages are processed after the error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'Error Occurred?'
ANo
BUnknown
CYes
DFalse
💡 Hint
Check the 'Error Occurred?' column in execution_table row 3
At which step does the message get sent to the dead letter topic?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Error Handling' and 'Output' columns in execution_table rows 3 and 4
If the error handling was removed, what would happen to bad-data-2?
AIt would cause the stream to stop
BIt would be logged but not forwarded
CIt would be forwarded to the next topic
DIt would be ignored silently
💡 Hint
Refer to the concept_flow and key_moments about error handling importance
Concept Snapshot
Error handling in streams:
- Try processing each message
- If error occurs, catch exception
- Log error and send message to dead letter topic
- Continue processing next messages
- Prevents stream from stopping on errors
Full Transcript
This visual trace shows how Kafka stream processing handles errors. The stream reads messages one by one. For each message, it tries to process it. If no error occurs, the message is forwarded to the next topic. If an error happens, the stream catches the exception, logs the error, and sends the problematic message to a dead letter topic. Then the stream continues processing the next messages without stopping. This approach keeps the stream running smoothly even when some messages cause errors.