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

You attended talk: An introduction to event sourcing (short)

You attended talk: An introduction to event sourcing (short)

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.

carnage

May 13, 2016
Tweet

More Decks by carnage

Other Decks in Technology

Transcript

  1. You Attended Talk:
    An Introduction To Event Sourcing
    Christopher Riley
    PHP Day, May 2016
    1

    View full-size slide

  2. Introduction

    View full-size slide

  3. A familiar model
    2

    View full-size 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
    3

    View full-size 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
    4

    View full-size 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
    5

    View full-size 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
    6

    View full-size 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
    7

    View full-size 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
    8

    View full-size 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
    9

    View full-size 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
    10

    View full-size slide

  12. A familiar model (again)
    11

    View full-size slide

  13. Event sourcing

    View full-size slide

  14. Event sourcing example
    12

    View full-size slide

  15. Technical advantages
    • Debugging
    13

    View full-size slide

  16. Technical advantages
    • Debugging
    • Scalabilty
    14

    View full-size slide

  17. Technical advantages
    • Debugging
    • Scalabilty
    • Auditing
    15

    View full-size slide

  18. Technical advantages
    • Debugging
    • Scalabilty
    • Auditing
    • Testing
    16

    View full-size slide

  19. Example test
    17

    View full-size slide

  20. Retrieving data

    View full-size slide

  21. Performance Problems?

    View full-size slide

  22. MVC Application
    18

    View full-size slide

  23. CQRS Application
    19

    View full-size slide

  24. Technical advantages
    • Scalabilty
    20

    View full-size slide

  25. Technical advantages
    • Scalabilty
    • Performance
    21

    View full-size slide

  26. Technical advantages
    • Scalabilty
    • Performance
    • Multiple read models
    22

    View full-size slide

  27. CQRS + Event sourcing Application
    23

    View full-size slide

  28. Should I use Event sourcing?
    • Complex to implement
    24

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. Thanks
    • @giveupalready
    • https://github.com/carnage
    • http://joind.in/talk/1d28c
    29

    View full-size slide