Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

3 days of afterparty

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Hello Wroclaw.rb

Slide 7

Slide 7 text

Fist time in Poland

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

What I know now

Slide 11

Slide 11 text

Pierogi

Slide 12

Slide 12 text

Pelmeni -> Pierogi z mięsem Varenyky -> Pierogi ruskie

Slide 13

Slide 13 text

Best state machine implementation

Slide 14

Slide 14 text

class State def method_missing(method, *args, &block) eval(method.to_s.capitalize).send(:new) end end class Created < State; end class Open < State; end class Closed < State; end

Slide 15

Slide 15 text

Event Sourcing is popular

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Memes is not

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Autoload is still

Slide 20

Slide 20 text

Awesome people and community

Slide 21

Slide 21 text

Anton Davydov github.com/davydovanton
 twitter.com/anton_davydov davydovanton.com

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

• Software developer at Hippo • OpenSource evangelist • Hanami core developer • Try to make own product • Technical consulting

Slide 24

Slide 24 text

Stickers

Slide 25

Slide 25 text

• Coffee • Beer • Psychology • SW-2039-0208-4228 • How to draw images for presentations • Bad stories

Slide 26

Slide 26 text

• Coffee • Beer • Psychology • SW-2039-0208-4228 • How to draw images for presentations • Bad stories

Slide 27

Slide 27 text

Tried to be writer and make a story

Slide 28

Slide 28 text

expectations

Slide 29

Slide 29 text

reality

Slide 30

Slide 30 text

But I got help from my friend

Slide 31

Slide 31 text

Meet George

Slide 32

Slide 32 text

He is quiet and love ask questions

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Events

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Git

Slide 40

Slide 40 text

Each commit - something which was happened

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Each commit contain description and changes (payload)

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

What happened

Slide 45

Slide 45 text

Payload

Slide 46

Slide 46 text

List of cart items which user deleted and added

Slide 47

Slide 47 text

Activity audit

Slide 48

Slide 48 text

Don’t delete deleted data

Slide 49

Slide 49 text

deleted_at

Slide 50

Slide 50 text

But usually we’re working with current state of application

Slide 51

Slide 51 text

actions -> events data in DB -> current state

Slide 52

Slide 52 text

actions -> events data in DB -> current state

Slide 53

Slide 53 text

Knok knok ▼ George

Slide 54

Slide 54 text

… ▼ George

Slide 55

Slide 55 text

What will happen if we change it? ▼ George

Slide 56

Slide 56 text

Store events ▼ George

Slide 57

Slide 57 text

And calculate current state when we need it ▼ George

Slide 58

Slide 58 text

Event Sourcing

Slide 59

Slide 59 text

Store what was happened instead current state

Slide 60

Slide 60 text

And get state when you need

Slide 61

Slide 61 text

Where to store?

Slide 62

Slide 62 text

Event storage

Slide 63

Slide 63 text

Can be anything

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Rules

Slide 66

Slide 66 text

Event was happened

Slide 67

Slide 67 text

Immutable events

Slide 68

Slide 68 text

Can’t delete or update event

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

Only valid data in the events

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Data evolution

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Stop ▼ George

Slide 85

Slide 85 text

We can break the app! ▼ George

Slide 86

Slide 86 text

>_< ▼ George

Slide 87

Slide 87 text

Pro tips: optional fields, binary data structures

Slide 88

Slide 88 text

more in the book

Slide 89

Slide 89 text

How to get state from event store?

Slide 90

Slide 90 text

We need to calculate it using current event list

Slide 91

Slide 91 text

Projections

Slide 92

Slide 92 text

project(projection, events, init_state) -> state

Slide 93

Slide 93 text

project(projection, events, init_state) -> state same as a block in ruby’s enumerator

Slide 94

Slide 94 text

project(projection, events, init_state) -> state pure function

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

We’re compile current state for all events each time

Slide 108

Slide 108 text

… ▼ George

Slide 109

Slide 109 text

We can’t store all data for last 10 years ▼ George

Slide 110

Slide 110 text

And will have a performance issues for getting current state ▼ George

Slide 111

Slide 111 text

How to fix it?

Slide 112

Slide 112 text

Streams!

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

Event bus#1 Event bus#1 Event bus#2 Event bus#1 Event bus#2 Event bus#2

Slide 115

Slide 115 text

Event stream: bus#1 Event stream: bus#1 Event stream: bus#2 Event stream: bus#1 Event stream: bus#2 Event stream: bus#2

Slide 116

Slide 116 text

Stream: bus#1
 Event stream: bus#1 Event stream: bus#1 Event stream: bus#1

Slide 117

Slide 117 text

Stream: bus#2
 Event stream: bus#2 Event stream: bus#2 Event stream: bus#2

Slide 118

Slide 118 text

Pros: less events for processing

Slide 119

Slide 119 text

Cons: one more abstraction

Slide 120

Slide 120 text

Add snapshot for the current data state!

Slide 121

Slide 121 text

project(function, events, state) -> state

Slide 122

Slide 122 text

project(function, events, state) -> state {}

Slide 123

Slide 123 text

project(function, events, state) -> state { users: […] }

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

You can use any DB as a cache

Slide 131

Slide 131 text

No content

Slide 132

Slide 132 text

Idea: use more that one DB for cache

Slide 133

Slide 133 text

Relation DB for search and index page

Slide 134

Slide 134 text

Document oriented DB for show page

Slide 135

Slide 135 text

Real world

Slide 136

Slide 136 text

Where it can be useful

Slide 137

Slide 137 text

E-comerse systems

Slide 138

Slide 138 text

Order and checkout flow

Slide 139

Slide 139 text

Something where you need version control (wiki, docs, group editing)

Slide 140

Slide 140 text

Domain depends on events (tracking systems)

Slide 141

Slide 141 text

redux? ▼ George

Slide 142

Slide 142 text

Event sourcing and event driven architecture are different things

Slide 143

Slide 143 text

And they use events in different ways

Slide 144

Slide 144 text

ES -> event happened and I store it ED -> event happened and I say everyone it

Slide 145

Slide 145 text

Blockchain! ▼ George

Slide 146

Slide 146 text

pros

Slide 147

Slide 147 text

Easier to communicate with domain experts

Slide 148

Slide 148 text

• Logging out from box • Time traveling • Restore a state of system

Slide 149

Slide 149 text

• You don’t work with tables you work with events and changes • Experemental data structures • Easy to change database implementations

Slide 150

Slide 150 text

cons

Slide 151

Slide 151 text

• Hard for understand idea and complicated abstraction • Not popular in ruby • Developers need deprogramming

Slide 152

Slide 152 text

• Harder to get state than CRUD • Hard to understand the whole chain of events • Another architecture type
 with different DB structure

Slide 153

Slide 153 text

• Versions and versions compatibility • Updating or deleting events • Eventual Consistency

Slide 154

Slide 154 text

Advanced topics

Slide 155

Slide 155 text

How to display created data ASAP?

Slide 156

Slide 156 text

Solution 1

Slide 157

Slide 157 text

cheat user display real data

Slide 158

Slide 158 text

No content

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

No content

Slide 161

Slide 161 text

No content

Slide 162

Slide 162 text

No content

Slide 163

Slide 163 text

solution 2

Slide 164

Slide 164 text

WebSockets and Long Polling

Slide 165

Slide 165 text

How to make right events order?

Slide 166

Slide 166 text

No content

Slide 167

Slide 167 text

No content

Slide 168

Slide 168 text

No content

Slide 169

Slide 169 text

No content

Slide 170

Slide 170 text

distributed systems

Slide 171

Slide 171 text

more here

Slide 172

Slide 172 text

And here

Slide 173

Slide 173 text

How to delete user related data

Slide 174

Slide 174 text

Retroactive Event

Slide 175

Slide 175 text

No content

Slide 176

Slide 176 text

Lose encrypted data key

Slide 177

Slide 177 text

How to use ES in ruby

Slide 178

Slide 178 text

Simple way

Slide 179

Slide 179 text

gems

Slide 180

Slide 180 text

No content

Slide 181

Slide 181 text

github.com/zilverline/sequent

Slide 182

Slide 182 text

No content

Slide 183

Slide 183 text

eventide-project.org

Slide 184

Slide 184 text

No content

Slide 185

Slide 185 text

Brave way

Slide 186

Slide 186 text

Build all stuff by self

Slide 187

Slide 187 text

No content

Slide 188

Slide 188 text

No content

Slide 189

Slide 189 text

No content

Slide 190

Slide 190 text

hanami-events

Slide 191

Slide 191 text

Event driven transport layer with event versions and types

Slide 192

Slide 192 text

Connection without global state

Slide 193

Slide 193 text

Hanami::Events.new(:memory_sync) Hanami::Events.new(:memory_async) Hanami::Events.new(:redis)

Slide 194

Slide 194 text

Hanami::Events::Adapter.register(:google_pubsub) do Google::PubSub end Hanami::Events.new(:google_pubsub) # => event instance with your google pubsub adapter

Slide 195

Slide 195 text

events = Hanami::Events.new(:memory_sync) events.broadcast('user.created', user: user) Broadcaster

Slide 196

Slide 196 text

events.subscribe('user.created') do |payload| puts payload end events.subscribe('user.*') do |payload| puts payload end events.subscribe(/\Auser\..*/) do |payload| puts payload end Subscriber

Slide 197

Slide 197 text

Event sourcing looks cool. ▼ George

Slide 198

Slide 198 text

But I have legacy app. How can I migrate? ▼ George

Slide 199

Slide 199 text

User add something to cart

Slide 200

Slide 200 text

module Web::Controllers::CartItem class Create include Web::Action def call(params) halt(400) unless params.valid? if cart_repo.item_exist?(params[:item]) item = CartItemRepository.new.create_copy(params[:item]) else item = CartItemRepository.new.create(params[:item]) end analytics.call('Item Added') redirect_to routes.order_path(params[:order_id]) end end end

Slide 201

Slide 201 text

Step 1: Event Storming

Slide 202

Slide 202 text

Why? Detect important business events in the system

Slide 203

Slide 203 text

Orders::Events::ItemAdded Orders::Events::CoppyItemAdded

Slide 204

Slide 204 text

Step 2: store (apply) event

Slide 205

Slide 205 text

module Web::Controllers::CartItem class Create include Web::Action def call(params) halt(400) unless params.valid? event_store.apply(Order::Events::ItemAdded.new(payload)) if cart_repo.item_exist?(params[:item]) item = CartItemRepository.new.create_copy(params[:item]) else item = CartItemRepository.new.create(params[:item]) end analytics.call('Item Added') redirect_to routes.order_path(params[:order_id]) end end end

Slide 206

Slide 206 text

How to validate item?

Slide 207

Slide 207 text

module Web::Controllers::CartItem class Create include Web::Action def call(params) halt(400) unless params.valid? event_store.apply(Orders::Events::ItemAdded.new(payload)) if cart_repo.item_exist?(params[:item]) item = CartItemRepository.new.create_copy(params[:item]) else item = CartItemRepository.new.create(params[:item]) end analytics.call('Item Added') redirect_to routes.order_path(params[:order_id]) end end end

Slide 208

Slide 208 text

Step 3: calculate state

Slide 209

Slide 209 text

module Web::Controllers::CartItem class Create include Web::Action def call(params) halt(400) unless params.valid? events = event_store.get_stream(params[:order_id]) items = @project.call(Order::Projections::ItemList, events) if items.include?(params[:item]) event_store.apply(Orders::Events::ItemAdded.new(payload)) else event_store.apply(Orders::Events::CoppyItemAdded.new(payload)) end analytics.call('Item Added') redirect_to routes.order_path(params[:order_id]) end end end

Slide 210

Slide 210 text

module Web::Controllers::CartItem class Create include Web::Action def call(params) halt(400) unless params.valid? events = event_store.get_stream(params[:order_id]) items = @project.call(Order::Projections::ItemList, events, {}) if items.include?(params[:item]) event_store.apply(Orders::Events::ItemAdded.new(payload)) else event_store.apply(Orders::Events::CoppyItemAdded.new(payload)) end analytics.call('Item Added') redirect_to routes.order_path(params[:order_id]) end end end

Slide 211

Slide 211 text

module Projections class AllTask def call(event, state) case event when Orders::Events::ItemAdded state[:orders] ||= [] state[:orders] << event.payload when Orders::Events::ItemRemoved # ... end state end end end

Slide 212

Slide 212 text

module Projections class AllTask def call(event, state) case event when Orders::Events::ItemAdded state[:orders] ||= [] state[:orders] << event.payload when Orders::Events::ItemRemoved # ... end state end end end

Slide 213

Slide 213 text

module Projections class AllTask def call(event, state) case event when Orders::Events::ItemAdded state[:orders] ||= [] state[:orders] << event.payload when Orders::Events::ItemRemoved # ... end state end end end

Slide 214

Slide 214 text

module Web::Controllers::CartItem class Create include Web::Action def call(params) halt(400) unless params.valid? events = event_store.get_stream(params[:order_id]) items = @project.call(Order::Projections::ItemList, events, {}) if items.include?(params[:item]) event_store.apply(Orders::Events::ItemAdded.new(payload)) else event_store.apply(Orders::Events::CoppyItemAdded.new(payload)) end analytics.call('Item Added') redirect_to routes.order_path(params[:order_id]) end end end

Slide 215

Slide 215 text

Step 4: subscribers

Slide 216

Slide 216 text

# analytics subscribers event_store.subscribe(Orders::Events::ItemAdded) do |event| analytics.call('Item removed', event.payload) end event_store.subscribe(Orders::Events::CoppyItemAdded) do |event| analytics.call(‘Copy of item added’, event.payload) end event_store.subscribe(Orders::Events::ItemRemoved) do |event| analytics.call(‘Item removed', event.payload) end

Slide 217

Slide 217 text

module Web::Controllers::CartItem class Create include Web::Action def call(params) halt(400) unless params.valid? events = event_store.get_stream(params[:order_id]) items = @project.call(Order::Projections::ItemList, events, {}) if items.include?(params[:item]) event_store.apply(Orders::Events::ItemAdded.new(payload)) else event_store.apply(Orders::Events::CoppyItemAdded.new(payload)) end redirect_to routes.order_path(params[:order_id]) end end end

Slide 218

Slide 218 text

Step 4: move logic to domain

Slide 219

Slide 219 text

PROFIT

Slide 220

Slide 220 text

pro tip: you can use ES only as a part of your system

Slide 221

Slide 221 text

What next?

Slide 222

Slide 222 text

No content

Slide 223

Slide 223 text

No content

Slide 224

Slide 224 text

SAGA PATTERN

Slide 225

Slide 225 text

Long business transactions

Slide 226

Slide 226 text

No content

Slide 227

Slide 227 text

No content

Slide 228

Slide 228 text

CQRS
 
 (Command Query Responsibility Segregation)

Slide 229

Slide 229 text

– Martin Fowler “You can use a different model to update information than the model you use to read information”

Slide 230

Slide 230 text

Looking outside ruby world

Slide 231

Slide 231 text

github.com/heynickc/ awesome-ddd

Slide 232

Slide 232 text

github.com/davydovanton/event_sourcing_ruby

Slide 233

Slide 233 text

DDD

Slide 234

Slide 234 text

Again? ▼ George

Slide 235

Slide 235 text

Detect and store business events

Slide 236

Slide 236 text

Split system by domains

Slide 237

Slide 237 text

No content

Slide 238

Slide 238 text

No content

Slide 239

Slide 239 text

No content

Slide 240

Slide 240 text

Distributed systems

Slide 241

Slide 241 text

more here

Slide 242

Slide 242 text

Conclusions

Slide 243

Slide 243 text

Sometimes event base architecture win

Slide 244

Slide 244 text

Sometimes not

Slide 245

Slide 245 text

EventSourcing is simple as conception

Slide 246

Slide 246 text

But introduce a lot of practical problems
 (data evolution, event order, etc)

Slide 247

Slide 247 text

ES expensive

Slide 248

Slide 248 text

It’s not about distributed systems and communication across services

Slide 249

Slide 249 text

ES will be a trend in the future

Slide 250

Slide 250 text

Do you don’t trust?

Slide 251

Slide 251 text

ES will be a trend in the future

Slide 252

Slide 252 text

twitter.com/anton_davydov github.com/davydovanton [email protected] davydovanton.com Thank you ❤