My first year with Event-sourcing

A presentation at SymfonyCon Lisbon 2018 in December 2018 in Lisbon, Portugal by Tim Huijzers

Slide 1

Slide 1

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

Slide 2

Slide 2

Tim Huijzers Dragem or Webbaard Developer @ drukwerkdeal.nl Founder of DeventerPHP Usergroup

Slide 3

Slide 3

Slide 4

Slide 4

First Try ● ● ● ● Limited knowledge No experience No ES framework Doomed from the start

Slide 5

Slide 5

Second Try ● ● ● ● ● ● Limited knowledge Limited experience New Framework New DI manager New ES Framework Doomed To Fail

Slide 6

Slide 6

Third Try ● ● ● ● ● ● Some knowledge Some experience Known framework known database known ES framework Still doomed

Slide 7

Slide 7

Slide 8

Slide 8

Slide 9

Slide 9

BeerWarehouse

Slide 10

Slide 10

Why use Event Sourcing

Slide 11

Slide 11

CRUD

Slide 12

Slide 12

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

Slide 13

Slide 13

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

Slide 14

Slide 14

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

Slide 15

Slide 15

We want to keep a history of everything we drank.

Slide 16

Slide 16

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

Slide 17

Slide 17

But that’s only for new beers.

Slide 18

Slide 18

Slide 19

Slide 19

Events

Slide 20

Slide 20

Same Information as before + Explicit action about what happened

Slide 21

Slide 21

Make Small Events

Slide 22

Slide 22

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

Slide 23

Slide 23

When returning home I put the beer in my fridge

Slide 24

Slide 24

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

Slide 25

Slide 25

And at last a event about when I consumed it.

Slide 26

Slide 26

Slide 27

Slide 27

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.

Slide 28

Slide 28

“Every software program relates to some activity or interest of its user.” Eric Evans, Domain-Driven Design: Tackling Complexity in the Heart of Software

Slide 29

Slide 29

Slide 30

Slide 30

When To Use Event Sourcing

Slide 31

Slide 31

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 ● ● ● ●

Slide 32

Slide 32

When NOT To Use Event Sourcing

Slide 33

Slide 33

● ● ● ● 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

Slide 34

Slide 34

Event Sourcing in code

Slide 35

Slide 35

Prooph

Slide 36

Slide 36

http://getprooph.org/

Slide 37

Slide 37

Slide 38

Slide 38

Command

Slide 39

Slide 39

Slide 40

Slide 40

Slide 41

Slide 41

Slide 42

Slide 42

Slide 43

Slide 43

Command Handler

Slide 44

Slide 44

Slide 45

Slide 45

Slide 46

Slide 46

Aggregate

Slide 47

Slide 47

Slide 48

Slide 48

Event

Slide 49

Slide 49

Slide 50

Slide 50

Slide 51

Slide 51

Slide 52

Slide 52

Slide 53

Slide 53

Back to the Aggregate

Slide 54

Slide 54

Slide 55

Slide 55

Slide 56

Slide 56

Slide 57

Slide 57

Slide 58

Slide 58

Think About Side Effects

Slide 59

Slide 59

Slide 60

Slide 60

Slide 61

Slide 61

Slide 62

Slide 62

What about Symfony?

Slide 63

Slide 63

Slide 64

Slide 64

Slide 65

Slide 65

Structuring your application

Slide 66

Slide 66

Slide 67

Slide 67

Understanding the DB

Slide 68

Slide 68

Slide 69

Slide 69

How many beers do I have?

Slide 70

Slide 70

How many different styles do I have?

Slide 71

Slide 71

How many beers have I drank last 30 days?

Slide 72

Slide 72

Projection

Slide 73

Slide 73

A Projection allows you to loop through all event (past and present) and build your own views. ● Read Model ○ ● Projection ○ ● Define the data you would like to use. Loops through the events and applies that data to your view Finder ○ Helps you find data from that view.

Slide 74

Slide 74

Slide 75

Slide 75

Slide 76

Slide 76

Slide 77

Slide 77

Slide 78

Slide 78

Pitfalls

Slide 79

Slide 79

Refactoring is harder, think about your architecture

Slide 80

Slide 80

Slide 81

Slide 81

Versioning ● ● ● Change an Event but support the old version Make a new Event Make the Event right from the start

Slide 82

Slide 82

Something wrong with the event

Slide 83

Slide 83

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

Slide 84

Slide 84

But what if I have like 100 trillion gazillion events?

Slide 85

Slide 85

Slide 86

Slide 86

Snapshots

Slide 87

Slide 87

You Do Not Need Snapshots From The start

Slide 88

Slide 88

Trigger on Event Count

Slide 89

Slide 89

Pure Event Sourcing Is Not A Holy Grail

Slide 90

Slide 90

Do Not Save Personal Data In Events

Slide 91

Slide 91

Make Projections For All You Lists

Slide 92

Slide 92

Try It In A Hackathon First

Slide 93

Slide 93

Most Of The Time Your DB Is Not Holy

Slide 94

Slide 94

What Now?

Slide 95

Slide 95

http://getprooph.org/

Slide 96

Slide 96

Slide 97

Slide 97

Source https://github.com/prooph/proophessor-do-symfony http://getprooph.org/

Slide 98

Slide 98

Other Tools ● Broadway ○ ○ ○ ● Axon ○ ○ ○ ● No Upcaster, No Snapshots, No Replaying 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

Slide 99

Slide 99

Slide 100

Slide 100

Thanks, Any Questions? Slides of this talk on cfp.owncloud.com: https://cfp.owncloud.com/occon18/talk/XFB9PF/ Example code from talk on: https://github.com/webbaard/BeerWarehouse

Slide 101

Slide 101