Slide 1

Slide 1 text

Event Sourcing w/ Kafka Streams Amitay Horwitz | @amitayh Milan | November 29 - 30, 2018

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

AGENDA ✅

Slide 5

Slide 5 text

AGENDA ✅ • Event sourcing 101

Slide 6

Slide 6 text

AGENDA ✅ • Event sourcing 101 • Eventim

Slide 7

Slide 7 text

AGENDA ✅ • Event sourcing 101 • Eventim • Kafka & Kafka Streams

Slide 8

Slide 8 text

AGENDA ✅ • Event sourcing 101 • Eventim • Kafka & Kafka Streams • Putting it all together

Slide 9

Slide 9 text

BACKGROUND

Slide 10

Slide 10 text

A SERVICE IS BORN ✒ • Wix Invoices was incepted in mid 2015 • Rich domain model • Auditing is important for monetary products

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

invoices invoice_id customer_id issue_date due_date sent_date currency status line_items line_item_id invoice_id description quantity price customers customer_id name email address payments transaction_id invoice_id payment_type payment_amount taxes tax_id name rate NAÏVE SOLUTION tax_to_line line_item_id tax_id

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

HOW DO YOU HYDRATE YOUR DOMAIN OBJECTS? SELECT e.employee_id AS "Employee #" , e.first_name || ' ' || e.last_name AS "Name" , e.email AS "Email" , e.phone_number AS "Phone" , TO_CHAR(e.hire_date, 'MM/DD/YYYY') AS "Hire Date" , TO_CHAR(e.salary, 'L99G999D99', 'NLS_NUMERIC_CHARACTERS = ''.,'' NLS_CURRENCY = ''$''') AS "Salary" , e.commission_pct AS "Comission %" , 'works as ' || j.job_title || ' in ' || d.department_name || ' department (manager: ' || dm.first_name || ' ' || dm.last_name || ') and immediate supervisor: ' || m.first_name || ' ' || m.last_name AS "Current Job" , TO_CHAR(j.min_salary, 'L99G999D99', 'NLS_NUMERIC_CHARACTERS = ''.,'' NLS_CURRENCY = ''$''') || ' - ' || TO_CHAR(j.max_salary, 'L99G999D99', 'NLS_NUMERIC_CHARACTERS = ''.,'' NLS_CURRENCY = ''$''') AS "Current Salary" , l.street_address || ', ' || l.postal_code || ', ' || l.city || ', ' || l.state_province || ', ' || c.country_name || ' (' || r.region_name || ')' AS "Location" , jh.job_id AS "History Job ID" , 'worked from ' || TO_CHAR(jh.start_date, 'MM/DD/YYYY') || ' to ' || TO_CHAR(jh.end_date, 'MM/DD/YYYY') || ' as ' || jj.job_title || ' in ' || dd.department_name || ' department' AS "History Job Title" FROM employees e -- to get title of current job_id JOIN jobs j ON e.job_id = j.job_id -- to get name of current manager_id LEFT JOIN employees m ON e.manager_id = m.employee_id -- to get name of current department_id LEFT JOIN departments d ON d.department_id = e.department_id -- to get name of manager of current department -- (not equal to current manager and can be equal to the employee itself) LEFT JOIN employees dm ON d.manager_id = dm.employee_id -- to get name of location

Slide 23

Slide 23 text

HOW DO YOU HYDRATE YOUR DOMAIN OBJECTS? SELECT e.employee_id AS "Employee #" , e.first_name || ' ' || e.last_name AS "Name" , e.email AS "Email" , e.phone_number AS "Phone" , TO_CHAR(e.hire_date, 'MM/DD/YYYY') AS "Hire Date" , TO_CHAR(e.salary, 'L99G999D99', 'NLS_NUMERIC_CHARACTERS = ''.,'' NLS_CURRENCY = ''$''') AS "Salary" , e.commission_pct AS "Comission %" , 'works as ' || j.job_title || ' in ' || d.department_name || ' department (manager: ' || dm.first_name || ' ' || dm.last_name || ') and immediate supervisor: ' || m.first_name || ' ' || m.last_name AS "Current Job" , TO_CHAR(j.min_salary, 'L99G999D99', 'NLS_NUMERIC_CHARACTERS = ''.,'' NLS_CURRENCY = ''$''') || ' - ' || TO_CHAR(j.max_salary, 'L99G999D99', 'NLS_NUMERIC_CHARACTERS = ''.,'' NLS_CURRENCY = ''$''') AS "Current Salary" , l.street_address || ', ' || l.postal_code || ', ' || l.city || ', ' || l.state_province || ', ' || c.country_name || ' (' || r.region_name || ')' AS "Location" , jh.job_id AS "History Job ID" , 'worked from ' || TO_CHAR(jh.start_date, 'MM/DD/YYYY') || ' to ' || TO_CHAR(jh.end_date, 'MM/DD/YYYY') || ' as ' || jj.job_title || ' in ' || dd.department_name || ' department' AS "History Job Title" FROM employees e -- to get title of current job_id JOIN jobs j ON e.job_id = j.job_id -- to get name of current manager_id LEFT JOIN employees m ON e.manager_id = m.employee_id -- to get name of current department_id LEFT JOIN departments d ON d.department_id = e.department_id -- to get name of manager of current department -- (not equal to current manager and can be equal to the employee itself) LEFT JOIN employees dm ON d.manager_id = dm.employee_id -- to get name of location

Slide 24

Slide 24 text

DOMAIN MODEL ≠ DB TABLE

Slide 25

Slide 25 text

EVENT SOURCING 101

Slide 26

Slide 26 text

MUTABLE STATE

Slide 27

Slide 27 text

MUTABLE STATE • Instead of saving the current state, we save the succession of events that brought us to this state

Slide 28

Slide 28 text

MUTABLE STATE • Instead of saving the current state, we save the succession of events that brought us to this state • currentState = fold(events, emptyState)

Slide 29

Slide 29 text

INVOICE LIFECYCLE time

Slide 30

Slide 30 text

Invoice created INVOICE LIFECYCLE { "customer": {...}, "issueDate": "2018-01-01", "dueDate": "2018-02-01", "lineItems: [], "status": "DRAFT" } time

Slide 31

Slide 31 text

Invoice created { "customer": {...}, "issueDate": "2018-01-01", "dueDate": "2018-02-01", "lineItems: [{"price": 1.99, "qty": 1}], "status": "DRAFT" } Line item added time INVOICE LIFECYCLE

Slide 32

Slide 32 text

Invoice created INVOICE LIFECYCLE { "customer": {...}, "issueDate": "2018-01-01", "dueDate": "2018-02-01", "lineItems: [{"price": 1.99, "qty": 1}, {"price": 3.50, "qty": 2}], "status": "DRAFT" } Line item added Line item added time

Slide 33

Slide 33 text

Invoice created INVOICE LIFECYCLE { "customer": {...}, "issueDate": "2018-01-01", "dueDate": "2018-02-01", "lineItems: [{"price": 3.50, "qty": 2}], "status": "DRAFT" } Line item added Line item added Line item removed time

Slide 34

Slide 34 text

Invoice created INVOICE LIFECYCLE { "customer": {...}, "issueDate": "2018-01-01", "dueDate": "2018-02-01", "lineItems: [{"price": 3.50, "qty": 2}], "status": "SENT" } Line item added Line item added Line item removed Invoice sent to customer time

Slide 35

Slide 35 text

Invoice created INVOICE LIFECYCLE { "customer": {...}, "issueDate": "2018-01-01", "dueDate": "2018-02-01", "lineItems: [{"price": 3.50, "qty": 2}], "status": "PAID" } Line item added Line item added Line item removed Invoice sent to customer Payment received time

Slide 36

Slide 36 text

EVENTIM

Slide 37

Slide 37 text

DESIGN GOALS • Small and simple library • Non-intrusive • Maintain data integrity • Easily add custom views

Slide 38

Slide 38 text

WRITE PATH ✏ Event Sourced
 Command Handler User Interface

Slide 39

Slide 39 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface

Slide 40

Slide 40 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface invoice ID: 12345
 expected version: 5
 amount: $12.34
 Add Payment

Slide 41

Slide 41 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface invoice ID: 12345
 expected version: 5
 amount: $12.34
 Add Payment Aggregare
 Repository Load

Slide 42

Slide 42 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface invoice ID: 12345
 expected version: 5
 amount: $12.34
 Add Payment Aggregare
 Repository Load Event
 Store Get events

Slide 43

Slide 43 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface invoice ID: 12345
 expected version: 5
 amount: $12.34
 Add Payment Aggregare
 Repository Load Event
 Store Get events version: 5
 customer: {...}
 line items: [...]
 balance: $12.34 Invoice #12345

Slide 44

Slide 44 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface Aggregare
 Repository Load Event
 Store Get events invoice ID: 12345
 expected version: 5
 amount: $12.34 Add Payment version: 5
 customer: {...}
 line items: [...]
 balance: $12.34 Invoice #12345

Slide 45

Slide 45 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface Aggregare
 Repository Load Event
 Store Get events invoice ID: 12345
 expected version: 5
 amount: $12.34 Add Payment version: 5
 customer: {...}
 line items: [...]
 balance: $12.34 Invoice #12345

Slide 46

Slide 46 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface Aggregare
 Repository Load Event
 Store Get events

Slide 47

Slide 47 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface Aggregare
 Repository Load Event
 Store Get events Payment Added: $12.34

Slide 48

Slide 48 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface Aggregare
 Repository Load Event
 Store Get events Payment Added: $12.34 Status Changed: Paid

Slide 49

Slide 49 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface Aggregare
 Repository Load Event
 Store Get events Payment Added: $12.34 Status Changed: Paid version: 7
 customer: {...}
 line items: [...]
 balance: $0.00 Invoice #12345

Slide 50

Slide 50 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface Aggregare
 Repository Load Event
 Store Get events Payment Added: $12.34 Status Changed: Paid Publish events (OCC) version: 7
 customer: {...}
 line items: [...]
 balance: $0.00 Invoice #12345

Slide 51

Slide 51 text

WRITE PATH ✏ Event Sourced
 Command Handler Command User Interface Aggregare
 Repository Load Event
 Store Get events Payment Added: $12.34 Status Changed: Paid Publish events (OCC) Event Bus

Slide 52

Slide 52 text

READ PATH Event Bus

Slide 53

Slide 53 text

READ PATH Event Bus Events

Slide 54

Slide 54 text

READ PATH View Projector Event Handler Event Bus Events

Slide 55

Slide 55 text

READ PATH View Projector Event Handler DB Event Bus Events

Slide 56

Slide 56 text

READ PATH View Projector Event Handler DB Event Bus Events invoice_id customer balance status 12345 John Doe $12.34 New 67890 Jane Doe $34.56 Sent

Slide 57

Slide 57 text

READ PATH View Projector Event Handler DB Event Bus Events invoice_id customer balance status 12345 John Doe $12.34 New 67890 Jane Doe $34.56 Sent Payment Added: $12.34

Slide 58

Slide 58 text

READ PATH View Projector Event Handler DB Event Bus Events invoice_id customer balance status 12345 John Doe $0.00 New 67890 Jane Doe $34.56 Sent

Slide 59

Slide 59 text

READ PATH View Projector Event Handler DB Event Bus Events invoice_id customer balance status 12345 John Doe $0.00 New 67890 Jane Doe $34.56 Sent Status Changed: Paid

Slide 60

Slide 60 text

READ PATH View Projector Event Handler DB Event Bus Events invoice_id customer balance status 12345 John Doe $0.00 Paid 67890 Jane Doe $34.56 Sent

Slide 61

Slide 61 text

READ PATH View Projector Event Handler DB Event Bus Events

Slide 62

Slide 62 text

READ PATH View Projector Event Handler DB Event Bus Events

Slide 63

Slide 63 text

READ PATH View Projector Event Handler DB Event Bus Events

Slide 64

Slide 64 text

READ PATH View Projector Event Handler DB Event Bus Events

Slide 65

Slide 65 text

READ PATH View Projector Event Handler DB Event Bus Events User Interface

Slide 66

Slide 66 text

READ PATH View Projector Event Handler DB Queries Event Bus Events User Interface

Slide 67

Slide 67 text

READ PATH Mailer ✉ Event Handler View Projector Event Handler DB Queries Event Bus Events User Interface

Slide 68

Slide 68 text

READ PATH Mailer ✉ Event Handler View Projector Event Handler Reporting Event Handler DB Queries Event Bus Events User Interface

Slide 69

Slide 69 text

PAIN POINTS

Slide 70

Slide 70 text

PAIN POINTS • Persisting events and publishing them is not atomic

Slide 71

Slide 71 text

PAIN POINTS • Persisting events and publishing them is not atomic • Inherent eventual consistency is not integrated in the product (read after write)

Slide 72

Slide 72 text

PAIN POINTS • Persisting events and publishing them is not atomic • Inherent eventual consistency is not integrated in the product (read after write) • Rebuilding views is a complex operation

Slide 73

Slide 73 text

REBUILDING VIEWS version event_payload

Slide 74

Slide 74 text

REBUILDING VIEWS version event_payload 1 InvoiceCreated(...)

Slide 75

Slide 75 text

REBUILDING VIEWS version event_payload 1 InvoiceCreated(...) 2 LineItemAdded(...)

Slide 76

Slide 76 text

REBUILDING VIEWS version event_payload 1 InvoiceCreated(...) 2 LineItemAdded(...) 3 LineItemAdded(...)

Slide 77

Slide 77 text

REBUILDING VIEWS version event_payload 1 InvoiceCreated(...) 2 LineItemAdded(...) 3 LineItemAdded(...) 4 InvoiceSent(...)

Slide 78

Slide 78 text

REBUILDING VIEWS SELECT *
 FROM events
 WHERE version > 0
 ORDER BY version
 LIMIT 100;

Slide 79

Slide 79 text

REBUILDING VIEWS SELECT *
 FROM events
 WHERE version > 100
 ORDER BY version
 LIMIT 100;

Slide 80

Slide 80 text

REBUILDING VIEWS invoice_id invoice_version event_payload

Slide 81

Slide 81 text

REBUILDING VIEWS invoice_id invoice_version event_payload 12345 1 InvoiceCreated

Slide 82

Slide 82 text

REBUILDING VIEWS invoice_id invoice_version event_payload 12345 1 InvoiceCreated 12345 2 LineItemAdded

Slide 83

Slide 83 text

REBUILDING VIEWS invoice_id invoice_version event_payload 12345 1 InvoiceCreated 12345 2 LineItemAdded 67890 1 InvoiceCreated

Slide 84

Slide 84 text

REBUILDING VIEWS invoice_id invoice_version event_payload 12345 1 InvoiceCreated 12345 2 LineItemAdded 67890 1 InvoiceCreated 67890 2 InvoiceDeleted

Slide 85

Slide 85 text

REBUILDING VIEWS invoice_id invoice_version event_payload 12345 1 InvoiceCreated 12345 2 LineItemAdded 12345 3 InvoiceSent 67890 1 InvoiceCreated 67890 2 InvoiceDeleted

Slide 86

Slide 86 text

REBUILDING VIEWS invoice_id version payload timestamp 12345 1 ... 14:05 12345 2 ... 14:06 12345 3 ... 15:50 67890 1 ... 15:30 67890 2 ... 15:33

Slide 87

Slide 87 text

REBUILDING VIEWS invoice_id version payload timestamp 12345 1 ... 14:05 12345 2 ... 14:06 67890 1 ... 15:30 67890 2 ... 15:33 12345 3 ... 15:50

Slide 88

Slide 88 text

REBUILDING VIEWS invoice_id version payload order 12345 1 ... 1 12345 2 ... 2 67890 1 ... 3 67890 2 ... 4 12345 3 ... 5

Slide 89

Slide 89 text

KAFKA & KAFKA STREAMS 101

Slide 90

Slide 90 text

APACHE KAFKA • Distributed append-only log • Replicated, fault-tolerant • Often used as pub-sub or queue • Used heavily at LinkedIn, Netflix, Wix and many others

Slide 91

Slide 91 text

KAFKA TOPICS 6 5 4 3 2 1 4 3 2 1 7 6 5 4 3 2 1 P0 P1 P2 Producer

Slide 92

Slide 92 text

KAFKA TOPICS Consumer Group Node #1 6 5 4 3 2 1 4 3 2 1 7 6 5 4 3 2 1 P0 P1 P2

Slide 93

Slide 93 text

KAFKA TOPICS Consumer Group Node #1 6 5 4 3 2 1 4 3 2 1 7 6 5 4 3 2 1 P0 P1 P2 Node #2

Slide 94

Slide 94 text

TOPICS RETENTION 7 6 5 4 3 2 1 Consumer #1

Slide 95

Slide 95 text

TOPICS RETENTION 7 6 5 4 3 2 1 Consumer #1 Consumer #2

Slide 96

Slide 96 text

TOPICS RETENTION 7 6 5 4 3 2 1 Consumer #1 Consumer #2

Slide 97

Slide 97 text

TOPICS RETENTION 7 6 5 4 3 2 1 Consumer #1 Consumer #2

Slide 98

Slide 98 text

KAFKA STREAMS

Slide 99

Slide 99 text

KAFKA STREAMS Streams

Slide 100

Slide 100 text

KAFKA STREAMS Streams Tables

Slide 101

Slide 101 text

STREAMS ✈ • "Data in flight" • Unbounded, continuously updating data set • Ordered, replayable, sequence of immutable data key-value pairs

Slide 102

Slide 102 text

TABLES • "Data at rest" • A collection of evolving facts • A point-in-time view of aggregated data

Slide 103

Slide 103 text

STREAM-TABLE DUALITY

Slide 104

Slide 104 text

STREAM-TABLE DUALITY

Slide 105

Slide 105 text

STREAM-TABLE DUALITY Stream of all changes to a table

Slide 106

Slide 106 text

STREAM-TABLE DUALITY Stream of all changes to a table Materialize a stream into a table

Slide 107

Slide 107 text

STREAM-TABLE DUALITY User Pageviews alice 1

Slide 108

Slide 108 text

STREAM-TABLE DUALITY User Pageviews alice 1 User Pageviews alice 1 charlie 1

Slide 109

Slide 109 text

STREAM-TABLE DUALITY User Pageviews alice 1 User Pageviews alice 1 charlie 1 User Pageviews alice 2 charlie 1

Slide 110

Slide 110 text

STREAM-TABLE DUALITY User Pageviews alice 1 User Pageviews alice 1 charlie 1 User Pageviews alice 2 charlie 1 ("alice", 1)

Slide 111

Slide 111 text

STREAM-TABLE DUALITY User Pageviews alice 1 User Pageviews alice 1 charlie 1 User Pageviews alice 2 charlie 1 ("alice", 1) ("charlie", 1)

Slide 112

Slide 112 text

STREAM-TABLE DUALITY User Pageviews alice 1 User Pageviews alice 1 charlie 1 User Pageviews alice 2 charlie 1 ("alice", 1) ("charlie", 1) ("alice", 2)

Slide 113

Slide 113 text

STREAM-TABLE DUALITY User Pageviews alice 1 User Pageviews alice 1 charlie 1 User Pageviews alice 2 charlie 1 ("alice", 1) ("charlie", 1) ("alice", 2) User Pageviews alice 1

Slide 114

Slide 114 text

STREAM-TABLE DUALITY User Pageviews alice 1 User Pageviews alice 1 charlie 1 User Pageviews alice 2 charlie 1 ("alice", 1) ("charlie", 1) ("alice", 2) User Pageviews alice 1 User Pageviews alice 1 charlie 1

Slide 115

Slide 115 text

STREAM-TABLE DUALITY User Pageviews alice 1 User Pageviews alice 1 charlie 1 User Pageviews alice 2 charlie 1 ("alice", 1) ("charlie", 1) ("alice", 2) User Pageviews alice 1 User Pageviews alice 1 charlie 1 User Pageviews alice 2 charlie 1

Slide 116

Slide 116 text

STREAM TRANSFORMATIONS

Slide 117

Slide 117 text

STREAM TRANSFORMATIONS

Slide 118

Slide 118 text

STREAM TRANSFORMATIONS

Slide 119

Slide 119 text

STREAM TRANSFORMATIONS • map

Slide 120

Slide 120 text

STREAM TRANSFORMATIONS • map • filter

Slide 121

Slide 121 text

STREAM TRANSFORMATIONS • map • filter • flatMap

Slide 122

Slide 122 text

STREAM TRANSFORMATIONS • map • filter • flatMap • …

Slide 123

Slide 123 text

STREAM TRANSFORMATIONS • map • filter • flatMap • …

Slide 124

Slide 124 text

STREAM TRANSFORMATIONS

Slide 125

Slide 125 text

STREAM TRANSFORMATIONS

Slide 126

Slide 126 text

STREAM TRANSFORMATIONS

Slide 127

Slide 127 text

STREAM TRANSFORMATIONS Stream-stream join

Slide 128

Slide 128 text

STREAM TRANSFORMATIONS

Slide 129

Slide 129 text

STREAM TRANSFORMATIONS

Slide 130

Slide 130 text

STREAM TRANSFORMATIONS

Slide 131

Slide 131 text

STREAM TRANSFORMATIONS Stream-table join

Slide 132

Slide 132 text

STREAM PROCESSING APP Streams API Your app

Slide 133

Slide 133 text

STREAM PROCESSING APP Streams API Your app • Transforms and enriches data

Slide 134

Slide 134 text

STREAM PROCESSING APP Streams API Your app • Transforms and enriches data • Stateless / stateful processing

Slide 135

Slide 135 text

STREAM PROCESSING APP Streams API Your app • Transforms and enriches data • Stateless / stateful processing • Supports windowing operations

Slide 136

Slide 136 text

STREAM PROCESSING APP Streams API Your app • Transforms and enriches data • Stateless / stateful processing • Supports windowing operations • Embedded in your app

Slide 137

Slide 137 text

STREAM PROCESSING APP Streams API Your app • Transforms and enriches data • Stateless / stateful processing • Supports windowing operations • Embedded in your app • Elastic, scaleable, fault-tolerant

Slide 138

Slide 138 text

val props = new Properties props.put( StreamsConfig.BOOTSTRAP_SERVERS_CONFIG,
 "localhost:9092") props.put( StreamsConfig.APPLICATION_ID_CONFIG, "my-streams-app")

Slide 139

Slide 139 text

STREAM PROCESSING APP Streams API Your app Streams API Your app Streams API Your app

Slide 140

Slide 140 text

PROCESSOR TOPOLOGY group map join filter join Source processor Sink processor Source processor

Slide 141

Slide 141 text

PROCESSOR TOPOLOGY group map join filter join Source processor Sink processor Source processor state-store

Slide 142

Slide 142 text

PROCESSOR API • The most low-level • Interact with state-stores, schedulers, etc. • All standard operations are implemented like this (map / filter / …) • Create your own custom processing logic!

Slide 143

Slide 143 text

STREAMS DSL • Programmatically describe your topology

Slide 144

Slide 144 text

STREAMS DSL val builder = new StreamsBuilder • Programmatically describe your topology

Slide 145

Slide 145 text

STREAMS DSL val builder = new StreamsBuilder val textLines: KStream[String, String] = builder.stream("TextLinesTopic") • Programmatically describe your topology

Slide 146

Slide 146 text

STREAMS DSL val builder = new StreamsBuilder val textLines: KStream[String, String] = builder.stream("TextLinesTopic") val wordCounts: KTable[String, Long] = textLines • Programmatically describe your topology

Slide 147

Slide 147 text

STREAMS DSL val builder = new StreamsBuilder val textLines: KStream[String, String] = builder.stream("TextLinesTopic") val wordCounts: KTable[String, Long] = textLines .flatMapValues(textLine => textLine.split("\\W+")) • Programmatically describe your topology

Slide 148

Slide 148 text

STREAMS DSL val builder = new StreamsBuilder val textLines: KStream[String, String] = builder.stream("TextLinesTopic") val wordCounts: KTable[String, Long] = textLines .flatMapValues(textLine => textLine.split("\\W+")) .groupBy((_, word) => word) • Programmatically describe your topology

Slide 149

Slide 149 text

STREAMS DSL val builder = new StreamsBuilder val textLines: KStream[String, String] = builder.stream("TextLinesTopic") val wordCounts: KTable[String, Long] = textLines .flatMapValues(textLine => textLine.split("\\W+")) .groupBy((_, word) => word) .count(Materialized.as("counts-store")) • Programmatically describe your topology

Slide 150

Slide 150 text

STREAMS DSL val builder = new StreamsBuilder val textLines: KStream[String, String] = builder.stream("TextLinesTopic") val wordCounts: KTable[String, Long] = textLines .flatMapValues(textLine => textLine.split("\\W+")) .groupBy((_, word) => word) .count(Materialized.as("counts-store")) wordCounts.toStream.to("WordsWithCountsTopic") • Programmatically describe your topology

Slide 151

Slide 151 text

STREAMS DSL val builder = new StreamsBuilder val textLines: KStream[String, String] = builder.stream("TextLinesTopic") val wordCounts: KTable[String, Long] = textLines .flatMapValues(textLine => textLine.split("\\W+")) .groupBy((_, word) => word) .count(Materialized.as("counts-store")) wordCounts.toStream.to("WordsWithCountsTopic") • Programmatically describe your topology

Slide 152

Slide 152 text

KSQL • SQL dialect for streaming data CREATE TABLE possible_fraud AS SELECT card_number, count(*) FROM authorization_attempts WINDOW TUMBLING (SIZE 5 SECONDS) GROUP BY card_number HAVING count(*) > 3;

Slide 153

Slide 153 text

PUTTING EVERYTHING TOGETHER

Slide 154

Slide 154 text

HIGH LEVEL DESIGN User Interface

Slide 155

Slide 155 text

HIGH LEVEL DESIGN User Interface

Slide 156

Slide 156 text

HIGH LEVEL DESIGN User Interface

Slide 157

Slide 157 text

HIGH LEVEL DESIGN User Interface {"create":…}

Slide 158

Slide 158 text

HIGH LEVEL DESIGN User Interface invoice-commands

Slide 159

Slide 159 text

HIGH LEVEL DESIGN User Interface Command Handler

Slide 160

Slide 160 text

HIGH LEVEL DESIGN User Interface Command Handler Stream processing app

Slide 161

Slide 161 text

HIGH LEVEL DESIGN User Interface command-results Command Handler Web Socket

Slide 162

Slide 162 text

HIGH LEVEL DESIGN User Interface Projector Projection DB Command Handler

Slide 163

Slide 163 text

HIGH LEVEL DESIGN User Interface Projector Projection DB Command Handler

Slide 164

Slide 164 text

HIGH LEVEL DESIGN User Interface Projector Projector Command Handler Command Handler Command Handler

Slide 165

Slide 165 text

No content

Slide 166

Slide 166 text

COMMAND HANDLER

Slide 167

Slide 167 text

COMMAND HANDLER commands stream invoice-commands

Slide 168

Slide 168 text

COMMAND HANDLER commands stream invoice-commands invoice ID: 12345
 command ID: 67890
 amount: $12.34
 Add Payment

Slide 169

Slide 169 text

COMMAND HANDLER commands stream invoice-commands invoice ID: 12345
 command ID: 67890
 amount: $12.34
 Add Payment

Slide 170

Slide 170 text

COMMAND HANDLER transform: results commands stream invoice-commands invoice ID: 12345
 command ID: 67890
 amount: $12.34 Add Payment

Slide 171

Slide 171 text

COMMAND HANDLER transform: results commands stream invoice-commands Snapshots state-store invoice ID: 12345
 command ID: 67890
 amount: $12.34 Add Payment

Slide 172

Slide 172 text

COMMAND HANDLER transform: results commands stream invoice-commands Snapshots state-store invoice ID: 12345
 command ID: 67890
 amount: $12.34 Add Payment customer: {...}
 line items: [...]
 balance: $12.34 Invoice #12345

Slide 173

Slide 173 text

COMMAND HANDLER transform: results commands stream invoice-commands Snapshots state-store invoice ID: 12345
 command ID: 67890
 amount: $12.34 Add Payment customer: {...}
 line items: [...]
 balance: $12.34 Invoice #12345

Slide 174

Slide 174 text

COMMAND HANDLER transform: results commands stream invoice-commands Snapshots state-store

Slide 175

Slide 175 text

COMMAND HANDLER transform: results commands stream invoice-commands Snapshots state-store Payment Added: $12.34

Slide 176

Slide 176 text

COMMAND HANDLER transform: results commands stream invoice-commands Snapshots state-store Payment Added: $12.34 Status Changed: Paid

Slide 177

Slide 177 text

COMMAND HANDLER transform: results commands stream invoice-commands Snapshots state-store Payment Added: $12.34 Status Changed: Paid customer: {...}
 line items: [...]
 balance: $0.00 Invoice #12345

Slide 178

Slide 178 text

COMMAND HANDLER command-results transform: results commands stream invoice-commands Snapshots state-store command ID: 67890
 snapshot: {...}
 events: [...] Successful

Slide 179

Slide 179 text

COMMAND HANDLER command-results transform: results commands stream invoice-commands Snapshots state-store

Slide 180

Slide 180 text

COMMAND HANDLER command-results transform: results commands stream invoice-commands Snapshots state-store command ID: 67890
 cause: invoice deleted
 
 Failed

Slide 181

Slide 181 text

COMMAND HANDLER command-results transform: results commands stream invoice-commands Snapshots state-store

Slide 182

Slide 182 text

COMMAND HANDLER command-results transform: results commands stream invoice-commands Snapshots state-store Short retention (~1 hour)

Slide 183

Slide 183 text

COMMAND HANDLER command-results filter: successful transform: results commands stream invoice-commands Snapshots state-store

Slide 184

Slide 184 text

COMMAND HANDLER command-results invoice-events filter: successful transform: results commands stream invoice-commands Snapshots state-store flatMap: events

Slide 185

Slide 185 text

COMMAND HANDLER command-results invoice-events filter: successful transform: results commands stream invoice-commands Snapshots state-store flatMap: events map: snapshots invoice-snapshots

Slide 186

Slide 186 text

COMMAND HANDLER command-results invoice-events filter: successful transform: results commands stream invoice-commands Snapshots state-store flatMap: events map: snapshots invoice-snapshots Long retention (forever) Compacted

Slide 187

Slide 187 text

PROJECTOR

Slide 188

Slide 188 text

PROJECTOR snapshots stream invoice-snapshots

Slide 189

Slide 189 text

PROJECTOR map: records snapshots stream invoice-snapshots

Slide 190

Slide 190 text

PROJECTOR foreach: persist DB map: records snapshots stream invoice-snapshots

Slide 191

Slide 191 text

PROJECTOR foreach: persist DB invoice-records map: records snapshots stream invoice-snapshots

Slide 192

Slide 192 text

API DB Business Logic Persistance Service #2 Service #3 Service #4

Slide 193

Slide 193 text

No content

Slide 194

Slide 194 text

Invoices

Slide 195

Slide 195 text

Invoices Customers Orders Products

Slide 196

Slide 196 text

Invoices Inventory Customers Orders Products

Slide 197

Slide 197 text

Invoices Inventory Analytics Customers Orders Products

Slide 198

Slide 198 text

Stream processors Materialized views API Writes Reads

Slide 199

Slide 199 text

WINS

Slide 200

Slide 200 text

WINS • Simple and declarative system

Slide 201

Slide 201 text

WINS • Simple and declarative system • Atomicity - Kafka used as event-store + notification

Slide 202

Slide 202 text

WINS • Simple and declarative system • Atomicity - Kafka used as event-store + notification • Eventual consistency is handled gracefully

Slide 203

Slide 203 text

WINS • Simple and declarative system • Atomicity - Kafka used as event-store + notification • Eventual consistency is handled gracefully • Easy to add or change views

Slide 204

Slide 204 text

DEMO

Slide 205

Slide 205 text

TAKEAWAYS

Slide 206

Slide 206 text

TAKEAWAYS • Event driven systems and event sourcing can help create very flexible and scalable systems

Slide 207

Slide 207 text

TAKEAWAYS • Event driven systems and event sourcing can help create very flexible and scalable systems • Know your tradeoffs (consistency, schema evolution, debugging, error handling, …)

Slide 208

Slide 208 text

TAKEAWAYS • Event driven systems and event sourcing can help create very flexible and scalable systems • Know your tradeoffs (consistency, schema evolution, debugging, error handling, …) • Kafka & Kafka Streams are powerful tools that can be employed in many use cases

Slide 209

Slide 209 text

4USFBNPO

Slide 210

Slide 210 text

RESOURCES • Demo code:
 https://github.com/amitayh/event-sourcing-kafka-streams • Event sourcing by Greg Young -
 https://youtu.be/8JKjvY4etTY • Martin Kleppmann - Is Kafka a Database?
 https://youtu.be/v2RJQELoM6Y • Kafka Streams docs - http://wix.to/00C2ADs • Blog post from Confluent - http://wix.to/Z0C2ADs

Slide 211

Slide 211 text

Q&A