Upgrade to Pro — share decks privately, control downloads, hide ads and more …

You Attended Talk: an introduction to event sourcing

carnage
October 03, 2015

You Attended Talk: an introduction to event sourcing

Imagine for a moment you work for a large online retailer specialising in household goods. One morning, the head of marketing comes to you and says "I've had this great idea we're going to send discount vouchers to anyone who's changed their address in the past 3 months; people who've recently moved are more likely to be buying new furniture. Could you retrieve a list of all these customers?" You explain to him your systems only store a customers current address and doesn't record when it was last changed, a new feature is added to the backlog and the head of marketing leaves a little disappointed.

What if you could build a system which was able to answer this kind of question without knowing it up front? A possible solution is to use event sourcing, being able to go back to any previous state of your data is just one advantage of using event sourcing in your application. Event sourcing is a total paradigm shift from the more traditional model of storing an application's current state and can appear to be a very unnatural way to think about and build a system. In this talk I'm going to show you examples of why event sourcing can be a superior model and how to go about building an event sourced system in PHP.

Example code: https://github.com/carnage/broadway-bowling

carnage

October 03, 2015
Tweet

More Decks by carnage

Other Decks in Programming

Transcript

  1. You Attended Talk:
    An Introduction To Event Sourcing
    Christopher Riley
    PHP North West, 2015
    1

    View Slide

  2. Introduction

    View Slide

  3. A familiar model
    3

    View Slide

  4. Deleting data
    LineItem table
    id order id product code quantity
    1 1 PK00001 3
    2 1 PK00002 1
    3 2 PK00002 1
    4 3 PK00007 4
    5 4 PK00003 2
    6 4 PK00005 6
    4

    View Slide

  5. Deleting data
    LineItem table
    id order id product code quantity
    1 1 PK00001 3
    3 2 PK00002 1
    4 3 PK00007 4
    5 4 PK00003 2
    6 4 PK00005 6
    5

    View Slide

  6. Deleting data
    LineItem table
    id order id product code quantity deleted
    1 1 PK00001 3 0
    2 1 PK00002 1 0
    3 2 PK00002 1 0
    4 3 PK00007 4 0
    5 4 PK00003 2 0
    6 4 PK00005 6 0
    6

    View Slide

  7. Deleting data
    LineItem table
    id order id product code quantity deleted
    1 1 PK00001 3 0
    2 1 PK00002 1 1
    3 2 PK00002 1 0
    4 3 PK00007 4 0
    5 4 PK00003 2 0
    6 4 PK00005 6 0
    7

    View Slide

  8. Editing data
    LineItem table
    id order id product code quantity deleted
    1 1 PK00001 3 0
    2 1 PK00002 1 1
    3 2 PK00002 1 0
    4 3 PK00007 4 0
    5 4 PK00003 2 0
    6 4 PK00005 6 0
    8

    View Slide

  9. Editing data
    LineItem table
    id order id product code quantity deleted
    1 1 PK00001 2 0
    2 1 PK00002 1 1
    3 2 PK00002 1 0
    4 3 PK00007 4 0
    5 4 PK00003 2 0
    6 4 PK00005 6 0
    9

    View Slide

  10. Editing data
    LineItem table
    id order id product code quantity deleted version
    1 1 PK00001 3 0 1
    2 1 PK00002 1 1 1
    3 2 PK00002 1 0 1
    4 3 PK00007 4 0 1
    5 4 PK00003 2 0 1
    6 4 PK00005 6 0 1
    10

    View Slide

  11. Editing data
    LineItem table
    id order id product code quantity deleted version
    1 1 PK00001 3 0 1
    2 1 PK00002 1 1 1
    3 2 PK00002 1 0 1
    4 3 PK00007 4 0 1
    5 4 PK00003 2 0 1
    6 4 PK00005 6 0 1
    1 1 PK00001 2 0 2
    11

    View Slide

  12. A familiar model (again)
    12

    View Slide

  13. Event sourcing

    View Slide

  14. Event sourcing example
    14

    View Slide

  15. Advantages

    View Slide

  16. Another Example

    View Slide

  17. Technical advantages
    • Debugging
    17

    View Slide

  18. Technical advantages
    • Debugging
    • Scalabilty
    18

    View Slide

  19. Technical advantages
    • Debugging
    • Scalabilty
    • Auditing
    19

    View Slide

  20. Summary

    View Slide

  21. Retrieving data

    View Slide

  22. Performance Problems?

    View Slide

  23. CQRS

    View Slide

  24. MVC Application
    24

    View Slide

  25. CQRS Application
    25

    View Slide

  26. Technical advantages
    • Scalabilty
    26

    View Slide

  27. Technical advantages
    • Scalabilty
    • Performance
    27

    View Slide

  28. Technical advantages
    • Scalabilty
    • Performance
    • Multiple read models
    28

    View Slide

  29. CQRS + Event sourcing Application
    29

    View Slide

  30. Code - Stage 1

    View Slide

  31. Bowling Game Entity
    31

    View Slide

  32. Game Started Event
    32

    View Slide

  33. Bowling Game Entity
    33

    View Slide

  34. Command Handler
    34

    View Slide

  35. Code - Stage 2

    View Slide

  36. Command Handler
    36

    View Slide

  37. Validation of Throw
    37

    View Slide

  38. Handling Throw Score Recorded Event
    38

    View Slide

  39. Code - Stage 3

    View Slide

  40. Event Sourced Repository
    40

    View Slide

  41. Projection
    41

    View Slide

  42. Read Model
    42

    View Slide

  43. Code - Stage 4

    View Slide

  44. Projection 2
    44

    View Slide

  45. Should I use Event sourcing?
    • Complex to implement
    45

    View Slide

  46. Should I use Event sourcing?
    • Complex to implement
    • Development overhead
    46

    View Slide

  47. Should I use Event sourcing?
    • Complex to implement
    • Development overhead
    • Less understanding
    47

    View Slide

  48. Should I use Event sourcing?
    • Complex to implement
    • Development overhead
    • Less understanding
    • Hard to retofit
    48

    View Slide

  49. Conclusions

    View Slide

  50. Further reading
    • Greg Young
    • https://github.com/qandidate-labs/broadway
    • https://github.com/carnage/broadway-bowling
    50

    View Slide

  51. Thanks
    • https://joind.in/talk/view/15440
    • @giveupalready
    • https://github.com/carnage
    51

    View Slide