SlideShare a Scribd company logo
1 of 44
Download to read offline
The Nuts and Bolts of Kafka Streams:
An Architectural Deep Dive
Matthias J. Sax
Software Engineer | Apache Kafka Committer and PMC member
Twitter/𝕏 @MatthiasJSax
2
10,000ft View
The DSL Program
(let’s go down the rabbit hole)
Program
3
StreamsBuilder builder = …
KStream words = builder.stream(…);
KTable result = words.groupBy(…)
.count();
result.toStream().to(…);
Topology t = builder.build();
Program
4
StreamsBuilder builder = …
KStream words = builder.stream(…);
KTable result = words.groupBy(…)
.count();
result.toStream().to(…);
Topology t = builder.build();
(Sub-)Topology
src
grpBy
snk
src
cnt
snk
shuffle
input
result
log
1
2
3
4
1
2 3
4
Program
5
StreamsBuilder builder = …
KStream words = builder.stream(…);
KTable result = words.groupBy(…)
.count();
result.toStream().to(…);
Topology t = builder.build();
1
2
3
4
1
2 3
4
(Sub-)Topology
src
grpBy
snk
src
cnt
snk
shuffle
input
result
log
(Sub-)Topology
6
src
grpBy
snk
src
cnt
snk
shuffle
input
result
log
(Sub-)Topology Tasks
src
grpBy
snk
src
cnt
snk
shuffle
input
result
log
7
shuffle-0
input-0
result-1
shuffle-1
0_0 0_1
1_0 1_1
input-1
result-0
log-1
log-0
(Sub-)Topology Tasks
src
grpBy
snk
src
cnt
snk
shuffle
input
result
log
8
shuffle-0
input-0
result-1
shuffle-1
0_0 0_1
1_0 1_1
input-1
result-0
log-1
log-0
Instance B
Instance A
9
Kafka Streams Instances and StreamThreads
KafkaStreams streamsClient = new KafkaStreams(…); // num.stream.threads
streamsClient.start();
streamsClient.addStreamThread();
streamsClient.removeStreamThread();
// error handling: uncaught exception handler
10
StreamThreads and Tasks
KafkaConsumer KafkaProducer
Kafka Cluster
1_0 1_1
0_0
0_1
11
Main Processing Loop
while (running)
records = mainConsumer.poll() // config: poll.ms
// max.poll.records
// fetch configs
addRecordsToTask(records); // no deserialization
// config: buffered.records.per.partition
while (keepProcessing)
for each task:
if (task.isReady) // data available for all inputs?
task.process(nRecords) // -> ts-extraction
// -> (de)serialization + exception-handling
maybePunctuate()
maybeCommit() // config: commit.interval.ms [ctx.commit()]
adjust nRecords
12
Main Processing Loop
while (running)
records = mainConsumer.poll() // config: poll.ms
// max.poll.records
// fetch configs
addRecordsToTask(records); // no deserialization
// config: buffered.records.per.partition
while (keepProcessing)
for each task:
if (task.isReady) // data available for all inputs?
task.process(nRecords) // -> ts-extraction
// -> (de)serialization + exception-handling
maybePunctuate()
maybeCommit() // config: commit.interval.ms [ctx.commit()]
adjust nRecords max.poll.interval.ms
13
Main Processing Loop
while (running)
records = mainConsumer.poll() // config: poll.ms
// max.poll.records
// fetch configs
addRecordsToTask(records); // no deserialization
// config: buffered.records.per.partition
while (keepProcessing)
for each task:
if (task.isReady) // data available for all inputs?
task.process(nRecords) // -> ts-extraction
// -> (de)serialization + exception-handling
maybePunctuate()
maybeCommit() // config: commit.interval.ms [ctx.commit()]
adjust nRecords max.poll.interval.ms
14
Task Execution
1_1
src
cnt
snk
Record
Buffer
Record
Buffer
Record
Buffer
“head” records
(deserialized)
- exception handling
- ts-extraction
max.task.idle.ms
src.process(record)
ctx.forward(r) {
cntProcessor.process(r)
}
store.put(…) {
changelogger.put(…)
}
sink.process(record) {
recordCollector.send(…)
}
15
Task Execution
1_1
src
cnt
snk
Record
Buffer
Record
Buffer
Record
Buffer
“head” records
(deserialized)
- exception handling
- ts-extraction
max.task.idle.ms
src.process(record)
ctx.forward(r) {
cntProcessor.process(r)
}
store.put(…) {
changelogger.put(…)
}
sink.process(record) {
recordCollector.send(…)
}
depth-first
execution
16
Task Execution
1_1
src
cnt
snk
Record
Buffer
Record
Buffer
Record
Buffer
“head” records
(deserialized)
- exception handling
- ts-extraction
max.task.idle.ms
src.process(record)
ctx.forward(r) {
cntProcessor.process(r)
}
store.put(…) {
changelogger.put(…)
}
sink.process(record) {
recordCollector.send(…)
}
depth-first
execution
RecordCollector
• Serialization
• Error handling
• Partitioning
KafkaProducer
17
Main Processing Loop
while (running)
records = mainConsumer.poll() // config: poll.ms
// max.poll.records
// fetch configs
addRecordsToTask(records); // no deserialization
// config: buffered.records.per.partition
while (keepProcessing)
for each task:
if (task.isReady) // data available for all inputs?
task.process(nRecords) // -> ts-extraction
// -> (de)serialization + exception-handling
maybePunctuate()
maybeCommit() // config: commit.interval.ms [ctx.commit()]
adjust nRecords max.poll.interval.ms
18
Main Processing Loop
while (running)
records = mainConsumer.poll() // config: poll.ms
// max.poll.records
// fetch configs
addRecordsToTask(records); // no deserialization
// config: buffered.records.per.partition
while (keepProcessing)
for each task:
if (task.isReady) // data available for all inputs?
task.process(nRecords) // -> ts-extraction
// -> (de)serialization + exception-handling
maybePunctuate()
maybeCommit() // config: commit.interval.ms [ctx.commit()]
adjust nRecords max.poll.interval.ms
Committing
19
At-least-once:
• flush state stores
• flush producer
• avoid data
• get offset / consumer position
• record buffers
• commitSync()
Exactly-once:
Committing
20
At-least-once:
• flush state stores
• flush producer
• avoid data
• get offset / consumer position
• record buffers
• commitSync()
Exactly-once:
• flush state stores
• flush producer
• avoid data
• get offsets / consumer position
• record buffers
• addOffsetsToTransaction()
• commitTransaction()
• configs:
• transaction.timeout.ms
21
Main Processing Loop
while (running)
records = mainConsumer.poll() // config: poll.ms
// max.poll.records
// fetch configs
addRecordsToTask(records); // no deserialization
// config: buffered.records.per.partition
while (keepProcessing)
for each task:
if (task.isReady) // data available for all inputs?
task.process(nRecords) // -> ts-extraction
// -> (de)serialization + exception-handling
maybePunctuate()
maybeCommit() // config: commit.interval.ms [ctx.commit()]
adjust nRecords max.poll.interval.ms
22
Main Processing Loop
while (running)
records = mainConsumer.poll() // config: poll.ms
// max.poll.records
// fetch configs
addRecordsToTask(records); // no deserialization
// config: buffered.records.per.partition
while (keepProcessing)
for each task:
if (task.isReady) // data available for all inputs?
task.process(nRecords) // -> ts-extraction
// -> (de)serialization + exception-handling
maybePunctuate()
maybeCommit() // config: commit.interval.ms [ctx.commit()]
adjust nRecords max.poll.interval.ms
transaction.timeout.ms
Fault-Tolerance, High-Availability, and Scaling
23
Instance A
24
State Stores and Changelog Topics
Task 1_1
cnt
log-1
Instance A
25
State Stores and Changelog Topics
Task 1_1
cnt
log-1
26
State Stores and Changelog Topics
log-1
Instance B
Task 1_1
restore
27
State Stores and Changelog Topics
log-1
Instance B
Task 1_1
cnt
Restore Consumer
28
KafkaConsumer (restore)
no consumer group
read changelog topics
used to restore state
no offset commits
while (running) {
mainConsumer.poll()
if (restoring)
restoreConsumer.poll()
restoreState()
continue;
processActiveTasks()
}
Instance A
29
Standby Tasks and High-Availability
Task 1_1
cnt
log-1
Instance A
30
Standby Tasks and High-Availability
Task 1_1
cnt
log-1
Instance B
Standby 1_1
restore
Instance A
31
Standby Tasks and High-Availability
Task 1_1
cnt
log-1
Instance B
Standby 1_1
restore
config: num.standby.tasks
Restore Consumer
32
KafkaConsumer (restore)
no consumer group
read changelog topics
used to restore state
no offset commits
while (running) {
mainConsumer.poll()
if (restoring)
restoreConsumer.poll()
restoreState()
continue;
else
restoreConsumer.poll()
updateStandbys()
processActiveTasks()
}
33
Smooth Scaling with ”Warmup” Tasks
Instance A
Task 1_0
cnt
log-0
Task 1_1
cnt
log-1
34
Smooth Scaling with ”Warmup” Tasks
Instance A
Task 1_0
cnt
log-0
Instance B
Standby 1_1
(warmup)
restore
Task 1_1
cnt
log-1
35
Smooth Scaling with ”Warmup” Tasks
Instance A
Task 1_0
cnt
log-0
Instance B
Task 1_1
cnt
log-1
36
Smooth Scaling with ”Warmup” Tasks
Instance A
Task 1_0
cnt
log-0
Instance B
Task 1_1
cnt
log-1
config: max.warmup.replicas
GlobalKTables/Stores and Threading
37
Instance B
Instance A
38
Global State Stores and GlobalStreamThread
builder.globalTable(…); // or .addGlobalStore(…)
KafkaStreams streamsClient = new KafkaStreams(…);
streamsClient.start()
GlobalStreamThread
GlobalStreamThread
39
KafkaConsumer (global)
no consumer group
reads all global topic partitions
no offset commits
Global StateStore updating
GlobalStreamThread
40
KafkaConsumer (global)
no consumer group
reads all global topic partitions
no offset commits
Global StateStore updating
// bootstrap global state
start() {
while(!endOffsetsReached)
globalConsumer.poll()
updateGlobalState()
}
// start StreamThreads
// regular processing
while(running) {
globalConsumer.poll()
updateGlobalState()
}
Thread Refactoring Project
(https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=263429334)
41
Instance B
Instance A
42
Thread Refactoring – StateUpdaterThread (AK 3.7 ?)
GlobalStreamThread
StateUpdaterThread
Instance B
Instance A
43
Thread Refactoring – Phase 2
Decouple consumer and processing threads (WIP)
GlobalStreamThread ProcessingThread
StateUpdaterThread
The Nuts and Bolts of Kafka Streams:
An Architectural Deep Dive
Matthias J. Sax
Software Engineer | Apache Kafka Committer and PMC member
Twitter/𝕏 @MatthiasJSax

More Related Content

Similar to The Nuts and Bolts of Kafka Streams---An Architectural Deep Dive

Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...
Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...
Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...Flink Forward
 
Big Data Analytics with Scala at SCALA.IO 2013
Big Data Analytics with Scala at SCALA.IO 2013Big Data Analytics with Scala at SCALA.IO 2013
Big Data Analytics with Scala at SCALA.IO 2013Samir Bessalah
 
Exactly once with spark streaming
Exactly once with spark streamingExactly once with spark streaming
Exactly once with spark streamingQuentin Ambard
 
Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...
Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...
Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...confluent
 
Performance Analysis and Optimizations for Kafka Streams Applications
Performance Analysis and Optimizations for Kafka Streams ApplicationsPerformance Analysis and Optimizations for Kafka Streams Applications
Performance Analysis and Optimizations for Kafka Streams ApplicationsGuozhang Wang
 
Distributed Real-Time Stream Processing: Why and How: Spark Summit East talk ...
Distributed Real-Time Stream Processing: Why and How: Spark Summit East talk ...Distributed Real-Time Stream Processing: Why and How: Spark Summit East talk ...
Distributed Real-Time Stream Processing: Why and How: Spark Summit East talk ...Spark Summit
 
Distributed Stream Processing - Spark Summit East 2017
Distributed Stream Processing - Spark Summit East 2017Distributed Stream Processing - Spark Summit East 2017
Distributed Stream Processing - Spark Summit East 2017Petr Zapletal
 
design-compiler.pdf
design-compiler.pdfdesign-compiler.pdf
design-compiler.pdfFrangoCamila
 
Apache Flink internals
Apache Flink internalsApache Flink internals
Apache Flink internalsKostas Tzoumas
 
Writing a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdfWriting a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdfRomanKhavronenko
 
Robust Operations of Kafka Streams
Robust Operations of Kafka StreamsRobust Operations of Kafka Streams
Robust Operations of Kafka Streamsconfluent
 
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...Data Con LA
 
Spark streaming with kafka
Spark streaming with kafkaSpark streaming with kafka
Spark streaming with kafkaDori Waldman
 
Spark stream - Kafka
Spark stream - Kafka Spark stream - Kafka
Spark stream - Kafka Dori Waldman
 

Similar to The Nuts and Bolts of Kafka Streams---An Architectural Deep Dive (20)

Spark streaming: Best Practices
Spark streaming: Best PracticesSpark streaming: Best Practices
Spark streaming: Best Practices
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...
Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...
Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...
 
Big Data Analytics with Scala at SCALA.IO 2013
Big Data Analytics with Scala at SCALA.IO 2013Big Data Analytics with Scala at SCALA.IO 2013
Big Data Analytics with Scala at SCALA.IO 2013
 
Exactly once with spark streaming
Exactly once with spark streamingExactly once with spark streaming
Exactly once with spark streaming
 
Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...
Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...
Performance Analysis and Optimizations for Kafka Streams Applications (Guozha...
 
Performance Analysis and Optimizations for Kafka Streams Applications
Performance Analysis and Optimizations for Kafka Streams ApplicationsPerformance Analysis and Optimizations for Kafka Streams Applications
Performance Analysis and Optimizations for Kafka Streams Applications
 
Deathstar
DeathstarDeathstar
Deathstar
 
Distributed Real-Time Stream Processing: Why and How: Spark Summit East talk ...
Distributed Real-Time Stream Processing: Why and How: Spark Summit East talk ...Distributed Real-Time Stream Processing: Why and How: Spark Summit East talk ...
Distributed Real-Time Stream Processing: Why and How: Spark Summit East talk ...
 
Distributed Stream Processing - Spark Summit East 2017
Distributed Stream Processing - Spark Summit East 2017Distributed Stream Processing - Spark Summit East 2017
Distributed Stream Processing - Spark Summit East 2017
 
design-compiler.pdf
design-compiler.pdfdesign-compiler.pdf
design-compiler.pdf
 
GCC
GCCGCC
GCC
 
Apache Flink internals
Apache Flink internalsApache Flink internals
Apache Flink internals
 
Stream Processing made simple with Kafka
Stream Processing made simple with KafkaStream Processing made simple with Kafka
Stream Processing made simple with Kafka
 
Scala to assembly
Scala to assemblyScala to assembly
Scala to assembly
 
Writing a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdfWriting a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdf
 
Robust Operations of Kafka Streams
Robust Operations of Kafka StreamsRobust Operations of Kafka Streams
Robust Operations of Kafka Streams
 
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
 
Spark streaming with kafka
Spark streaming with kafkaSpark streaming with kafka
Spark streaming with kafka
 
Spark stream - Kafka
Spark stream - Kafka Spark stream - Kafka
Spark stream - Kafka
 

More from HostedbyConfluent

Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Renaming a Kafka Topic | Kafka Summit London
Renaming a Kafka Topic | Kafka Summit LondonRenaming a Kafka Topic | Kafka Summit London
Renaming a Kafka Topic | Kafka Summit LondonHostedbyConfluent
 
Evolution of NRT Data Ingestion Pipeline at Trendyol
Evolution of NRT Data Ingestion Pipeline at TrendyolEvolution of NRT Data Ingestion Pipeline at Trendyol
Evolution of NRT Data Ingestion Pipeline at TrendyolHostedbyConfluent
 
Ensuring Kafka Service Resilience: A Dive into Health-Checking Techniques
Ensuring Kafka Service Resilience: A Dive into Health-Checking TechniquesEnsuring Kafka Service Resilience: A Dive into Health-Checking Techniques
Ensuring Kafka Service Resilience: A Dive into Health-Checking TechniquesHostedbyConfluent
 
Exactly-once Stream Processing with Arroyo and Kafka
Exactly-once Stream Processing with Arroyo and KafkaExactly-once Stream Processing with Arroyo and Kafka
Exactly-once Stream Processing with Arroyo and KafkaHostedbyConfluent
 
Fish Plays Pokemon | Kafka Summit London
Fish Plays Pokemon | Kafka Summit LondonFish Plays Pokemon | Kafka Summit London
Fish Plays Pokemon | Kafka Summit LondonHostedbyConfluent
 
Tiered Storage 101 | Kafla Summit London
Tiered Storage 101 | Kafla Summit LondonTiered Storage 101 | Kafla Summit London
Tiered Storage 101 | Kafla Summit LondonHostedbyConfluent
 
Building a Self-Service Stream Processing Portal: How And Why
Building a Self-Service Stream Processing Portal: How And WhyBuilding a Self-Service Stream Processing Portal: How And Why
Building a Self-Service Stream Processing Portal: How And WhyHostedbyConfluent
 
From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...
From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...
From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...HostedbyConfluent
 
Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...
Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...
Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...HostedbyConfluent
 
Navigating Private Network Connectivity Options for Kafka Clusters
Navigating Private Network Connectivity Options for Kafka ClustersNavigating Private Network Connectivity Options for Kafka Clusters
Navigating Private Network Connectivity Options for Kafka ClustersHostedbyConfluent
 
Apache Flink: Building a Company-wide Self-service Streaming Data Platform
Apache Flink: Building a Company-wide Self-service Streaming Data PlatformApache Flink: Building a Company-wide Self-service Streaming Data Platform
Apache Flink: Building a Company-wide Self-service Streaming Data PlatformHostedbyConfluent
 
Explaining How Real-Time GenAI Works in a Noisy Pub
Explaining How Real-Time GenAI Works in a Noisy PubExplaining How Real-Time GenAI Works in a Noisy Pub
Explaining How Real-Time GenAI Works in a Noisy PubHostedbyConfluent
 
TL;DR Kafka Metrics | Kafka Summit London
TL;DR Kafka Metrics | Kafka Summit LondonTL;DR Kafka Metrics | Kafka Summit London
TL;DR Kafka Metrics | Kafka Summit LondonHostedbyConfluent
 
A Window Into Your Kafka Streams Tasks | KSL
A Window Into Your Kafka Streams Tasks | KSLA Window Into Your Kafka Streams Tasks | KSL
A Window Into Your Kafka Streams Tasks | KSLHostedbyConfluent
 
Mastering Kafka Producer Configs: A Guide to Optimizing Performance
Mastering Kafka Producer Configs: A Guide to Optimizing PerformanceMastering Kafka Producer Configs: A Guide to Optimizing Performance
Mastering Kafka Producer Configs: A Guide to Optimizing PerformanceHostedbyConfluent
 
Data Contracts Management: Schema Registry and Beyond
Data Contracts Management: Schema Registry and BeyondData Contracts Management: Schema Registry and Beyond
Data Contracts Management: Schema Registry and BeyondHostedbyConfluent
 
Code-First Approach: Crafting Efficient Flink Apps
Code-First Approach: Crafting Efficient Flink AppsCode-First Approach: Crafting Efficient Flink Apps
Code-First Approach: Crafting Efficient Flink AppsHostedbyConfluent
 
Debezium vs. the World: An Overview of the CDC Ecosystem
Debezium vs. the World: An Overview of the CDC EcosystemDebezium vs. the World: An Overview of the CDC Ecosystem
Debezium vs. the World: An Overview of the CDC EcosystemHostedbyConfluent
 
Beyond Tiered Storage: Serverless Kafka with No Local Disks
Beyond Tiered Storage: Serverless Kafka with No Local DisksBeyond Tiered Storage: Serverless Kafka with No Local Disks
Beyond Tiered Storage: Serverless Kafka with No Local DisksHostedbyConfluent
 

More from HostedbyConfluent (20)

Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Renaming a Kafka Topic | Kafka Summit London
Renaming a Kafka Topic | Kafka Summit LondonRenaming a Kafka Topic | Kafka Summit London
Renaming a Kafka Topic | Kafka Summit London
 
Evolution of NRT Data Ingestion Pipeline at Trendyol
Evolution of NRT Data Ingestion Pipeline at TrendyolEvolution of NRT Data Ingestion Pipeline at Trendyol
Evolution of NRT Data Ingestion Pipeline at Trendyol
 
Ensuring Kafka Service Resilience: A Dive into Health-Checking Techniques
Ensuring Kafka Service Resilience: A Dive into Health-Checking TechniquesEnsuring Kafka Service Resilience: A Dive into Health-Checking Techniques
Ensuring Kafka Service Resilience: A Dive into Health-Checking Techniques
 
Exactly-once Stream Processing with Arroyo and Kafka
Exactly-once Stream Processing with Arroyo and KafkaExactly-once Stream Processing with Arroyo and Kafka
Exactly-once Stream Processing with Arroyo and Kafka
 
Fish Plays Pokemon | Kafka Summit London
Fish Plays Pokemon | Kafka Summit LondonFish Plays Pokemon | Kafka Summit London
Fish Plays Pokemon | Kafka Summit London
 
Tiered Storage 101 | Kafla Summit London
Tiered Storage 101 | Kafla Summit LondonTiered Storage 101 | Kafla Summit London
Tiered Storage 101 | Kafla Summit London
 
Building a Self-Service Stream Processing Portal: How And Why
Building a Self-Service Stream Processing Portal: How And WhyBuilding a Self-Service Stream Processing Portal: How And Why
Building a Self-Service Stream Processing Portal: How And Why
 
From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...
From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...
From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...
 
Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...
Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...
Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...
 
Navigating Private Network Connectivity Options for Kafka Clusters
Navigating Private Network Connectivity Options for Kafka ClustersNavigating Private Network Connectivity Options for Kafka Clusters
Navigating Private Network Connectivity Options for Kafka Clusters
 
Apache Flink: Building a Company-wide Self-service Streaming Data Platform
Apache Flink: Building a Company-wide Self-service Streaming Data PlatformApache Flink: Building a Company-wide Self-service Streaming Data Platform
Apache Flink: Building a Company-wide Self-service Streaming Data Platform
 
Explaining How Real-Time GenAI Works in a Noisy Pub
Explaining How Real-Time GenAI Works in a Noisy PubExplaining How Real-Time GenAI Works in a Noisy Pub
Explaining How Real-Time GenAI Works in a Noisy Pub
 
TL;DR Kafka Metrics | Kafka Summit London
TL;DR Kafka Metrics | Kafka Summit LondonTL;DR Kafka Metrics | Kafka Summit London
TL;DR Kafka Metrics | Kafka Summit London
 
A Window Into Your Kafka Streams Tasks | KSL
A Window Into Your Kafka Streams Tasks | KSLA Window Into Your Kafka Streams Tasks | KSL
A Window Into Your Kafka Streams Tasks | KSL
 
Mastering Kafka Producer Configs: A Guide to Optimizing Performance
Mastering Kafka Producer Configs: A Guide to Optimizing PerformanceMastering Kafka Producer Configs: A Guide to Optimizing Performance
Mastering Kafka Producer Configs: A Guide to Optimizing Performance
 
Data Contracts Management: Schema Registry and Beyond
Data Contracts Management: Schema Registry and BeyondData Contracts Management: Schema Registry and Beyond
Data Contracts Management: Schema Registry and Beyond
 
Code-First Approach: Crafting Efficient Flink Apps
Code-First Approach: Crafting Efficient Flink AppsCode-First Approach: Crafting Efficient Flink Apps
Code-First Approach: Crafting Efficient Flink Apps
 
Debezium vs. the World: An Overview of the CDC Ecosystem
Debezium vs. the World: An Overview of the CDC EcosystemDebezium vs. the World: An Overview of the CDC Ecosystem
Debezium vs. the World: An Overview of the CDC Ecosystem
 
Beyond Tiered Storage: Serverless Kafka with No Local Disks
Beyond Tiered Storage: Serverless Kafka with No Local DisksBeyond Tiered Storage: Serverless Kafka with No Local Disks
Beyond Tiered Storage: Serverless Kafka with No Local Disks
 

Recently uploaded

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 

Recently uploaded (20)

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 

The Nuts and Bolts of Kafka Streams---An Architectural Deep Dive

  • 1. The Nuts and Bolts of Kafka Streams: An Architectural Deep Dive Matthias J. Sax Software Engineer | Apache Kafka Committer and PMC member Twitter/𝕏 @MatthiasJSax
  • 2. 2 10,000ft View The DSL Program (let’s go down the rabbit hole)
  • 3. Program 3 StreamsBuilder builder = … KStream words = builder.stream(…); KTable result = words.groupBy(…) .count(); result.toStream().to(…); Topology t = builder.build();
  • 4. Program 4 StreamsBuilder builder = … KStream words = builder.stream(…); KTable result = words.groupBy(…) .count(); result.toStream().to(…); Topology t = builder.build(); (Sub-)Topology src grpBy snk src cnt snk shuffle input result log 1 2 3 4 1 2 3 4
  • 5. Program 5 StreamsBuilder builder = … KStream words = builder.stream(…); KTable result = words.groupBy(…) .count(); result.toStream().to(…); Topology t = builder.build(); 1 2 3 4 1 2 3 4 (Sub-)Topology src grpBy snk src cnt snk shuffle input result log
  • 9. Instance B Instance A 9 Kafka Streams Instances and StreamThreads KafkaStreams streamsClient = new KafkaStreams(…); // num.stream.threads streamsClient.start(); streamsClient.addStreamThread(); streamsClient.removeStreamThread(); // error handling: uncaught exception handler
  • 10. 10 StreamThreads and Tasks KafkaConsumer KafkaProducer Kafka Cluster 1_0 1_1 0_0 0_1
  • 11. 11 Main Processing Loop while (running) records = mainConsumer.poll() // config: poll.ms // max.poll.records // fetch configs addRecordsToTask(records); // no deserialization // config: buffered.records.per.partition while (keepProcessing) for each task: if (task.isReady) // data available for all inputs? task.process(nRecords) // -> ts-extraction // -> (de)serialization + exception-handling maybePunctuate() maybeCommit() // config: commit.interval.ms [ctx.commit()] adjust nRecords
  • 12. 12 Main Processing Loop while (running) records = mainConsumer.poll() // config: poll.ms // max.poll.records // fetch configs addRecordsToTask(records); // no deserialization // config: buffered.records.per.partition while (keepProcessing) for each task: if (task.isReady) // data available for all inputs? task.process(nRecords) // -> ts-extraction // -> (de)serialization + exception-handling maybePunctuate() maybeCommit() // config: commit.interval.ms [ctx.commit()] adjust nRecords max.poll.interval.ms
  • 13. 13 Main Processing Loop while (running) records = mainConsumer.poll() // config: poll.ms // max.poll.records // fetch configs addRecordsToTask(records); // no deserialization // config: buffered.records.per.partition while (keepProcessing) for each task: if (task.isReady) // data available for all inputs? task.process(nRecords) // -> ts-extraction // -> (de)serialization + exception-handling maybePunctuate() maybeCommit() // config: commit.interval.ms [ctx.commit()] adjust nRecords max.poll.interval.ms
  • 14. 14 Task Execution 1_1 src cnt snk Record Buffer Record Buffer Record Buffer “head” records (deserialized) - exception handling - ts-extraction max.task.idle.ms src.process(record) ctx.forward(r) { cntProcessor.process(r) } store.put(…) { changelogger.put(…) } sink.process(record) { recordCollector.send(…) }
  • 15. 15 Task Execution 1_1 src cnt snk Record Buffer Record Buffer Record Buffer “head” records (deserialized) - exception handling - ts-extraction max.task.idle.ms src.process(record) ctx.forward(r) { cntProcessor.process(r) } store.put(…) { changelogger.put(…) } sink.process(record) { recordCollector.send(…) } depth-first execution
  • 16. 16 Task Execution 1_1 src cnt snk Record Buffer Record Buffer Record Buffer “head” records (deserialized) - exception handling - ts-extraction max.task.idle.ms src.process(record) ctx.forward(r) { cntProcessor.process(r) } store.put(…) { changelogger.put(…) } sink.process(record) { recordCollector.send(…) } depth-first execution RecordCollector • Serialization • Error handling • Partitioning KafkaProducer
  • 17. 17 Main Processing Loop while (running) records = mainConsumer.poll() // config: poll.ms // max.poll.records // fetch configs addRecordsToTask(records); // no deserialization // config: buffered.records.per.partition while (keepProcessing) for each task: if (task.isReady) // data available for all inputs? task.process(nRecords) // -> ts-extraction // -> (de)serialization + exception-handling maybePunctuate() maybeCommit() // config: commit.interval.ms [ctx.commit()] adjust nRecords max.poll.interval.ms
  • 18. 18 Main Processing Loop while (running) records = mainConsumer.poll() // config: poll.ms // max.poll.records // fetch configs addRecordsToTask(records); // no deserialization // config: buffered.records.per.partition while (keepProcessing) for each task: if (task.isReady) // data available for all inputs? task.process(nRecords) // -> ts-extraction // -> (de)serialization + exception-handling maybePunctuate() maybeCommit() // config: commit.interval.ms [ctx.commit()] adjust nRecords max.poll.interval.ms
  • 19. Committing 19 At-least-once: • flush state stores • flush producer • avoid data • get offset / consumer position • record buffers • commitSync() Exactly-once:
  • 20. Committing 20 At-least-once: • flush state stores • flush producer • avoid data • get offset / consumer position • record buffers • commitSync() Exactly-once: • flush state stores • flush producer • avoid data • get offsets / consumer position • record buffers • addOffsetsToTransaction() • commitTransaction() • configs: • transaction.timeout.ms
  • 21. 21 Main Processing Loop while (running) records = mainConsumer.poll() // config: poll.ms // max.poll.records // fetch configs addRecordsToTask(records); // no deserialization // config: buffered.records.per.partition while (keepProcessing) for each task: if (task.isReady) // data available for all inputs? task.process(nRecords) // -> ts-extraction // -> (de)serialization + exception-handling maybePunctuate() maybeCommit() // config: commit.interval.ms [ctx.commit()] adjust nRecords max.poll.interval.ms
  • 22. 22 Main Processing Loop while (running) records = mainConsumer.poll() // config: poll.ms // max.poll.records // fetch configs addRecordsToTask(records); // no deserialization // config: buffered.records.per.partition while (keepProcessing) for each task: if (task.isReady) // data available for all inputs? task.process(nRecords) // -> ts-extraction // -> (de)serialization + exception-handling maybePunctuate() maybeCommit() // config: commit.interval.ms [ctx.commit()] adjust nRecords max.poll.interval.ms transaction.timeout.ms
  • 24. Instance A 24 State Stores and Changelog Topics Task 1_1 cnt log-1
  • 25. Instance A 25 State Stores and Changelog Topics Task 1_1 cnt log-1
  • 26. 26 State Stores and Changelog Topics log-1 Instance B Task 1_1 restore
  • 27. 27 State Stores and Changelog Topics log-1 Instance B Task 1_1 cnt
  • 28. Restore Consumer 28 KafkaConsumer (restore) no consumer group read changelog topics used to restore state no offset commits while (running) { mainConsumer.poll() if (restoring) restoreConsumer.poll() restoreState() continue; processActiveTasks() }
  • 29. Instance A 29 Standby Tasks and High-Availability Task 1_1 cnt log-1
  • 30. Instance A 30 Standby Tasks and High-Availability Task 1_1 cnt log-1 Instance B Standby 1_1 restore
  • 31. Instance A 31 Standby Tasks and High-Availability Task 1_1 cnt log-1 Instance B Standby 1_1 restore config: num.standby.tasks
  • 32. Restore Consumer 32 KafkaConsumer (restore) no consumer group read changelog topics used to restore state no offset commits while (running) { mainConsumer.poll() if (restoring) restoreConsumer.poll() restoreState() continue; else restoreConsumer.poll() updateStandbys() processActiveTasks() }
  • 33. 33 Smooth Scaling with ”Warmup” Tasks Instance A Task 1_0 cnt log-0 Task 1_1 cnt log-1
  • 34. 34 Smooth Scaling with ”Warmup” Tasks Instance A Task 1_0 cnt log-0 Instance B Standby 1_1 (warmup) restore Task 1_1 cnt log-1
  • 35. 35 Smooth Scaling with ”Warmup” Tasks Instance A Task 1_0 cnt log-0 Instance B Task 1_1 cnt log-1
  • 36. 36 Smooth Scaling with ”Warmup” Tasks Instance A Task 1_0 cnt log-0 Instance B Task 1_1 cnt log-1 config: max.warmup.replicas
  • 38. Instance B Instance A 38 Global State Stores and GlobalStreamThread builder.globalTable(…); // or .addGlobalStore(…) KafkaStreams streamsClient = new KafkaStreams(…); streamsClient.start() GlobalStreamThread
  • 39. GlobalStreamThread 39 KafkaConsumer (global) no consumer group reads all global topic partitions no offset commits Global StateStore updating
  • 40. GlobalStreamThread 40 KafkaConsumer (global) no consumer group reads all global topic partitions no offset commits Global StateStore updating // bootstrap global state start() { while(!endOffsetsReached) globalConsumer.poll() updateGlobalState() } // start StreamThreads // regular processing while(running) { globalConsumer.poll() updateGlobalState() }
  • 42. Instance B Instance A 42 Thread Refactoring – StateUpdaterThread (AK 3.7 ?) GlobalStreamThread StateUpdaterThread
  • 43. Instance B Instance A 43 Thread Refactoring – Phase 2 Decouple consumer and processing threads (WIP) GlobalStreamThread ProcessingThread StateUpdaterThread
  • 44. The Nuts and Bolts of Kafka Streams: An Architectural Deep Dive Matthias J. Sax Software Engineer | Apache Kafka Committer and PMC member Twitter/𝕏 @MatthiasJSax