Event-Driven Architectures for Spring Developers

A presentation at SpringOne Platform by Pivotal in October 2019 in Austin, TX, USA by Viktor Gamov

Slide 1

Slide 1

1 #SpringOne @s1p Event-Driven Architectures for Spring Developers Viktor Gamov Developer Advocate, Confluent @gamussa Gary Russell Engineer, Pivotal @gprussell

Slide 2

Slide 2

2 Why Event Streaming ? @gamussa | #SpringOne | @gprussell

Slide 3

Slide 3

3 The world is changing.

Slide 4

Slide 4

4 The New Business Reality Past Today Technology was a support function Technology is the business Innovation required for growth Innovation required for survival Running the business on yesterday’s data Yesterday’s data = failure. was “good enough” Modern, real-time data infrastructure is required. @gamussa | #SpringOne | @gprussell

Slide 5

Slide 5

5 Taxis become Software 2 min @gamussa | #SpringOne | @gprussell

Slide 6

Slide 6

6 Transportation Then Hardware product Up-front purchase Opaque No data Now Hardware, Software, and Global Internet Service The world is changing. On-demand Real-time visibility Built on a foundation of data

Slide 7

Slide 7

7 What enables this transformation? @gamussa | #SpringOne | @gprussell

Slide 8

Slide 8

8 Cloud Machine Learning Mobile Event Streaming Rethink Data Centers Rethink Decision Making Rethink User Experience Rethink Data @gamussa | #SpringOne | @gprussell

Slide 9

Slide 9

9 Do you see me? Or: Would you blindly cross the street with traffic information that is 5 minutes old?

Slide 10

Slide 10

10 Transportation Banking Retail ETA Fraud detection Real-time inventory Real-time sensor diagnostics Trading and risk systems Real-time POS reporting Driver-rider match Mobile applications / customer experience Personalization @gamussa | #SpringOne Entertainment Real-time recommendations Personalized news feed In-app purchases | @gprussell

Slide 11

Slide 11

11 This is a fundamental paradigm shift… Cloud Future of the datacenter Infrastructure as code Event Streaming Future of data Data as continuous stream of events @gamussa | #SpringOne | @gprussell

Slide 12

Slide 12

17 12 The Event Streaming Paradigm @gamussa | #SpringOne | @gprussell

Slide 13

Slide 13

13 Two Problems in Application Infrastructure What’s the state of the world? What’s happening in the world? Solution: Solution: Databases Messaging, RPC, ETL, etc. @gamussa | #SpringOne | @gprussell

Slide 14

Slide 14

14 ETL/Data Integration Batch Messaging High Throughput Expensive Time Consuming Fast (Low Latency) Difficult to Scale Durable No Persistence Persistent Data Loss Maintains Order No Replay @gamussa | #SpringOne | @gprussell

Slide 15

Slide 15

15 ETL/Data Integration Batch Messaging High Throughput Expensive Time Consuming Fast (Low Latency) Difficult to Scale Durable No Persistence Persistent Data Loss Maintains Order No Replay Transient Messages Stored records @gamussa | #SpringOne | @gprussell

Slide 16

Slide 16

16 @gamussa | #SpringOne | @gprussell

Slide 17

Slide 17

17 ETL/Data Integration Batch Event Streaming Paradigm High High Throughput Throughput Expensive Time Consuming Durable Durable Fast Fast (Low (Low Latency) Latency) Messaging Difficult to Scale No Persistence Replay Persistent Persistent Data Loss Maintains Maintains Order Order No Replay Stored records Transient Messages @gamussa | #SpringOne | @gprussell

Slide 18

Slide 18

18 Event Streaming Paradigm To rethink data as neither stored records nor transient messages, but instead as a continuously updating Stream of Events @gamussa | #SpringOne | @gprussell

Slide 19

Slide 19

19 24 An Event records the fact that something happened A good was sold An invoice was issued @gamussa | A payment was made #SpringOne | @gprussell A new customer registered

Slide 20

Slide 20

20 25 A Stream represents history as a sequence of Events @gamussa | #SpringOne | @gprussell

Slide 21

Slide 21

21 26 Events change the way we think Monolithic Approach Event-First Approach ● a database ● an event ● a stream ● a variable ● a singleton ● an RPC ● a ‘data’ flow ● a stream processor Orders Service Payments Service Orders Service Order Validation Service Customers Service request response event streams Email Notification Service DB @gamussa | #SpringOne | @gprussell Tax Service

Slide 22

Slide 22

22 27 An Event Streaming Platform gives you three key functionalities Publish & Subscribe to Events @gamussa Store Events | #SpringOne Process & Analyze Events | @gprussell

Slide 23

Slide 23

30 23 Event-Driven Apps, with Historical Context Real-Time Inventory Real-Time Fraud Detection Real-Time Customer 360 Machine Learning Models Real-Time Data Transformation Event Streaming Platform … ✓ Real-time but also persistent ✓ Elastic, scalable, reliable ✓ High throughput, low latency ✓ All apps and systems can now speak to each other for a complete view of data Universal Event Pipeline Data Stores Logs @gamussa 3rd Party Apps | #SpringOne Custom Apps/Microservices | @gprussell

Slide 24

Slide 24

24 Why Combine Real-time With Historical Context? Contextual Event-Driven App Event-Driven App (Location Tracking) (ETA) 2 min Only Real-time Events Messaging Queues and Event Streaming Platforms can do this Whereisismy mydriver? driver? Where @gamussa | #SpringOne Real-time combined with stored data Only Event Streaming Platforms can do this When When will will my my driver driver get get here? here? | @gprussell

Slide 25

Slide 25

33 25 How to Build Event Streaming Architectures With Kafka @gamussa | #SpringOne | @gprussell

Slide 26

Slide 26

26 is a distributed event streaming platform Publish & Subscribe to Events @gamussa Store Events | #SpringOne Process & Analyze Events | @gprussell

Slide 27

Slide 27

35 27 01 Stream your data in real-time as Events 02 03 Store your Event Streams @gamussa | #SpringOne Process & Analyze your Events Streams | @gprussell

Slide 28

Slide 28

36 28 From apps, microservices Use a Kafka producer client from your favorite language … and many more 01 https://gamov.dev/kafka-clients-demo Stream your data in real-time as Events From/to other systems Use Kafka Connect plus a Connector for your system … and many more confluent.io/hub @gamussa | #SpringOne @gamussa | | @gprussell #SpringOne | @gprussell

Slide 29

Slide 29

29 From apps, microservices: producer example network write @Service class Producer @Autowired constructor(private val kafkaTemplate: KafkaTemplate<String, User>) { @Value(“${topic.name}”) lateinit var TOPIC: String; internal fun sendMessage(user: User) { this.kafkaTemplate.send(this.TOPIC, user.getName(), user) println(String.format(“Produced user !-> %s”, user)) } } … and more

Slide 30

Slide 30

30 From/to other systems: Kafka Connect and more Tip: Great option to gradually move workloads to Kafka while keeping production running!

Slide 31

Slide 31

31 Kafka Connect ● Deployed standalone (development) or as a distributed cluster (production) ● Elastic service that works on bare-metal, VMs, containers, Kubernetes, … ● The individual ‘Connector’ determines delivery guarantees, e.g., exactly-once VM VM

Slide 32

Slide 32

32 Single Message Transforms for real-time ETL Ingress: modify an Event before storing Egress: modify an Event on its way out ● Obfuscate sensitive information, e.g. PII ● Route high-priority events to faster stores ● Add origin of event for lineage tracking ● Direct events to different Elasticsearch ● Remove unnecessary data fields ● … and more { user: ab123, gender: female, ip: 1.2.3.95 } indexes ● Cast data types to match destination ● … and more { user: ab123, ip: 1.2.3.XXX }

Slide 33

Slide 33

33 Confluent Hub Discover Connectors, SMTs, and converters Documentation, support, etc. Easy installation confluent.io/hub @gamussa | #SpringOne | @gprussell

Slide 34

Slide 34

34 43 Kafka Cluster Storage is Distributed 02 Scalable Store your Event Streams Reliable Durable Performant VM @gamussa | #SpringOne | @gprussell

Slide 35

Slide 35

35 Kafka scales from S to XXL Messages / sec Topics 1 10,000,000 5 250,000 25,000 500 Partitions Brokers 1,000,000 25,000 300 1,500 25 3

Slide 36

Slide 36

36 https://www.confluent.io/blog/publishing-apache-kafka-new-york-times/ Store your Events as long as you want Event Streaming Paradigm Kafka = Source of Truth, stores every articleHighly since Scalable 1851 Fast (Low Latency) Durable Persistent Maintains Order Normalized assets (images, articles, bylines, etc.) @gamussa Denormalized into “Content View” | #SpringOne | @gprussell

Slide 37

Slide 37

37 Achievement Data Unlocked: All Your Data Now Available as Streams of Events @gamussa | #SpringOne | @gprussell

Slide 38

Slide 38

49 38 With Streaming SQL KSQL 03 With apps, microservices Process & Analyze your Events Streams streams or … and more Kafka consumer clients With separate frameworks … and more @gamussa | #SpringOne | @gprussell

Slide 39

Slide 39

39 Stream Processing with KSQL Process event streams to create new, continuously updated streams or tables Stream 03 Stream 01 Streaming Query Query Query Stream 02 Table CREATE TABLE OrderTotals AS SELECT * FROM … EMIT CHANGES

Slide 40

Slide 40

Up Stream Processing with KSQL com ing 40 fea tur e Query tables in Kafka from other apps, similar to a relational database Result Table Other Applications (Java, Go, Python, etc.) can directly query tables request-response via network (KSQL REST API) SELECT * FROM OrderTotals WHERE region = ‘Europe’ (KL IP8 )

Slide 41

Slide 41

41 An example use case Creating an event-driven dashboard from a currencies database Maxwell is streaming change events Results are continuously updating Elasticsearch Currencies Aggregations are computed in real-time https://github.com/garyrussell/s1p-2019

Slide 42

Slide 42

42 @StreamListener fun processCurrency(input: KStream<String, Double>) { val groupByKey: KGroupedStream<String, Double> = input.groupByKey() val countKTable = groupByKey.count() val sumKTable = groupByKey.reduce { value1, value2 !-> value1 + value2 } val avgRate = sumKTable.join(countKTable) { sum, count !-> sum / count } avgRate.toStream().to(“avg-rates”) } @gamussa | #SpringOne | @gprussell

Slide 43

Slide 43

43 @StreamListener fun processCurrency(input: KStream<String, Double>) { val groupByKey: KGroupedStream<String, Double> = input.groupByKey() val countKTable = groupByKey.count() val sumKTable = groupByKey.reduce { value1, value2 !-> value1 + value2 } val avgRate = sumKTable.join(countKTable) { sum, count !-> sum / count } avgRate.toStream().to(“avg-rates”) } @gamussa | #SpringOne | @gprussell

Slide 44

Slide 44

44 @StreamListener fun processCurrency(input: KStream<String, Double>) { val groupByKey: KGroupedStream<String, Double> = input.groupByKey() val countKTable = groupByKey.count() val sumKTable = groupByKey.reduce { value1, value2 !-> value1 + value2 } val avgRate = sumKTable.join(countKTable) { sum, count !-> sum / count } avgRate.toStream().to(“avg-rates”) } @gamussa | #SpringOne | @gprussell

Slide 45

Slide 45

45 @StreamListener fun processCurrency(input: KStream<String, Double>) { val groupByKey: KGroupedStream<String, Double> = input.groupByKey() val countKTable = groupByKey.count() val sumKTable = groupByKey.reduce { value1, value2 !-> value1 + value2 } val avgRate = sumKTable.join(countKTable) { sum, count !-> sum / count } avgRate.toStream().to(“avg-rates”) } @gamussa | #SpringOne | @gprussell

Slide 46

Slide 46

46 Where your Kafka Streams apps live KStreams Application App instance 1 network read/write fun processCurrency(input: KStream<String, Double>) { val groupByKey: KGroupedStream<String, Double> = input.groupByKey() val countKTable = groupByKey.count() val sumKTable = groupByKey.reduce { value1, value2 !-> value1 + value2 } VM val avgRate = sumKTable.join(countKTable) { sum, count !-> sum / count } avgRate.toStream().to(“avg-rates”) }

Slide 47

Slide 47

47 Where your Kafka Streams apps live KStreams Application App instance 1 … network read/write App instance n VM Elastic & Scalable Fault-tolerant Exactly-once Kafka security Aggregations Windowing Streams & Tables

Slide 48

Slide 48

48 Stream Processing with Kafka Streams apps Process event streams to create new, continuously updated streams or tables Inventory Event-driven apps and services communicate through Kafka Reporting Orders Frontend New apps can easily be added by tapping into existing event streams Shipping

Slide 49

Slide 49

49 Stream Processing with Kafka Streams apps Query your application’s tables and state from other apps Reporting App Table Result App instance 1 … App instance n request-response via network (e.g. REST API) Other Applications (Java, Go, Python, etc.) can directly query tables

Slide 50

Slide 50

50 ● Spring For Apache Kafka 2.3 What’s New? @gamussa | #SpringOne | @gprussell Producers • Option for Producer per thread • AggregatingReplyingKafkaTemplate

Slide 51

Slide 51

51 ● Spring For Apache Kafka 2.3 What’s New? @gamussa | #SpringOne | @gprussell Consumers • Consumer RecordInterceptor • Relative Seeks in ConsumerSeekAware • Configurable delay between poll()s • Micrometer Timers • Backoff between redeliveries after delivery failures • RetryingDeserializer

Slide 52

Slide 52

52 ● Spring For Apache Kafka 2.3 What’s New? @gamussa | #SpringOne | @gprussell Streams • RecoveringDeserializationExc eptionHandler • Transformers • Add headers (SpEL) • Invoke Spring Integration flows

Slide 53

Slide 53

53 ● Spring For Apache Kafka 2.3 What’s New? @gamussa | #SpringOne | @gprussell Miscellaneous • Delegating Serializer/ Deserializer

Slide 54

Slide 54

54 is a distributed event streaming platform Publish & Subscribe to Events @gamussa Store Events | #SpringOne Process & Analyze Events | @gprussell

Slide 55

Slide 55

64 55 Where to go from here for more details on event-driven architectures with Kafka @gamussa | #SpringOne | @gprussell

Slide 56

Slide 56

56 THANK YOU @gamussa viktor@confluent.io @gprussell grussell@pivotal.io cnfl.io/meetups cnfl.io/blog cnfl.io/slack