Slide 1

Slide 1 text

&WFOU4PVSDJOH XJUI,BGLB4USFBNT Amitay Horwitz

Slide 2

Slide 2 text

~$ whois • Software engineer @ Wix • Functional programming,
 distributed systems, TDD, … • • @amitayh on Twitter, GitHub, et al

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

• 120M < Users • 2,100 < Employees • 700 < Engineers • 600 < Micro-services in production

Slide 6

Slide 6 text

Israel !
 Tel Aviv
 Be'er Sheva Ukraine "
 Kiev
 Dnipro Lithuania #
 Vilnius

Slide 7

Slide 7 text

AGENDA ✅

Slide 8

Slide 8 text

AGENDA ✅ • Event sourcing 101

Slide 9

Slide 9 text

AGENDA ✅ • Event sourcing 101 • Eventim

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

BACKGROUND

Slide 13

Slide 13 text

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

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

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

EVENT SOURCING 101

Slide 24

Slide 24 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 line_item_id name rate OBJECT RELATIONAL IMPEDANCE MISMATCH

Slide 25

Slide 25 text

DOMAIN MODEL ≠ DB TABLE

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 =
 events.foldLeft(empty) { (state, event) =>
 state apply event
 }

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 ✏ User Interface

Slide 39

Slide 39 text

WRITE PATH ✏ User Interface Command JSON

Slide 40

Slide 40 text

WRITE PATH ✏ User Interface Command JSON Command DTO Decoder

Slide 41

Slide 41 text

WRITE PATH ✏ User Interface Command JSON Command DTO Decoder Validate command payload

Slide 42

Slide 42 text

WRITE PATH ✏ User Interface Command JSON Command DTO Decoder Domain Command Validate command payload

Slide 43

Slide 43 text

WRITE PATH ✏ User Interface Command JSON Command DTO Decoder Domain Command Handler #1 … Command Dispatcher Handler #2 Handler #n Validate command payload

Slide 44

Slide 44 text

WRITE PATH ✏ User Interface Command JSON Command DTO Decoder Domain Command Handler #1 … Command Dispatcher Handler #2 Handler #n Validate command payload PartialFunction[DomainCommand, Unit]

Slide 45

Slide 45 text

WRITE PATH ✏ User Interface Command JSON Command DTO Decoder Domain Command Handler #1 … Command Dispatcher Handler #2 Handler #n Validate command payload PartialFunction[DomainCommand, Unit] Function[DomainCommand, Unit]

Slide 46

Slide 46 text

WRITE PATH ✏ Event Sourced
 Command Handler

Slide 47

Slide 47 text

WRITE PATH ✏ Event Sourced
 Command Handler Command

Slide 48

Slide 48 text

WRITE PATH ✏ Event Sourced
 Command Handler Aggregare
 Repository Load Command

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

WRITE PATH ✏ Event Sourced
 Command Handler Aggregare
 Repository Load Event
 Store Get events Current
 aggregate Command

Slide 51

Slide 51 text

WRITE PATH ✏ Event Sourced
 Command Handler Aggregare
 Repository Load Event
 Store Get events Current
 aggregate Command
 events Command

Slide 52

Slide 52 text

WRITE PATH ✏ Event Sourced
 Command Handler Aggregare
 Repository Load Event
 Store Get events New Aggregate Current
 aggregate Command
 events Command

Slide 53

Slide 53 text

WRITE PATH ✏ Event Sourced
 Command Handler Aggregare
 Repository Load Event
 Store Get events New Aggregate Current
 aggregate Command
 events Publish events (optimistic locking) Command

Slide 54

Slide 54 text

WRITE PATH ✏ Event Sourced
 Command Handler Aggregare
 Repository Load Event
 Store Get events New Aggregate Current
 aggregate Command
 events Publish events (optimistic locking) Event Bus Command

Slide 55

Slide 55 text

READ PATH Event Bus

Slide 56

Slide 56 text

READ PATH Event Bus Events

Slide 57

Slide 57 text

READ PATH View Projector Event Handler Event Bus Events

Slide 58

Slide 58 text

READ PATH View Projector Event Handler DB Event Bus Events

Slide 59

Slide 59 text

READ PATH Mailer ✉ Event Handler View Projector Event Handler DB Event Bus Events

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

PAIN POINTS • Despite of the simple design, became quite a big library • Inherent eventual consistency is not integrated in the product (read after write) • Rebuilding views is a complex operation

Slide 64

Slide 64 text

REBUILDING VIEWS Event Bus Events

Slide 65

Slide 65 text

REBUILDING VIEWS View Projector #1 Event Handler DB #1 Event Bus Events

Slide 66

Slide 66 text

REBUILDING VIEWS View Projector #1 Event Handler DB #1 Event Bus Events View Projector #2 Event Handler DB #2

Slide 67

Slide 67 text

REBUILDING VIEWS View Projector #1 Event Handler DB #1 Event Bus Events View Projector #2 Event Handler DB #2 User Interface Queries

Slide 68

Slide 68 text

KAFKA & KAFKA STREAMS 101

Slide 69

Slide 69 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 70

Slide 70 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 71

Slide 71 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 72

Slide 72 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 73

Slide 73 text

KAFKA STREAMS

Slide 74

Slide 74 text

KAFKA STREAMS Streams

Slide 75

Slide 75 text

KAFKA STREAMS Streams Tables

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

STREAM-TABLE DUALITY

Slide 79

Slide 79 text

STREAM-TABLE DUALITY

Slide 80

Slide 80 text

STREAM-TABLE DUALITY Stream of all changes to a table

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

STREAM-TABLE DUALITY User Pageviews alice 1

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 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 87

Slide 87 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 88

Slide 88 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 89

Slide 89 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 90

Slide 90 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 91

Slide 91 text

STREAM TRANSFORMATIONS

Slide 92

Slide 92 text

STREAM TRANSFORMATIONS

Slide 93

Slide 93 text

STREAM TRANSFORMATIONS

Slide 94

Slide 94 text

STREAM TRANSFORMATIONS • map

Slide 95

Slide 95 text

STREAM TRANSFORMATIONS • map • filter

Slide 96

Slide 96 text

STREAM TRANSFORMATIONS • map • filter • flatMap

Slide 97

Slide 97 text

STREAM TRANSFORMATIONS • map • filter • flatMap • …

Slide 98

Slide 98 text

STREAM TRANSFORMATIONS • map • filter • flatMap • …

Slide 99

Slide 99 text

STREAM TRANSFORMATIONS

Slide 100

Slide 100 text

STREAM TRANSFORMATIONS

Slide 101

Slide 101 text

STREAM TRANSFORMATIONS

Slide 102

Slide 102 text

STREAM TRANSFORMATIONS Stream-stream join

Slide 103

Slide 103 text

STREAM TRANSFORMATIONS

Slide 104

Slide 104 text

STREAM TRANSFORMATIONS

Slide 105

Slide 105 text

STREAM TRANSFORMATIONS

Slide 106

Slide 106 text

STREAM TRANSFORMATIONS Stream-table join

Slide 107

Slide 107 text

STREAM PROCESSING APP Streams API Your app

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

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

Slide 112

Slide 112 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 113

Slide 113 text

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

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

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

Slide 116

Slide 116 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 117

Slide 117 text

STREAMS DSL • Programmatically describe your topology

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

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

Slide 120

Slide 120 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 121

Slide 121 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 122

Slide 122 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 123

Slide 123 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 124

Slide 124 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 125

Slide 125 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 126

Slide 126 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 127

Slide 127 text

PUTTING EVERYTHING TOGETHER

Slide 128

Slide 128 text

HIGH LEVEL DESIGN User Interface

Slide 129

Slide 129 text

HIGH LEVEL DESIGN User Interface

Slide 130

Slide 130 text

HIGH LEVEL DESIGN User Interface

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

HIGH LEVEL DESIGN User Interface invoice-commands

Slide 133

Slide 133 text

HIGH LEVEL DESIGN User Interface Command Handler

Slide 134

Slide 134 text

HIGH LEVEL DESIGN User Interface Command Handler Stream processing app

Slide 135

Slide 135 text

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

Slide 136

Slide 136 text

HIGH LEVEL DESIGN User Interface Projector Command Handler

Slide 137

Slide 137 text

HIGH LEVEL DESIGN User Interface Projector Projection DB Command Handler

Slide 138

Slide 138 text

HIGH LEVEL DESIGN User Interface Projector Projection DB Command Handler

Slide 139

Slide 139 text

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

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

COMMAND HANDLER

Slide 142

Slide 142 text

COMMAND HANDLER commands stream invoice-commands

Slide 143

Slide 143 text

COMMAND HANDLER map: results commands stream invoice-commands

Slide 144

Slide 144 text

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

Slide 145

Slide 145 text

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

Slide 146

Slide 146 text

COMMAND HANDLER command-results flatMap: events map: results commands stream invoice-commands Snapshots state-store

Slide 147

Slide 147 text

COMMAND HANDLER command-results invoice-events flatMap: events map: results commands stream invoice-commands Snapshots state-store

Slide 148

Slide 148 text

COMMAND HANDLER command-results invoice-events flatMap: events map: results commands stream invoice-commands Snapshots state-store events stream

Slide 149

Slide 149 text

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

Slide 150

Slide 150 text

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

Slide 151

Slide 151 text

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

Slide 152

Slide 152 text

COMMAND HANDLER val builder = new StreamsBuilder val snapshots: KTable[UUID, Snapshot[Invoice]] = builder .stream("invoice-events") .groupByKey() .aggregate( reducer.initializer, reducer.aggregator, Materialized.as("snapshots-store"))

Slide 153

Slide 153 text

PROJECTOR

Slide 154

Slide 154 text

PROJECTOR snapshots stream invoice-snapshots

Slide 155

Slide 155 text

PROJECTOR map: records snapshots stream invoice-snapshots

Slide 156

Slide 156 text

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

Slide 157

Slide 157 text

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

Slide 158

Slide 158 text

No content

Slide 159

Slide 159 text

Invoices

Slide 160

Slide 160 text

Invoices Customers Orders Products

Slide 161

Slide 161 text

Invoices Inventory Customers Orders Products

Slide 162

Slide 162 text

Invoices Inventory Analytics Customers Orders Products

Slide 163

Slide 163 text

WINS

Slide 164

Slide 164 text

WINS • Simple and declarative system

Slide 165

Slide 165 text

WINS • Simple and declarative system • Eventual consistency is handled gracefully

Slide 166

Slide 166 text

WINS • Simple and declarative system • Eventual consistency is handled gracefully • Easy to add or change views

Slide 167

Slide 167 text

WINS • Simple and declarative system • Eventual consistency is handled gracefully • Easy to add or change views • Benefits of event sourcing + scalability and fault- tolerance properties of Kafka

Slide 168

Slide 168 text

DEMO

Slide 169

Slide 169 text

TAKEAWAYS

Slide 170

Slide 170 text

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

Slide 171

Slide 171 text

TAKEAWAYS • Event driven systems and event sourcing can help create very flexible and scaleable systems • Know your tradeoffs (consistency guarantees,
 schema evolution, data integrity, …)

Slide 172

Slide 172 text

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

Slide 173

Slide 173 text

4USFBNPO

Slide 174

Slide 174 text

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

Slide 175

Slide 175 text

Q&A