Streams Must Flow, Deploying fault-tolerant stream processing applications to Kubernetes

A presentation at NYC Kubernetes and Cloud Native Meetups in April 2019 in New York, NY, USA by Viktor Gamov

Slide 1

Slide 1

Streams Must Flow: fault-tolerant stream processing apps on Kubernetes April, 2019 / New York, NY @gamussa | #cloudnativenyc | @ConfluentINc

Slide 2

Slide 2

2 Special thanks! @gwenshap @gamussa @MatthiasJSax | #cloudnativenyc | @ConfluentINc

Slide 3

Slide 3

3 Agenda Kafka Streams 101 How do Kafka Streams applications scale? Stateful Workloads Recommendations for Kafka Streams @gamussa | #cloudnativenyc | @ConfluentINc

Slide 4

Slide 4

4 Kafka Streams – 101 Your App @gamussa | #cloudnativenyc | @ConfluentINc Other Systems Kafka Connect Kafka Connect Other Systems Kafka Streams

Slide 5

Slide 5

5 Stock Trade Stats Example KStream<String, Trade> source = builder.stream(STOCK_TOPIC); KStream<Windowed<String>, TradeStats> stats = source .groupByKey() .windowedBy(TimeWindows.of(5000).advanceBy(1000)) .aggregate(TradeStats::new, (k, v, tradestats) -> tradestats.add(v), Materialized.<~>as(“trade-aggregates”) .withValueSerde(new TradeStatsSerde())) .toStream() .mapValues(TradeStats::computeAvgPrice); stats.to(STATS_OUT_TOPIC, Produced.keySerde(WindowedSerdes.timeWindowedSerdeFrom(String.class))); @gamussa | #cloudnativenyc | @ConfluentINc

Slide 6

Slide 6

6 Stock Trade Stats Example KStream<String, Trade> source = builder.stream(STOCK_TOPIC); KStream<Windowed<String>, TradeStats> stats = source .groupByKey() .windowedBy(TimeWindows.of(5000).advanceBy(1000)) .aggregate(TradeStats::new, (k, v, tradestats) -> tradestats.add(v), Materialized.<~>as(“trade-aggregates”) .withValueSerde(new TradeStatsSerde())) .toStream() .mapValues(TradeStats::computeAvgPrice); stats.to(STATS_OUT_TOPIC, Produced.keySerde(WindowedSerdes.timeWindowedSerdeFrom(String.class))); @gamussa | #cloudnativenyc | @ConfluentINc

Slide 7

Slide 7

7 Stock Trade Stats Example KStream<String, Trade> source = builder.stream(STOCK_TOPIC); KStream<Windowed<String>, TradeStats> stats = source .groupByKey() .windowedBy(TimeWindows.of(5000).advanceBy(1000)) .aggregate(TradeStats::new, (k, v, tradestats) -> tradestats.add(v), Materialized.<~>as(“trade-aggregates”) .withValueSerde(new TradeStatsSerde())) .toStream() .mapValues(TradeStats::computeAvgPrice); stats.to(STATS_OUT_TOPIC, Produced.keySerde(WindowedSerdes.timeWindowedSerdeFrom(String.class))); @gamussa | #cloudnativenyc | @ConfluentINc

Slide 8

Slide 8

8 Stock Trade Stats Example KStream<String, Trade> source = builder.stream(STOCK_TOPIC); KStream<Windowed<String>, TradeStats> stats = source .groupByKey() .windowedBy(TimeWindows.of(5000).advanceBy(1000)) .aggregate(TradeStats::new, (k, v, tradestats) -> tradestats.add(v), Materialized.<~>as(“trade-aggregates”) .withValueSerde(new TradeStatsSerde())) .toStream() .mapValues(TradeStats::computeAvgPrice); stats.to(STATS_OUT_TOPIC, Produced.keySerde(WindowedSerdes.timeWindowedSerdeFrom(String.class))); @gamussa | #cloudnativenyc | @ConfluentINc

Slide 9

Slide 9

9 Topologies builder.stream Source Node state stores source.groupByKey .windowedBy(…) .aggregate(…) Processor Node mapValues() Processor Node streams Sink Node to(…) @gamussa | #cloudnativenyc | @ConfluentINc Processor Topology

Slide 10

Slide 10

How Do Kafka Streams Application Scale? @gamussa | #cloudnativenyc | @ConfluentINc

Slide 11

Slide 11

11 Partitions, Tasks, and Consumer Groups input topic Task executes processor topology One consumer group: can be executed with 1 - 4 threads on 1 - 4 machines 4 input topic partitions => 4 tasks result topic @gamussa | #cloudnativenyc | @ConfluentINc

Slide 12

Slide 12

12 Scaling with State “no state” Trade Stats App Instance 1 @gamussa | #cloudnativenyc | @ConfluentINc

Slide 13

Slide 13

Scaling with State “no state” Trade Stats App Trade Stats App Instance 1 @gamussa Instance 2 | #cloudnativenyc | @ConfluentINc 13

Slide 14

Slide 14

14 Scaling with State “no state” Trade Stats App Trade Stats App Instance 1 @gamussa Instance 2 | #cloudnativenyc | Trade Stats App Instance 3 @ConfluentINc

Slide 15

Slide 15

15 Scaling and FaultTolerance Two Sides of Same Coin @gamussa | #cloudnativenyc | @ConfluentINc

Slide 16

Slide 16

16 Fault-Tolerance Trade Stats App Trade Stats App Instance 1 @gamussa | Instance 2 #cloudnativenyc | Trade Stats App Instance 3 @ConfluentINc

Slide 17

Slide 17

17 Fault-Tolerant State State Updates Input Topic Changelog Topic Result Topic @gamussa | #cloudnativenyc | @ConfluentINc

Slide 18

Slide 18

18 Migrate State Trade Stats App Instance 1 Trade Stats App Instance 2 restore Changelog Topic @gamussa | #cloudnativenyc | @ConfluentINc

Slide 19

Slide 19

19 Recovery Time • Changelog topics are log compacted • Size of changelog topic linear in size of state • Large state implies high recovery times @gamussa | #cloudnativenyc | @ConfluentINc

Slide 20

Slide 20

20 Stateful Workloads @gamussa | #cloudnativenyc | @ConfluentINc

Slide 21

Slide 21

21 StatefulSet ● Rely on Headless Headless Service Service to provide network identity Pod-0 ● Ideal for highly Pod-1 Pod-2 Containers Containers Containers Volumes Volumes Volumes available stateful workloads @gamussa | #cloudnativenyc | @ConfluentINc

Slide 22

Slide 22

Recommendations for Kafka Streams @gamussa | #cloudnativenyc | @ConfluentINc

Slide 23

Slide 23

23 Stock Stats App Stock Stats App Stock Stats App Kafka Streams Kafka Streams Kafka Streams Instance 1 Instance 2 Instance 3 @gamussa | #cloudnativenyc | @ConfluentINc

Slide 24

Slide 24

24 WordCount App WordCount App WordCount App Kafka Streams Kafka Streams Kafka Streams Instance 1 Instance 2 Instance 3 @gamussa | #cloudnativenyc | @ConfluentINc

Slide 25

Slide 25

25 StatefulSets are new and complicated. We don’t need them. @gamussa | #cloudnativenyc | @ConfluentINc

Slide 26

Slide 26

26 Recovering state takes time. Statelful is faster. @gamussa | #cloudnativenyc | @ConfluentINc

Slide 27

Slide 27

27 But I’ll want to scale-out and back anyway. @gamussa | #cloudnativenyc | @ConfluentINc

Slide 28

Slide 28

28 @gamussa | #cloudnativenyc | @ConfluentINc

Slide 29

Slide 29

29 I don’t really trust my storage admin anyway @gamussa | #cloudnativenyc | @ConfluentINc

Slide 30

Slide 30

30 Recommendations ● Keep changelog shards small ● If you trust your storage: Use StatefulSets ● Use anti-affinity when possible ● Use “parallel” pod management

Slide 31

Slide 31

31 🛑 Stop! Demo time! @gamussa | #cloudnativenyc | @ConfluentINc

Slide 32

Slide 32

32 Summary Kafka Streams has recoverable state, that gives streams apps easy elasticity and high availability Kubernetes makes it easy to scale applications It also has StatefulSets for applications with state @gamussa | #cloudnativenyc | @ConfluentINc

Slide 33

Slide 33

33 Summary Now you know how to deploy Kafka Streams on Kubernetes and take advantage on all the scalability and highavailability capabilities @gamussa | #cloudnativenyc | @ConfluentINc

Slide 34

Slide 34

34 But what about Kafka itself? @gamussa | #cloudnativenyc | @ConfluentINc

Slide 35

Slide 35

35 Confluent Operator Automate provisioning Scale your Kafkas and CP clusters elastically Monitoring with Confluent Control Center or Prometheus Operate at scale with enterprise support from Confluent @gamussa | #cloudnativenyc | @ConfluentINc

Slide 36

Slide 36

36 Resources and Next Steps https://cnfl.io/helm_video https://cnfl.io/cp-helm https://cnfl.io/k8s https://slackpass.io/confluentcommunity #kubernetes @gamussa | #cloudnativenyc | @ConfluentINc

Slide 37

Slide 37

Thanks! @gamussa viktor@confluent.io We are hiring! https://www.confluent.io/careers/ @gamussa | @ #cloudnativenyc | @ConfluentINc

Slide 38

Slide 38

38