A presentation at Froscon php track 2018 in in 53757 Sankt Augustin, Germany by Tim Huijzers
My first year with event-sourcing Froscon 25-08-2018
Tim Huijzers @Dragem Developer @ drukwerkdeal.nl Founder of DeventerPHP Deventer NL
First Try ● ● ● ● Limited knowledge No experience No ES framework Doomed from the start
Second Try ● ● ● ● ● ● Limited knowledge Limited experience New Framework New DI manager New ES Framework Doomed To Fail
Third Try ● ● ● ● ● ● Some knowledge Some experience Known framework known database known ES framework Still doomed
BeerWarehouse
Why use Event Sourcing
CRUD
We will save a new entry in our system because we just bought it and will store it in the fridge for later.
If we change the location the system only knows about that location.
We drank it so it’s not in the system anymore
We want to keep a history of everything we drank.
I want to know when I drank this in my history.
But that’s only for new beers.
Events
Same Information as before + Explicit action about what happened
Make Small Events
Removed Location and changed name because in the real world you might not know this yet.
When returning home I put the beer in my fridge
I need room in my fridge so I take it out. Using the same Event
And at last a event about when I consumed it.
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.
“Every software program relates to some activity or interest of its user.” Eric Evans, Domain-Driven Design: Tackling Complexity in the Heart of Software
When To Use Event Sourcing
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 ● ● ● ●
When NOT To Use Event Sourcing
● ● ● ● 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
Event Sourcing in code
Prooph
http://getprooph.org/
Command
Command Handler
Aggregate
Event
Back to the Aggregate
Think About Side Effects
Structuring your application
Understanding the DB
How many beers do I have?
How many different styles do I have?
How many beers have I drank last 30 days?
Projection
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.
Pitfalls
Refactoring is harder, think about your architecture
Versioning ● ● ● Change an Event but support the old version Make a new Event Make the Event right from the start
Something wrong with the event
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
But what if I have like 100 trillion gazillion events?
Snapshots
You Do Not Need Snapshots From The start
Trigger on Event Count
Pure Event Sourcing Is Not A Holy Grail
Do Not Save Personal Data In Events
Make Projections For All You Lists
Try It In A Hackathon First
Most Of The Time Your DB Is Not Holy
What Now?
http://getprooph.org/
Source https://github.com/prooph/proophessor-do-symfony http://getprooph.org/
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
Thanks, Any Questions? Example code from talk on: https://github.com/webbaard/BeerWarehouse
Over the last couple of years, I have heard of Event sourcing but did't really know where to start until I did a tutorial at DPC '17. After having some basic information it was time to start a Hackathon and after that something production worthy . In this talk I will try to give the best information to get started and to know some of the problems you can face if you begin event-sourcing.