SlideShare a Scribd company logo
1
Robust Kafka Streams
Streams Technical Deep Dive and Making your Kafka Streams
applications more resilient
2
Brief Introduction
• Worked at Confluent (Streams Team) 1.5 years
• Author Kafka Streams in Action
3
What We’ll Cover
• Concept of Topology and Sub-Topology
• Tasks, threads and state stores
• Getting Notifications
• Error Handling
4
Sample Streams Application
...
stream = streamBuilder.stream(Arrays.asList(“A”, “B”))
stream.groupByKey()
.count(Materialized.as(“count-store”))
.toStream()
.to(“output-topic”,
Produced.with(Serdes.String(), Serdes.Long()))
5
View the Topology
Topology topology = streamBuilder.build()
topology.describe().toString()
6
View the Topology
Topologies:
Sub-topology: 0
Source: KSTREAM-SOURCE-0000000000 (topics: [A, B])
--> KSTREAM-AGGREGATE-0000000001
Processor: KSTREAM-AGGREGATE-0000000001 (stores: [count-store])
--> KTABLE-TOSTREAM-0000000002
<-- KSTREAM-SOURCE-0000000000
Processor: KTABLE-TOSTREAM-0000000002 (stores: [])
--> KSTREAM-SINK-0000000003
<-- KSTREAM-AGGREGATE-0000000001
Sink: KSTREAM-SINK-0000000003 (topic: output-topic)
<-- KTABLE-TOSTREAM-0000000002
7
View Graphic Topology
https://github.com/zz85/kafka-streams-viz
8
View the Topology
stream = streamBuilder.stream(Arrays.asList(“A”, “B”));
stream.groupByKey()
.count(Materialized.as(“count-store”))
.toStream()
.to(“output-topic”,
Produced.with(Serdes.String(), Serdes.Long()));
9
View the Topology
stream = builder.stream(Arrays.asList(“A”, “B”));
stream.groupByKey()
.count(Materialized.as(“count-store”))
.toStream()
.to(“output-topic”,
Produced.with(Serdes.String(), Serdes.Long()));
10
View the Topology
stream = builder.stream(Arrays.asList(“A”, “B”));
stream.groupByKey()
.count(Materialized.as(“count-store”))
.toStream()
.to(“output-topic”,
Produced.with(Serdes.String(), Serdes.Long()));
11
View the Topology
stream = builder.stream(Arrays.asList(“A”, “B”));
stream.groupByKey()
.count(Materialized.as(“count-store”))
.toStream()
.to(“output-topic”,
Produced.with(Serdes.String(), Serdes.Long()));
12
Kafka Streams Tasks
13
Updated Sample Streams Application
stream = streamBuilder.stream(“A”);
secondStream = streamBuilder.stream("B");
stream.groupByKey()
.count(Materialized.as(“count-store”))
.toStream()
.to(“output-topic”,
Produced.with(Serdes.String(), Serdes.Long()));
secondStream.filter((k,v) -> k.equals("Foo"))
.mapValues(v -> v.substring(0,3))
.to("filtered-output-topic");
14
Updated streams Example Topology
15
Kafka Streams Task Assignments
First Number: Group ID
Second Number: Partition ID
16
Tasks and Threads
17
Tasks and Threads
18
Tasks and Threads
19
Tasks and Threads
20
Tasks and Threads
21
So what does this mean?
• N tasks max of N Threads,
• Threads > Tasks; Idle threads
• Account for #core(s)
22
Tasks and State Stores
• Stores are assigned Per task
Task Migration with State
24
Standby Tasks
25
Standby Tasks
26
Standby Task Deployment Considerations
27
Standby Task Pros & Cons
28
View Task Assignments
for (ThreadMetadata threadMetadata : kafkaStreams.localThreadsMetadata()) {
LOG.info("Thread {} active {}",
threadMetadata.threadName(),
threadMetadata.activeTasks());
LOG.info("Thread {} standby {}",
threadMetadata.threadName(),
threadMetadata.standbyTasks();
}
29
The General Problem Areas
• Deserialization
• Producing
• Processing
30
Error Handling - Deserializing
props.put(StreamsConfig.DEFAULT_DESERIALIZATION_EXCEPTION_HANDLER_CLASS_CONFIG,
LogAndFailExceptionHandler.class.getName());
props.put(StreamsConfig.DEFAULT_DESERIALIZATION_EXCEPTION_HANDLER_CLASS_CONFIG,
LogAndContinueExceptionHandler.class.getName());
31
Error Handling - Producing
props.put(StreamsConfig.DEFAULT_PRODUCTION_EXCEPTION_HANDLER_CLASS_CONFIG,
DefaultProductionExceptionHandler.class.getName());
32
Processing Exceptions DSL
ValueMapper<String, ValueContainer> errorProneMapper = v -> {
ValueContainer valueContainer = new ValueContainer();
try {
valueContainer.addValue(v.substring(0, 5));
} catch (Exception e) {
valueContainer.setException(e);
}
return valueContainer;
};
33
Processing Exceptions DSL
Predicate<String, ValueContainer> errorPredicate =
(k, v) -> v.exception() != null;
Predicate<String, ValueContainer> successPredicate =
(k,v) -> !errorPredicate.test(k,v);
KStream<String, ValueContainer>[] branched = sourceStream.mapValues(errorProneMapper).
branch(errorPredicate, successPredicate);
int errorIndex = 0;
int successIndex = 1;
branched[errorIndex].to(errorReportTopic);
branched[successIndex].mapValues(ValueContainer::formatValue).to(outputTopic);
34
Processing Exceptions DSL
Predicate<String, ValueContainer> errorPredicate = (k, v) -> v.exception() != null;
Predicate<String, ValueContainer> successPredicate = (k,v) ->!errorPredicate.test(k,v);
KStream<String, String>[] branched = sourceStream
.mapValues(errorProneMapper)
.branch(errorPredicate,
successPredicate);
int errorIndex = 0;
int successIndex = 1;
branched[errorIndex].to(errorReportTopic);
branched[successIndex].mapValues(v -> ValueContainer::formatValue).to(outputTopic);
35
Processing Exceptions DSL
Predicate<String, ValueContainer> errorPredicate = (k, v) -> v.exception() != null;
Predicate<String, ValueContainer> successPredicate = (k,v) ->!errorPredicate.test(k,v);
KStream<String, ValueContainer>[] branched = sourceStream.mapValues(errorProneMapper).
branch(errorPredicate,successPredicate);
int errorIndex = 0;
int successIndex = 1;
branched[errorIndex].to(errorReportTopic);
branched[successIndex].mapValues(ValueContainer::formatValue)
.to(outputTopic);
36
Processing Exceptions PAPI
class ErrorProneProcessor extends AbstractProcessor<String, String> {
@Override
public void process(String key, String value) {
ValueContainer valueContainer = new ValueContainer();
try {
valueContainer.addValue(value.substring(0, 5));
context().forward(key,
valueContainer,
To.child(successProcessor));
} catch (Exception e) {
context().forward(key,
e.getMessage(),
To.child(errorSink)
}
}
}
37
Processing Exceptions PAPI
Topology topology = new Topology();
topology.addSource(sourceTopicNode, sourceTopic);
topology.addProcessor(errorProneProcessor,
ErrorProneProcessor::new,
sourceTopicNode);
topology.addProcessor(successProcessor,
SuccessProcessor::new,
errorProneProcessor);
topology.addSink(errorSink,
errorReportTopic,
errorProneProcessor);
topology.addSink(outputTopicNode, outputTopic, upperCaseProcessor);
38
Uncaught Exceptions
kafkaStreams.setUncaughtExceptionHandler(
(thread, exception) -> {
LOG.info(”It ran on my box fine!");
});
39
StateRestoreListener
kafkaStreams.setGlobalStateRestoreListener(new StateRestoreListener() {
@Override
public void onRestoreStart(TopicPartition topicPartition, String storeName, long
startingOffset, long endingOffset) {
}
@Override
public void onBatchRestored(TopicPartition topicPartition, String storeName, long
batchEndOffset, long numRestored) {
}
@Override
public void onRestoreEnd(TopicPartition topicPartition, String storeName, long
totalRestored) {
}
});
40
Kafka Streams StateListener
kafkaStreams.setStateListener((newState, oldState) -> {
if( newState == State.RUNNING &&
oldState == State.REBALANCING ) {
// Do something here
} else if ( newState == State.REBALANCING &&
oldState == State.RUNNING ) {
// Do something else
}
});
41
Wrapping Up
• Sub-topologies, tasks and threads
• Tasks and state
• Failover
42
Wrapping Up
• General Error handing
• Per-Record Error handling
• Events
43
Thanks!
Kafka Streams in Action book signing @4:15 at the Confluent
booth.
Stay in Touch!
• https://slackpass.io/confluentcommunity
• https://www.confluent.io/blog/

More Related Content

PPTX
Kick your database_to_the_curb_reston_08_27_19
PDF
So You Want to Write a Connector?
PDF
Event sourcing - what could possibly go wrong ? Devoxx PL 2021
PDF
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
PDF
Apache kafka meet_up_zurich_at_swissre_from_zero_to_hero_with_kafka_connect_2...
PDF
Performance Analysis and Optimizations for Kafka Streams Applications
PDF
Kafka Streams: the easiest way to start with stream processing
PDF
What is the State of my Kafka Streams Application? Unleashing Metrics. | Neil...
Kick your database_to_the_curb_reston_08_27_19
So You Want to Write a Connector?
Event sourcing - what could possibly go wrong ? Devoxx PL 2021
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Apache kafka meet_up_zurich_at_swissre_from_zero_to_hero_with_kafka_connect_2...
Performance Analysis and Optimizations for Kafka Streams Applications
Kafka Streams: the easiest way to start with stream processing
What is the State of my Kafka Streams Application? Unleashing Metrics. | Neil...

What's hot (20)

PPTX
Kafka Summit NYC 2017 - Easy, Scalable, Fault-tolerant Stream Processing with...
PDF
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
PDF
Kafka Summit SF 2017 - Exactly-once Stream Processing with Kafka Streams
PDF
Apache Kafka, and the Rise of Stream Processing
PPTX
Exactly-once Stream Processing with Kafka Streams
PPTX
Stream Application Development with Apache Kafka
PDF
Stream Processing made simple with Kafka
PDF
Achieving a 50% Reduction in Cross-AZ Network Costs from Kafka (Uday Sagar Si...
PDF
Consistency and Completeness: Rethinking Distributed Stream Processing in Apa...
PDF
Introducing Exactly Once Semantics in Apache Kafka with Matthias J. Sax
PDF
Getting Started with Confluent Schema Registry
PDF
KSQL: Streaming SQL for Kafka
PDF
Kafka Summit NYC 2017 - The Best Thing Since Partitioned Bread
PDF
Building Scalable and Extendable Data Pipeline for Call of Duty Games: Lesson...
PDF
Webinar: Deep Dive on Apache Flink State - Seth Wiesman
PDF
ksqlDB: A Stream-Relational Database System
PDF
Real Time Streaming Data with Kafka and TensorFlow (Yong Tang, MobileIron) Ka...
PDF
London Apache Kafka Meetup (Jan 2017)
PDF
Apache Kafka: New Features That You Might Not Know About
PDF
Kafka Summit NYC 2017 - Singe Message Transforms are not the Transformations ...
Kafka Summit NYC 2017 - Easy, Scalable, Fault-tolerant Stream Processing with...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Kafka Summit SF 2017 - Exactly-once Stream Processing with Kafka Streams
Apache Kafka, and the Rise of Stream Processing
Exactly-once Stream Processing with Kafka Streams
Stream Application Development with Apache Kafka
Stream Processing made simple with Kafka
Achieving a 50% Reduction in Cross-AZ Network Costs from Kafka (Uday Sagar Si...
Consistency and Completeness: Rethinking Distributed Stream Processing in Apa...
Introducing Exactly Once Semantics in Apache Kafka with Matthias J. Sax
Getting Started with Confluent Schema Registry
KSQL: Streaming SQL for Kafka
Kafka Summit NYC 2017 - The Best Thing Since Partitioned Bread
Building Scalable and Extendable Data Pipeline for Call of Duty Games: Lesson...
Webinar: Deep Dive on Apache Flink State - Seth Wiesman
ksqlDB: A Stream-Relational Database System
Real Time Streaming Data with Kafka and TensorFlow (Yong Tang, MobileIron) Ka...
London Apache Kafka Meetup (Jan 2017)
Apache Kafka: New Features That You Might Not Know About
Kafka Summit NYC 2017 - Singe Message Transforms are not the Transformations ...
Ad

Similar to Robust Operations of Kafka Streams (20)

PDF
R and cpp
PDF
R and C++
PDF
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
PDF
Streams Don't Fail Me Now - Robustness Features in Kafka Streams
PPS
CS101- Introduction to Computing- Lecture 29
PDF
Real Time Big Data Management
POTX
Stream analysis with kafka native way and considerations about monitoring as ...
PDF
Wprowadzenie do technologi Big Data i Apache Hadoop
PDF
Scala @ TechMeetup Edinburgh
PDF
Exactly-once Data Processing with Kafka Streams - July 27, 2017
PDF
Introduction to Scalding and Monoids
PDF
Building an Interactive Query Service in Kafka Streams With Bill Bejeck | Cur...
PDF
Correctness and Performance of Apache Spark SQL
PDF
Correctness and Performance of Apache Spark SQL with Bogdan Ghit and Nicolas ...
PDF
Apache Spark for Library Developers with William Benton and Erik Erlandson
PDF
Structuring Spark: DataFrames, Datasets, and Streaming by Michael Armbrust
PDF
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
PDF
Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...
PDF
The Nuts and Bolts of Kafka Streams---An Architectural Deep Dive
PPTX
Cassandra Java APIs Old and New – A Comparison
R and cpp
R and C++
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Streams Don't Fail Me Now - Robustness Features in Kafka Streams
CS101- Introduction to Computing- Lecture 29
Real Time Big Data Management
Stream analysis with kafka native way and considerations about monitoring as ...
Wprowadzenie do technologi Big Data i Apache Hadoop
Scala @ TechMeetup Edinburgh
Exactly-once Data Processing with Kafka Streams - July 27, 2017
Introduction to Scalding and Monoids
Building an Interactive Query Service in Kafka Streams With Bill Bejeck | Cur...
Correctness and Performance of Apache Spark SQL
Correctness and Performance of Apache Spark SQL with Bogdan Ghit and Nicolas ...
Apache Spark for Library Developers with William Benton and Erik Erlandson
Structuring Spark: DataFrames, Datasets, and Streaming by Michael Armbrust
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...
The Nuts and Bolts of Kafka Streams---An Architectural Deep Dive
Cassandra Java APIs Old and New – A Comparison
Ad

More from confluent (20)

PDF
Stream Processing Handson Workshop - Flink SQL Hands-on Workshop (Korean)
PPTX
Webinar Think Right - Shift Left - 19-03-2025.pptx
PDF
Migration, backup and restore made easy using Kannika
PDF
Five Things You Need to Know About Data Streaming in 2025
PDF
Data in Motion Tour Seoul 2024 - Keynote
PDF
Data in Motion Tour Seoul 2024 - Roadmap Demo
PDF
From Stream to Screen: Real-Time Data Streaming to Web Frontends with Conflue...
PDF
Confluent per il settore FSI: Accelerare l'Innovazione con il Data Streaming...
PDF
Data in Motion Tour 2024 Riyadh, Saudi Arabia
PDF
Build a Real-Time Decision Support Application for Financial Market Traders w...
PDF
Strumenti e Strategie di Stream Governance con Confluent Platform
PDF
Compose Gen-AI Apps With Real-Time Data - In Minutes, Not Weeks
PDF
Building Real-Time Gen AI Applications with SingleStore and Confluent
PDF
Unlocking value with event-driven architecture by Confluent
PDF
Il Data Streaming per un’AI real-time di nuova generazione
PDF
Unleashing the Future: Building a Scalable and Up-to-Date GenAI Chatbot with ...
PDF
Break data silos with real-time connectivity using Confluent Cloud Connectors
PDF
Building API data products on top of your real-time data infrastructure
PDF
Speed Wins: From Kafka to APIs in Minutes
PDF
Evolving Data Governance for the Real-time Streaming and AI Era
Stream Processing Handson Workshop - Flink SQL Hands-on Workshop (Korean)
Webinar Think Right - Shift Left - 19-03-2025.pptx
Migration, backup and restore made easy using Kannika
Five Things You Need to Know About Data Streaming in 2025
Data in Motion Tour Seoul 2024 - Keynote
Data in Motion Tour Seoul 2024 - Roadmap Demo
From Stream to Screen: Real-Time Data Streaming to Web Frontends with Conflue...
Confluent per il settore FSI: Accelerare l'Innovazione con il Data Streaming...
Data in Motion Tour 2024 Riyadh, Saudi Arabia
Build a Real-Time Decision Support Application for Financial Market Traders w...
Strumenti e Strategie di Stream Governance con Confluent Platform
Compose Gen-AI Apps With Real-Time Data - In Minutes, Not Weeks
Building Real-Time Gen AI Applications with SingleStore and Confluent
Unlocking value with event-driven architecture by Confluent
Il Data Streaming per un’AI real-time di nuova generazione
Unleashing the Future: Building a Scalable and Up-to-Date GenAI Chatbot with ...
Break data silos with real-time connectivity using Confluent Cloud Connectors
Building API data products on top of your real-time data infrastructure
Speed Wins: From Kafka to APIs in Minutes
Evolving Data Governance for the Real-time Streaming and AI Era

Recently uploaded (20)

PPTX
Machine Learning_overview_presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Machine learning based COVID-19 study performance prediction
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Approach and Philosophy of On baking technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Machine Learning_overview_presentation.pptx
Electronic commerce courselecture one. Pdf
Spectroscopy.pptx food analysis technology
Building Integrated photovoltaic BIPV_UPV.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
MIND Revenue Release Quarter 2 2025 Press Release
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Machine learning based COVID-19 study performance prediction
Assigned Numbers - 2025 - Bluetooth® Document
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Approach and Philosophy of On baking technology
Dropbox Q2 2025 Financial Results & Investor Presentation
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx

Robust Operations of Kafka Streams

  • 1. 1 Robust Kafka Streams Streams Technical Deep Dive and Making your Kafka Streams applications more resilient
  • 2. 2 Brief Introduction • Worked at Confluent (Streams Team) 1.5 years • Author Kafka Streams in Action
  • 3. 3 What We’ll Cover • Concept of Topology and Sub-Topology • Tasks, threads and state stores • Getting Notifications • Error Handling
  • 4. 4 Sample Streams Application ... stream = streamBuilder.stream(Arrays.asList(“A”, “B”)) stream.groupByKey() .count(Materialized.as(“count-store”)) .toStream() .to(“output-topic”, Produced.with(Serdes.String(), Serdes.Long()))
  • 5. 5 View the Topology Topology topology = streamBuilder.build() topology.describe().toString()
  • 6. 6 View the Topology Topologies: Sub-topology: 0 Source: KSTREAM-SOURCE-0000000000 (topics: [A, B]) --> KSTREAM-AGGREGATE-0000000001 Processor: KSTREAM-AGGREGATE-0000000001 (stores: [count-store]) --> KTABLE-TOSTREAM-0000000002 <-- KSTREAM-SOURCE-0000000000 Processor: KTABLE-TOSTREAM-0000000002 (stores: []) --> KSTREAM-SINK-0000000003 <-- KSTREAM-AGGREGATE-0000000001 Sink: KSTREAM-SINK-0000000003 (topic: output-topic) <-- KTABLE-TOSTREAM-0000000002
  • 8. 8 View the Topology stream = streamBuilder.stream(Arrays.asList(“A”, “B”)); stream.groupByKey() .count(Materialized.as(“count-store”)) .toStream() .to(“output-topic”, Produced.with(Serdes.String(), Serdes.Long()));
  • 9. 9 View the Topology stream = builder.stream(Arrays.asList(“A”, “B”)); stream.groupByKey() .count(Materialized.as(“count-store”)) .toStream() .to(“output-topic”, Produced.with(Serdes.String(), Serdes.Long()));
  • 10. 10 View the Topology stream = builder.stream(Arrays.asList(“A”, “B”)); stream.groupByKey() .count(Materialized.as(“count-store”)) .toStream() .to(“output-topic”, Produced.with(Serdes.String(), Serdes.Long()));
  • 11. 11 View the Topology stream = builder.stream(Arrays.asList(“A”, “B”)); stream.groupByKey() .count(Materialized.as(“count-store”)) .toStream() .to(“output-topic”, Produced.with(Serdes.String(), Serdes.Long()));
  • 13. 13 Updated Sample Streams Application stream = streamBuilder.stream(“A”); secondStream = streamBuilder.stream("B"); stream.groupByKey() .count(Materialized.as(“count-store”)) .toStream() .to(“output-topic”, Produced.with(Serdes.String(), Serdes.Long())); secondStream.filter((k,v) -> k.equals("Foo")) .mapValues(v -> v.substring(0,3)) .to("filtered-output-topic");
  • 15. 15 Kafka Streams Task Assignments First Number: Group ID Second Number: Partition ID
  • 21. 21 So what does this mean? • N tasks max of N Threads, • Threads > Tasks; Idle threads • Account for #core(s)
  • 22. 22 Tasks and State Stores • Stores are assigned Per task
  • 26. 26 Standby Task Deployment Considerations
  • 28. 28 View Task Assignments for (ThreadMetadata threadMetadata : kafkaStreams.localThreadsMetadata()) { LOG.info("Thread {} active {}", threadMetadata.threadName(), threadMetadata.activeTasks()); LOG.info("Thread {} standby {}", threadMetadata.threadName(), threadMetadata.standbyTasks(); }
  • 29. 29 The General Problem Areas • Deserialization • Producing • Processing
  • 30. 30 Error Handling - Deserializing props.put(StreamsConfig.DEFAULT_DESERIALIZATION_EXCEPTION_HANDLER_CLASS_CONFIG, LogAndFailExceptionHandler.class.getName()); props.put(StreamsConfig.DEFAULT_DESERIALIZATION_EXCEPTION_HANDLER_CLASS_CONFIG, LogAndContinueExceptionHandler.class.getName());
  • 31. 31 Error Handling - Producing props.put(StreamsConfig.DEFAULT_PRODUCTION_EXCEPTION_HANDLER_CLASS_CONFIG, DefaultProductionExceptionHandler.class.getName());
  • 32. 32 Processing Exceptions DSL ValueMapper<String, ValueContainer> errorProneMapper = v -> { ValueContainer valueContainer = new ValueContainer(); try { valueContainer.addValue(v.substring(0, 5)); } catch (Exception e) { valueContainer.setException(e); } return valueContainer; };
  • 33. 33 Processing Exceptions DSL Predicate<String, ValueContainer> errorPredicate = (k, v) -> v.exception() != null; Predicate<String, ValueContainer> successPredicate = (k,v) -> !errorPredicate.test(k,v); KStream<String, ValueContainer>[] branched = sourceStream.mapValues(errorProneMapper). branch(errorPredicate, successPredicate); int errorIndex = 0; int successIndex = 1; branched[errorIndex].to(errorReportTopic); branched[successIndex].mapValues(ValueContainer::formatValue).to(outputTopic);
  • 34. 34 Processing Exceptions DSL Predicate<String, ValueContainer> errorPredicate = (k, v) -> v.exception() != null; Predicate<String, ValueContainer> successPredicate = (k,v) ->!errorPredicate.test(k,v); KStream<String, String>[] branched = sourceStream .mapValues(errorProneMapper) .branch(errorPredicate, successPredicate); int errorIndex = 0; int successIndex = 1; branched[errorIndex].to(errorReportTopic); branched[successIndex].mapValues(v -> ValueContainer::formatValue).to(outputTopic);
  • 35. 35 Processing Exceptions DSL Predicate<String, ValueContainer> errorPredicate = (k, v) -> v.exception() != null; Predicate<String, ValueContainer> successPredicate = (k,v) ->!errorPredicate.test(k,v); KStream<String, ValueContainer>[] branched = sourceStream.mapValues(errorProneMapper). branch(errorPredicate,successPredicate); int errorIndex = 0; int successIndex = 1; branched[errorIndex].to(errorReportTopic); branched[successIndex].mapValues(ValueContainer::formatValue) .to(outputTopic);
  • 36. 36 Processing Exceptions PAPI class ErrorProneProcessor extends AbstractProcessor<String, String> { @Override public void process(String key, String value) { ValueContainer valueContainer = new ValueContainer(); try { valueContainer.addValue(value.substring(0, 5)); context().forward(key, valueContainer, To.child(successProcessor)); } catch (Exception e) { context().forward(key, e.getMessage(), To.child(errorSink) } } }
  • 37. 37 Processing Exceptions PAPI Topology topology = new Topology(); topology.addSource(sourceTopicNode, sourceTopic); topology.addProcessor(errorProneProcessor, ErrorProneProcessor::new, sourceTopicNode); topology.addProcessor(successProcessor, SuccessProcessor::new, errorProneProcessor); topology.addSink(errorSink, errorReportTopic, errorProneProcessor); topology.addSink(outputTopicNode, outputTopic, upperCaseProcessor);
  • 39. 39 StateRestoreListener kafkaStreams.setGlobalStateRestoreListener(new StateRestoreListener() { @Override public void onRestoreStart(TopicPartition topicPartition, String storeName, long startingOffset, long endingOffset) { } @Override public void onBatchRestored(TopicPartition topicPartition, String storeName, long batchEndOffset, long numRestored) { } @Override public void onRestoreEnd(TopicPartition topicPartition, String storeName, long totalRestored) { } });
  • 40. 40 Kafka Streams StateListener kafkaStreams.setStateListener((newState, oldState) -> { if( newState == State.RUNNING && oldState == State.REBALANCING ) { // Do something here } else if ( newState == State.REBALANCING && oldState == State.RUNNING ) { // Do something else } });
  • 41. 41 Wrapping Up • Sub-topologies, tasks and threads • Tasks and state • Failover
  • 42. 42 Wrapping Up • General Error handing • Per-Record Error handling • Events
  • 43. 43 Thanks! Kafka Streams in Action book signing @4:15 at the Confluent booth. Stay in Touch! • https://slackpass.io/confluentcommunity • https://www.confluent.io/blog/