my first year with event-sourcing · event-sourcing i know what beer i have. i know when it was...

101
My first year with event-sourcing And a little bit about tracking your beer Tim Huijzers

Upload: others

Post on 23-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

My first year with event-sourcingAnd a little bit about tracking your beer

Tim Huijzers

Page 2: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Tim HuijzersDragem or Webbaard

Developer @ drukwerkdeal.nlFounder of DeventerPHP

Usergroup

Page 3: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 4: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

First Try● Limited knowledge

● No experience

● No ES framework

● Doomed from the start

Page 5: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Second Try● Limited knowledge

● Limited experience

● New Framework

● New DI manager

● New ES Framework

● Doomed To Fail

Page 6: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Third Try● Some knowledge

● Some experience

● Known framework

● known database

● known ES framework

● Still doomed

Page 7: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 8: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 9: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

BeerWarehouse

Page 10: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Why use Event Sourcing

Page 11: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

CRUD

Page 12: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

We will save a new entry in our system because we just bought it and will store it in the fridge for later.

Page 13: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

If we change the location the system only knows about that location.

Page 14: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

We drank it so it’s not in the system anymore

Page 15: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

We want to keep a history of everything we drank.

Page 16: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

I want to know when I drank this in my history.

Page 17: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

But that’s only for new beers.

Page 18: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 19: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Events

Page 20: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Same Information as before + Explicit action about what happened

Page 21: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Make Small Events

Page 22: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Removed Location and changed name because in the real world you might not know this yet.

Page 23: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

When returning home I put the beer in my fridge

Page 24: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

I need room in my fridge so I take it out. Using the same Event

Page 25: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

And at last a event about when I consumed it.

Page 26: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 27: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Crud● I know what beer I have.

● I know when it was consumed.

● I know where it is.

Event-Sourcing● I know what beer I have.

● I know when it was consumed.

● I know where it is.

● I know where it was before.

● I know when it was moved.

● I know where it was at any point in

time

● I know how many times it was moved.

● I know when it was added to the

system.

● I know what else was moved in that

day.

Page 28: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

“Every software program relates to some activity or interest of its user.”

Eric Evans, Domain-Driven Design: Tackling Complexity in the Heart of Software

Page 29: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 30: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

When To Use Event Sourcing

Page 31: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

● You need an audit log

● You like scalability

● You want to separate the read and write of an application

● You want to replay event on a dev machine to get an accurate

situation of what the state was at a point in time.

● You want reporting but don’t know what yet.

● You are done with mapping objects to tables

Page 32: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

When NOT To Use Event Sourcing

Page 33: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

● You only need a simple CRUD system.

● You are processing a lot of personal data.

● You just want to query a lot of things on the DB

● You are starting on a big project for production

Page 34: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Event Sourcing in code

Page 35: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Prooph

Page 36: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

http://getprooph.org/

Page 37: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 38: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Command

Page 39: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 40: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 41: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 42: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 43: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Command Handler

Page 44: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 45: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 46: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Aggregate

Page 47: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 48: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Event

Page 49: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 50: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 51: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 52: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 53: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Back to the Aggregate

Page 54: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 55: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 56: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 57: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 58: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Think About Side Effects

Page 59: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 60: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 61: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 62: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

What about Symfony?

Page 63: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 64: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 65: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Structuring your application

Page 66: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 67: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Understanding the DB

Page 68: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 69: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

How many beers do I have?

Page 70: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

How many different styles do I have?

Page 71: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

How many beers have I drank last 30 days?

Page 72: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Projection

Page 73: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

A Projection allows you to loop through all event (past and present) and build your

own views.

● Read Model

○ Define the data you would like to use.

● Projection

○ Loops through the events and applies that data to your view

● Finder

○ Helps you find data from that view.

Page 74: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 75: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 76: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 77: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 78: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Pitfalls

Page 79: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Refactoring is harder, think about your architecture

Page 80: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 81: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Versioning● Change an Event but support the old version

● Make a new Event

● Make the Event right from the start

Page 82: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Something wrong with the event

Page 83: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Event are immutable, So don’t change them● Try solving it another way first.

● Correct errors with new events

● Try a upcaster

● Make a new stream and fill it with mutated events (and test)

● Change the events in the database

Page 84: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

But what if I have like 100 trillion gazillion events?

Page 85: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 86: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Snapshots

Page 87: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

You Do Not Need Snapshots From The start

Page 88: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Trigger on Event Count

Page 89: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Pure Event Sourcing Is Not A Holy Grail

Page 90: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Do Not Save Personal Data In Events

Page 91: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Make Projections For All You Lists

Page 92: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Try It In A Hackathon First

Page 93: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Most Of The Time Your DB Is Not Holy

Page 94: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

What Now?

Page 95: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

http://getprooph.org/

Page 96: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 97: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Sourcehttps://github.com/prooph/proophessor-do-symfony

http://getprooph.org/

Page 98: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Other Tools● Broadway

○ No Upcaster,

○ No Snapshots,

○ No Replaying

● Axon

○ Upcasting by MessageFactory,

○ Snapshots by Trigger on event count,

○ Replaying by Example code for replay

● Akka

○ Upcasting by Event Adapter,

○ Snapshots decided by actor,

○ Replaying

Page 99: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved
Page 100: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved

Thanks, Any Questions?

Example code from talk on: https://github.com/webbaard/BeerWarehouse

Slides of this talk on cfp.owncloud.com: https://cfp.owncloud.com/occon18/talk/XFB9PF/

Page 101: My first year with event-sourcing · Event-Sourcing I know what beer I have. I know when it was consumed. I know where it is. I know where it was before. I know when it was moved