Getting started with event sourcing in Laravel

Getting started with event sourcing in Laravel

A1b4b398630bbd32b77a82cbc37fae9a?s=128

Freek Van der Herten

March 27, 2019
Tweet

Transcript

  1. Getting started with event sourcing in Laravel

  2. About me Freek Van der Herten Partner & developer at

    Spatie @freekmurze https://murze.be https://ohdear.app
  3. Spatie Since 2003 Websites, applications and webshops Team 9 awesome

    human beings Specialisation Laravel, front-end development
  4. Open Source 
 Software

  5. We create a lot of it ± 200 packages on

    Packagist ± 2,000,000 downloads a month ± 30,000,000 total downloads All Postcardware!
  6. Highlights Popular laravel-backup laravel-medialibrary laravel-permission New laravel-query-builder laravel-blade-x

  7. https://spatie.be/open-source

  8. https://spatie.be/open-source/postcards

  9. Let's talk about event sourcing

  10. Talk overview Theory Laravel Event Projector demo Eventsauce demo

  11. Theory

  12. Traditional application Write data in database An update means overwriting

    the old data The old data cannot be accessed anymore
  13. APPLICATION DATABASE TABLE value X

  14. APPLICATION DATABASE TABLE value X value Y

  15. Event Sourced Application The application will fire off events Events

    will get written in a dedicated store Events are passed to consumers that create projections Aggregate, Aggregate Root...
  16. APP EVENTS Event Event 1

  17. APP EVENTS Event Event 1 Event 2

  18. APP EVENTS Event Event 1 Event 2 Event 3

  19. APP CONSUMER EVENTS PROJECTION Event Event 1 Event 2 Event

    3
  20. APP CONSUMER CONSUMER 2 EVENTS PROJECTION Event Event 1 Event

    2 Event 3 ANOTHER PROJECTION
  21. APP CONSUMER CONSUMER 2 EVENTS PROJECTION Event Event 1 Event

    2 Event 3 ANOTHER PROJECTION
  22. APP CONSUMER CONSUMER 2 EVENTS PROJECTION Event Event 1 Event

    2 Event 3 Event 4 ANOTHER PROJECTION
  23. Event Sourced Application Auditing requirements Extra reports needed Recording the

    unhappy path There is some setup required
  24. Laravel 
 Event Projector

  25. Laravel Event Projector Package made by Spatie It's not full

    event sourcing, it focuses on projections Beautifully integrated into Laravel Easy to get started with
  26. https://docs.spatie.be/laravel-event-projector

  27. Demo

  28. Eventsauce

  29. Eventsauce Package made by Frank De Jonge Focuses on aggregates

    Framework agnostic Separate Laravel bindings package available Very powerful, adds complexity
  30. https://eventsauce.io/docs/

  31. APP Subtract $1000

  32. AGGREGATE ROOT REPOSITORY UUID PAYLOAD TIME APP Subtract $1000

  33. AGGREGATE ROOT REPOSITORY UUID PAYLOAD TIME APP Subtract $1000 RETRIEVE

    Subtract $1000 AGGREGATE ROOT (empty)
  34. AGGREGATE ROOT REPOSITORY ABC-123 Subtract 1000 11:12 AM UUID PAYLOAD

    TIME APP Subtract $1000 RETRIEVE Subtract $1000 AGGREGATE ROOT ACCOUNTS CONSUMER -$1000 (empty) PERSIST
  35. AGGREGATE ROOT REPOSITORY ABC-123 Subtract 1000 11:12 AM UUID PAYLOAD

    TIME APP RETRIEVE Subtract $750 AGGREGATE ROOT ACCOUNTS -$1000 CONSUMER Subtract $750 Subtract $1000 PERSIST
  36. AGGREGATE ROOT REPOSITORY ABC-123 Subtract 1000 11:12 AM ABC-124 Subtract

    750 01:17 PM UUID PAYLOAD TIME APP RETRIEVE Subtract $750 AGGREGATE ROOT ACCOUNTS -$1750 CONSUMER Subtract $750 Subtract $1000 PERSIST
  37. AGGREGATE ROOT REPOSITORY ABC-123 Subtract 1000 11:12 AM ABC-124 Subtract

    750 01:17 PM UUID PAYLOAD TIME APP RETRIEVE AGGREGATE ROOT ACCOUNTS -$1750 CONSUMER Subtract $1250 Subtract $1000 Subtract $750 Subtract $1250 More Money Needed PERSIST
  38. AGGREGATE ROOT REPOSITORY ABC-123 Subtract 1000 11:12 AM ABC-124 Subtract

    750 01:17 PM ABC-125 Subtract 1250 05:51 PM ABC-126 More Money Needed 05:51 PM UUID PAYLOAD TIME ACCOUNTS APP CONSUMER CONSUMER 2 PROPOSED LOANS Subtract $1250 -$3000 RETRIEVE PERSIST Subtract $1000 Subtract $750 Subtract $1250 More Money Needed AGGREGATE ROOT
  39. Demo

  40. In closing

  41. Summary Laravel Event Projector Easy to use package to get

    started with projections Hooks into Laravel's native events Replay capabilities No aggregates
  42. Summary EventSauce Framework agnostic Support for aggregates, process modelling No

    replay capabilities out of the box https://github.com/spatie/laravel-eventsauce
  43. Writes are harder, reads are easier

  44. Event sourcing makes the easy things harder… …and the harder

    things easier — Frank De Jonge
  45. Larabank https://github.com/spatie/larabank-traditional https://github.com/spatie/larabank-event-projector https://github.com/spatie/larabank-eventsauce

  46. Resources https://kickstarter.engineering/event-sourcing-made-simple-4a2625113224 https://github.com/spatie/laravel-event-projector https://github.com/eventsaucephp/eventsauce https://github.com/prooph

  47. Thank you! https://speakerdeck.com/freekmurze/event-sourcing-full-stack-antwerp https://spatie.be/open-source https://murze.be https://ohdear.app