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

An In-Depth Look at Event Sourcing with CQRS

An In-Depth Look at Event Sourcing with CQRS

Event Sourcing is an approach to building software with a long track record of success. By placing business concepts at the heart of our code, we can decouple systems into small services that can be quickly built, changed, and replaced.

Although Event Sourcing has been around for many years, it remains outside the mainstream paradigm of software development--much to our detriment.

By placing the highest value on the simple capture of essential business events without attempting to interpret them, and by relegating all interpretations of those events to completely disposable subsystems, Event Sourcing enables us to move faster by supporting rapid experimentation with new perspectives, new user interactions, and new insights into your business. Without needing to predict the future, we are in a much better position to respond to it when it arrives.

Event Sourcing is agnostic of technology stack and language style, but it goes well with another pattern called CQRS: Command Query Responsibility Segregation. In this talk, we will do a deep-dive into the two patterns and scope out a system that has:

* Receiving and validating Commands that, if successful, result in Events.
* Interpreting Events into denormalised projections for very fast reads.
* Single responsibility services for reacting to Events by creating other events and, if necessary, triggering external behaviour.

I gave this talk at SATURN on May 3, 2017. Video can be found here: https://www.youtube.com/watch?v=EqpalkqJD8M

Sebastian von Conrad

May 03, 2017
Tweet

More Decks by Sebastian von Conrad

Other Decks in Technology

Transcript

  1. SATURN 2017 Title of the Presentation Goes Here © 2017

    [Copyright Owner[s]] An In-Depth Look at Event Sourcing with CQRS © 2017 Sebastian von Conrad 1 An In-Depth Look at Event Sourcing with CQRS Sebastian von Conrad @vonconrad
  2. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 3 Agenda ‣ Event Sourcing ‣ Event Sourcing with CQRS ‣ With a lil’ dose of DDD ‣ Upside ‣ Downside ‣ It’s not all sunshine and rainbows
  3. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 5 Event Sourcing at Envato
  4. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 5 Event Sourcing at Envato ‣ First generation: 1 system, >3 years in production.
  5. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 5 Event Sourcing at Envato ‣ First generation: 1 system, >3 years in production. ‣ Extracted framework.
  6. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 5 Event Sourcing at Envato ‣ First generation: 1 system, >3 years in production. ‣ Extracted framework. ‣ Second generation: Half-a-dozen systems, in production from ~1 year to now.
  7. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 5 Event Sourcing at Envato ‣ First generation: 1 system, >3 years in production. ‣ Extracted framework. ‣ Second generation: Half-a-dozen systems, in production from ~1 year to now. ‣ Used framework from first generation.
  8. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 5 Event Sourcing at Envato ‣ First generation: 1 system, >3 years in production. ‣ Extracted framework. ‣ Second generation: Half-a-dozen systems, in production from ~1 year to now. ‣ Used framework from first generation. ‣ Third generation: Being developed now.
  9. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 5 Event Sourcing at Envato ‣ First generation: 1 system, >3 years in production. ‣ Extracted framework. ‣ Second generation: Half-a-dozen systems, in production from ~1 year to now. ‣ Used framework from first generation. ‣ Third generation: Being developed now. ‣ Expanding paradigm to new frameworks and languages, including FP.
  10. SATURN 2017 Title of the Presentation Goes Here © 2017

    [Copyright Owner[s]] An In-Depth Look at Event Sourcing with CQRS © 2017 Sebastian von Conrad 6 Event Sourcing
  11. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 7 What is Event Sourcing?
  12. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 8 Many systems log “events.”
  13. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 9 Event Logging ≠ Event Sourcing
  14. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 10 Most systems are built around a database that stores current state. An “event log” would sit to the side.
  15. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 11 Event Sourcing forces current state to be derived from all past history.
  16. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 12 With Event Sourcing, all current state is disposable.
  17. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 13 Current state is a cache. A cache can be deleted and regenerated.
  18. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 14 Greg Young
  19. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 15 Event Sourcing isn’t new.
  20. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 16 At least 500 years old. Allegedly tracked back to Ancient Mesopotamia.
  21. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 17 Most mature industries are event sourced.
  22. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 18 Finance, accounting, insurance, medical, legal, etc.
  23. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 19 Bank account.
  24. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 20 Do you think your bank account balance is simply a column in a current state database?
  25. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 21 What if you disagree with your bank?
  26. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 22 The balance is an equation.
  27. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 23 balance = sum(transaction.amount)
  28. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 24 You can manually calculate this.
  29. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 25 Event Sourcing works the same way.
  30. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 26 All current state is transient.
  31. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 27 And are the result of equations.
  32. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 28 Your source of truth are events, not state.
  33. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 29 …so what are events?
  34. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 30 Overloaded term.
  35. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 31 In this context, events describe things that have happened that are of interest to you.
  36. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 32 Events are business facts.
  37. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 33 Events are expressed in past tense.
  38. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 34 Bank domain events ‣ New Account Application Received ‣ Customer Identity Verified ‣ Account Opened ‣ Funds Deposited ‣ Funds Withdrawn ‣ Funds Transferred ‣ Account Statement Issued
  39. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 35 E-commerce Domain events ‣ Item Added To Shopping Cart ‣ Item Removed From Shopping Cart ‣ Order Placed ‣ Payment Received ‣ Goods Dispatched
  40. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 36 Calendar domain events ‣ Appointment Scheduled ‣ Appointment Rescheduled ‣ Invitation Extended ‣ Invitation Sent ‣ Invitation Accepted ‣ Invitation Declined ‣ Appointment Canceled
  41. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 37 Events are self-described.
  42. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 38 Events completely describe the thing that has happened.
  43. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 39 But they are not denormalised.
  44. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 40 Keep your events focused on only the data for which they are the authoritative source.
  45. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 41 “Order Placed” event ‣ A reference to the customer who placed the order ‣ A timestamp for the order placement from the customer's perspective ‣ A fully specified shipping address for the order ‣ A fully specified billing address for the order ‣ A list of the items ordered, each of which include: ‣ A reference to the type of item ‣ The quantity ordered ‣ The price to be charged ‣ Details of any taxes charged ‣ Details of discounts, coupons and other adjustments that were applied ‣ The total amount charged for the order ‣ A reference to the payment that covers the order
  46. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 42 With Event Sourcing, events are the centre of your domain. Modelling them well is crucial.
  47. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 43 One technique: Event Storming. (Go watch Paul Rayner’s talk.)
  48. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 44 Okay, so, let’s put this into practice.
  49. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 45 Shopping Cart modelled as “current state” Cart Shipping Address Line Items Line Items Line Items Could be a document, a table in a relational database, a document in a document database, or something else.
  50. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 46 We’ve been taught that the shape of this data is the most important thing.
  51. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 47 But it can also be expressed in events.
  52. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 48 Shopping Cart as Events Cart Emptied Shipping Address Provided Item Added To Cart Item Added To Cart
  53. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 49 Shopping Cart “current state” Cart Shipping Address Line Items Line Items Line Items
  54. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 50 Cart Emptied Shipping Address Provided Item Added To Cart Item Added To Cart What we end up storing, however, are the events.
  55. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 51 Event Sourcing just a different way of storing information.
  56. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 52 Though it has fundamental consequences.
  57. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 53 With Event Sourcing, data is append-only.
  58. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 54 With Event Sourcing, data is immutable.
  59. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 55 With Event Sourcing, data can be cached forever.
  60. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 56 So how do we deal with mistakes?
  61. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 57 Well, how does an accountant deal with mistakes?
  62. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 58 We call these correction events.
  63. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 60 Shopping Cart “correction” event Cart Emptied Shipping Address Provided Item Added To Cart Item Added To Cart Item Removed From Cart
  64. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 61 Are these the same thing? Cart Emptied Shipping Address Provided Item Added To Cart Item Added To Cart Item Removed From Cart Cart Emptied Shipping Address Provided Item Added To Cart
  65. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 62 The current state is the same! Cart Shipping Address Line Items Line Items
  66. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 63 Let me issue you a challenge.
  67. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 64 In your system: Two use cases that result in the same current state.
  68. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 65 If you can, you’re losing data.
  69. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 66 Follow-up question: How do you determine which data you’re happy to lose? (Can you teach me?)
  70. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 67 I have only one rule: never lose data. Because I don’t know how to value it.
  71. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 68 I can’t predict its future value.
  72. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 69 This is the main benefit of Event Sourcing.
  73. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 71 CEO: “Let’s sell people some things they were interested in but didn’t end up buying.”
  74. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 72 Adding to current state model Cart Shipping Address Line Items Line Items Line Items Line Items Line Items Removed Line Items
  75. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 73 Shopping Cart “correction” event Cart Emptied Shipping Address Provided Item Added To Cart Item Added To Cart Item Removed From Cart
  76. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 74 We can write a projection. (More on this later.)
  77. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 75 Starts from event 0. May take a while. That’s fine, it’s async.
  78. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 76 We’re not losing data. And we can come up with new and interesting ways to interpret it.
  79. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 77 We also get a time machine.
  80. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 78 Want to know what that report looked like at 2016-05-03 21:16:09 UTC? No problem.
  81. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 79 An event sourced system is deterministic.
  82. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 80 Easy to go back to previous state, e.g. when a bug was occurring.
  83. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 81 Sure, sure, that’s all well and good but it can’t possibly scale!
  84. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 82 What if I have a lot of events?
  85. SATURN 2017 Title of the Presentation Goes Here © 2017

    [Copyright Owner[s]] An In-Depth Look at Event Sourcing with CQRS © 2017 Sebastian von Conrad 83 Event Sourcing with CQRS
  86. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 84 Command Query Responsibility Segregation
  87. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 85 Effectively, separating 
 reads from writes.
  88. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 86 It’s possible to use Event Sourcing without CQRS.
  89. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 87 It’s possible to use CQRS without Event Sourcing.
  90. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 88 They just go really well together.
  91. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 89 Four major building blocks to the architecture ‣ Event Store ‣ Reading with Queries ‣ Writing with Commands ‣ Reactors
  92. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 90 Event Store.
  93. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 91 Events are kept in a database called the “Event Store.”
  94. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 92 The Event Store is usually considered a sub-system in its own right.
  95. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 93 There are commercial and open-source options available.
  96. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 94 But it’s easy enough to roll your own. (We have.)
  97. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 95 The simplest event store has a single event “stream.”
  98. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 96 The simplest Event Store has three features: ‣ Put event. ‣ Get all events, in order, from an offset. ‣ Get all events, in order, scoped by <something>.
  99. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 97 These are the features we’ll make use of.
  100. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 98 Reading with Queries.
  101. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 99 Clients need to read (and display) information.
  102. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 100 Clients are usually interested in current state.
  103. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 101 The Event Store is not suitable for presenting current state. Client Event Store x
  104. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 102 So we need something that Clients can query for current state.
  105. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 103 We call this a Query Handler.
  106. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 104 It’s a little detour. Client Event Store Query Handler
  107. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 105 The Query Handler “tails” the event stream from the Event Store. Event Store Query Handler
  108. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 106 …and builds up a projection. Event Store Query Handler
  109. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 107 When a Client wants to know something, the query handler use the projection. Client Query Handler
  110. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 108 And returns the data to the Client. Client Query Handler
  111. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 109 There are many (N) projections, each optimised for a particular “query.”
  112. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 110 The Query Handler will pick a projection for a query from a client. Client Query Handler
  113. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 111 Each of which has their own “tailing” process called a projector that read from the stream. Event Store
  114. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 112 This is not using the Observer pattern.
  115. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 113 We use the following Event Store feature(s): ‣ Put event. ‣Get all events, in order, from an offset. ‣ Get all events, in order, scoped by <something>.
  116. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 114 The projectors keep a “pointer” to where in the event stream they’re at. Event Store 51 56 55 49 57 55
  117. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 115 The projections are disposable and can be rebuilt by going back to 0. Event Store 0
  118. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 116 The projectors and projections are completely decoupled from each other.
  119. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 117 This allows different data storage options to solve different problems.
  120. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 118 Relational DB Document DB Search Engine Graph DB Cache Data Warehouse Flat files …
  121. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 119 We can optimise for how we want to read the data.
  122. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 120 “Current” current state read models tend to be normalised, which is optimised for writing data.
  123. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 121 Projections can be (and often are) denormalised.
  124. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 122 Projections are cheap.
  125. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 123 New interpretation? New projection! Event Store Query Handler
  126. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 124 When caught up, we can swap to reading from the new projection. Event Store Query Handler
  127. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 125 And then we can decommission the old projection. Event Store Query Handler x x
  128. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 126 No more database migrations on production tables.
  129. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 127 Summary: Reading with Queries
  130. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 127 Summary: Reading with Queries ‣ Clients never query the events directly.
  131. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 127 Summary: Reading with Queries ‣ Clients never query the events directly. ‣ Clients query denormalised projections that are optimised for querying.
  132. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 127 Summary: Reading with Queries ‣ Clients never query the events directly. ‣ Clients query denormalised projections that are optimised for querying. ‣ Projections are built with projectors that process the event stream.
  133. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 127 Summary: Reading with Queries ‣ Clients never query the events directly. ‣ Clients query denormalised projections that are optimised for querying. ‣ Projections are built with projectors that process the event stream. ‣ Projectors are decoupled from each other and don’t share any state.
  134. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 127 Summary: Reading with Queries ‣ Clients never query the events directly. ‣ Clients query denormalised projections that are optimised for querying. ‣ Projections are built with projectors that process the event stream. ‣ Projectors are decoupled from each other and don’t share any state. ‣ Projections are cheap and easy to build and rebuild.
  135. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 128 Writing with Commands.
  136. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 129 In addition to reading, Clients also want to write.
  137. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 130 We established that Clients should not query the Event Store directly.
  138. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 131 Nor should Clients write directly to the Event Store.
  139. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 132 The Event Store is not responsible for validation, and so giving Clients direct access is dangerous.
  140. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 133 It would be like opening your SQL database to the world.
  141. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 134 Instead, Clients express their intentions in the form of Commands.
  142. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 135 Commands represent intent.
  143. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 136 Just like we had something responsible for handling queries, we need something responsible for handling commands.
  144. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 137 It’s called a Command Handler. Client Event Store Command Handler
  145. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 138 Commands express intent.
  146. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 139 Event and command naming Events (past tense) ‣ Account Opened ‣ Funds Deposited ‣ Funds Withdrawn ‣ Funds Transferred ‣ Account Statement Issued
  147. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 139 Event and command naming Events (past tense) ‣ Account Opened ‣ Funds Deposited ‣ Funds Withdrawn ‣ Funds Transferred ‣ Account Statement Issued Commands (imperative) ‣ Open Account ‣ Deposit Funds ‣ Withdraw Funds ‣ Transfer Funds ‣ Issue Account Statement
  148. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 140 Commands represent intent and can be rejected. Events are facts and cannot.
  149. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 141 A Command that is accepted results in an event.
  150. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 142 So how does the Command Handler validate Commands?
  151. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 143 This is where we start borrowing from DDD.
  152. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 144 DDD doesn’t come into play on the Query side, but it does on the Command side.
  153. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 145 Events happen to “something.”
  154. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 146 We call that “something” an Aggregate. This is a DDD term.
  155. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 147 The Aggregates are the nouns in our system.
  156. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 148 Bank domain events and aggregates Events ‣ New Account Application Received ‣ Customer Identity Verified ‣ Account Opened ‣ Funds Deposited ‣ Funds Withdrawn ‣ Funds Transferred ‣ Account Statement Issued
  157. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 148 Bank domain events and aggregates Events ‣ New Account Application Received ‣ Customer Identity Verified ‣ Account Opened ‣ Funds Deposited ‣ Funds Withdrawn ‣ Funds Transferred ‣ Account Statement Issued Aggregates Account Application Customer Account Transfer Account Statement
  158. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 149 E-commerce domain aggregates ‣ Customer ‣ Shopping Cart ‣ Order ‣ Payment ‣ Product (or Item) ‣ Shipment
  159. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 150 Calendar domain events ‣ Calendar ‣ Appointment ‣ Invitation
  160. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 151 Aggregates have identity that we need to track over time. Often, they have lifecycles.
  161. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 152 Aggregates can refer to each other. E.g. Order => Customer.
  162. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 153 When a Command is received, it is executed against an Aggregate.
  163. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 154 The Aggregate determines whether to accept or reject the Command.
  164. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 155 To do that, the Aggregate needs to know its current state.
  165. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 156 events = EventStore.load(aggregate_id)
  166. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 157 events = EventStore.load(aggregate_id) aggregate = Aggregate.new
  167. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 158 events = EventStore.load(aggregate_id) aggregate = Aggregate.new aggregate.replay_events(events)
  168. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 159 events = EventStore.load(aggregate_id) aggregate = Aggregate.new aggregate.replay_events(events) aggregate.attempt_command(payload)
  169. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 160 events = EventStore.load(aggregate_id) aggregate = Aggregate.new aggregate.replay_events(events) aggregate.attempt_command(payload) # => true
  170. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 161 events = EventStore.load(aggregate_id) aggregate = Aggregate.new aggregate.replay_events(events) aggregate.attempt_command(payload) # => false
  171. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 162 Let’s say we want to withdraw some funds.
  172. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 163 command = WithdrawFundsCommand.new( account_id: “6504630d-6ceb-4650-b516-5e720d3db941”, amount: 500)
  173. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 164 command = WithdrawFundsCommand.new( account_id: “6504630d-6ceb-4650-b516-5e720d3db941”, amount: 500) events = EventStore.load(command.account_id)
  174. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 165 command = WithdrawFundsCommand.new( account_id: “6504630d-6ceb-4650-b516-5e720d3db941”, amount: 500) events = EventStore.load(command.account_id) # [ FundsDeposited, FundsDeposited, FundsWithdrawn ]
  175. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 166 command = WithdrawFundsCommand.new( account_id: “6504630d-6ceb-4650-b516-5e720d3db941”, amount: 500) events = EventStore.load(command.account_id) # [ FundsDeposited, FundsDeposited, FundsWithdrawn ] account = Account.new account.replay_events(events)
  176. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 167 class Account def replay_events(events) events.each do |event| apply_event(event.type, event.body) end end apply FundsDeposited do |event| self.balance += event.amount end apply FundsWithdrawn do |event| self.balance -= event.amount end end
  177. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 168 class Account def replay_events(events) events.each do |event| apply_event(event.type, event.body) end end apply FundsDeposited do |event| self.balance += event.amount end apply FundsWithdrawn do |event| self.balance -= event.amount end end
  178. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 169 class Account def replay_events(events) events.each do |event| apply_event(event.type, event.body) end end apply FundsDeposited do |event| self.balance += event.amount end apply FundsWithdrawn do |event| self.balance -= event.amount end end
  179. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 170 class Account def replay_events(events) events.each do |event| apply_event(event.type, event.body) end end apply FundsDeposited do |event| self.balance += event.amount end apply FundsWithdrawn do |event| self.balance -= event.amount end end
  180. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 171 command = WithdrawFundsCommand.new( account_id: “6504630d-6ceb-4650-b516-5e720d3db941”, amount: 500) events = EventStore.load(command.account_id) # [ FundsDeposited, FundsDeposited, FundsWithdrawn ] account = Account.new account.replay_events(events)
  181. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 172 command = WithdrawFundsCommand.new( account_id: “6504630d-6ceb-4650-b516-5e720d3db941”, amount: 500) events = EventStore.load(command.account_id) # [ FundsDeposited, FundsDeposited, FundsWithdrawn ] account = Account.new account.replay_events(events) account.withdraw(command.amount)
  182. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 173 class Account def replay_events(events) events.each do |event| apply_event(event.type, event.body) end end apply FundsDeposited … apply FundsWithdrawn … def withdraw(amount) return false if amount > self.balance create_event(FundsWithdrawn, amount: amount) return true end end
  183. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 174 class Account def replay_events(events) events.each do |event| apply_event(event.type, event.body) end end apply FundsDeposited … apply FundsWithdrawn … def withdraw(amount) return false if amount > self.balance create_event(FundsWithdrawn, amount: amount) return true end end
  184. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 175 class Account def replay_events(events) events.each do |event| apply_event(event.type, event.body) end end apply FundsDeposited … apply FundsWithdrawn … def withdraw(amount) return false if amount > self.balance EventStore.store(FundsWithdrawn, amount: amount) return true end end
  185. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 176 We use the following Event Store feature(s): ‣Put event. ‣ Get all events, in order, from an offset. ‣Get all events, in order, scoped by <something>.
  186. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 177 We use the following Event Store feature(s): ‣Put event. ‣ Get all events, in order, from an offset. ‣Get all events, in order, scoped by aggregate_id.
  187. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 178 Isn’t this slow?
  188. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 179 It can be.
  189. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 180 Keep the number of events per aggregate low.
  190. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 181 An Aggregate that accumulates an unbounded number of events over time is a smell.
  191. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 182 Wherever possible, have fixed life Aggregates.
  192. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 183 We would not put OrderPlaced events under the Customer Aggregate. Instead we have an Order Aggregate.
  193. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 184 Domain modelling is hard. Event sourcing doesn’t change that.
  194. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 185 If you screw this up and find yourself with too many events per aggregate, there is another tool you can use.
  195. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 186 Snapshots.
  196. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 187 It’s effectively the Memento pattern from the Gang of Four book. Design Patterns: Elements of Reusable Object-Oriented Software Gamma, E; Helm, R; Johnson, R; Vlissides, J.
  197. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 188 Snapshots 1 2 … 100 101 99 … 200 199 201 202
  198. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 189 Snapshots 1 2 … 100 101 99 … 200 199 201 Snapshot Snapshot 202
  199. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 190 Snapshots 201 Snapshot 202 Command Handler
  200. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 191 Snapshots are a purely technical optimisation and play no role in the conceptual model of event sourcing.
  201. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 192 Summary: Writing with Commands
  202. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 192 Summary: Writing with Commands ‣ Clients never write the events directly.
  203. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 192 Summary: Writing with Commands ‣ Clients never write the events directly. ‣ Clients express an intent to do something via commands.
  204. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 192 Summary: Writing with Commands ‣ Clients never write the events directly. ‣ Clients express an intent to do something via commands. ‣ Commands are validated by Aggregates, which is a concept borrowed from DDD.
  205. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 192 Summary: Writing with Commands ‣ Clients never write the events directly. ‣ Clients express an intent to do something via commands. ‣ Commands are validated by Aggregates, which is a concept borrowed from DDD. ‣ Aggregates fetch events from the Event Store, and replay them to reconstitute their current state.
  206. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 192 Summary: Writing with Commands ‣ Clients never write the events directly. ‣ Clients express an intent to do something via commands. ‣ Commands are validated by Aggregates, which is a concept borrowed from DDD. ‣ Aggregates fetch events from the Event Store, and replay them to reconstitute their current state. ‣ If the Aggregate accepts the Command, it results in an event.
  207. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 192 Summary: Writing with Commands ‣ Clients never write the events directly. ‣ Clients express an intent to do something via commands. ‣ Commands are validated by Aggregates, which is a concept borrowed from DDD. ‣ Aggregates fetch events from the Event Store, and replay them to reconstitute their current state. ‣ If the Aggregate accepts the Command, it results in an event. ‣ If replaying gets slow, performance can be improved by snapshots.
  208. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 193 Where does this leave us?
  209. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 194 The Circular Architecture Client Event Store Command Handler Query Handler
  210. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 195 Clients express intent through Commands. Client Event Store Command Handler Query Handler Command
  211. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 196 Commands that are accepted result in Events persisted to the Event Store. Client Event Store Command Handler Query Handler Command Event
  212. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 197 Projectors tail the Event Store and builds up projections. Client Event Store Command Handler Query Handler Command Event Projector
  213. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 198 Clients retrieve data from the projections via Queries. Client Event Store Command Handler Query Handler Command Event Projector Query
  214. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 199 Reads (queries) and writes (commands) are decoupled.
  215. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 200 Reads and writes can scale independently.
  216. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 201 Great if you’re read-heavy. (We are.)
  217. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad Single instance Region 2 Region 1 202 Client Event Store Command Handler Query Handler Client Query Handler
  218. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 203 Writes are usually fast, and always consistent.
  219. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 204 Reads are fast, and eventually consistent. (We’ll talk more about this later.)
  220. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 205 Reactors.
  221. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 206 Reactors are the last major building block.
  222. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 207 If Commands are for Client writes…
  223. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 208 …and Queries are for Client reads…
  224. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 209 …then Reactors are for all of the business logic in between.
  225. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 210 Extending the Circular Architecture. Client Event Store Command Handler Query Handler
  226. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 211 Reactors process events, just like projectors. Client Event Store Command Handler Query Handler Reactor
  227. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 212 Reactors also keep a “pointer” of where they are. Client Event Store Command Handler Query Handler Reactor 51
  228. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 213 (In fact, you can argue that projectors are a type of reactor.)
  229. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 214 Instead of building projections, they react to events. Client Event Store Command Handler Query Handler Reactor
  230. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 215 Either through triggering external behaviour… Client Event Store Command Handler Query Handler Reactor External Behaviour Trigger
  231. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 216 …or by emitting new events back to the Event Store. Client Event Store Command Handler Query Handler Reactor Event
  232. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 217 …or both! Client Event Store Command Handler Query Handler Reactor Event External Behaviour Trigger
  233. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 218 Example: Raising events at a different level of abstraction.
  234. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 219 A Reactor reads the event stream and “subscribes” to Order Placed events. Reactor Event Store Order Placed Order Placed Order Placed Order Placed Order Placed Order Placed Order Placed Order Placed
  235. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 220 When it detects the Nth order from a customer, it triggers its business rules. Reactor Event Store Order Placed Order Placed Order Placed Order Placed Order Placed Order Placed Order Placed Order Placed
  236. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 221 It knows that this Customer should be promoted to gold, and emits an event. Reactor Event Store Order Placed Order Placed Order Placed Order Placed Order Placed Order Placed Order Placed Order Placed Customer Promoted To Gold Class
  237. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 222 This Reactor is stateful. Many (but not all) Reactors are.
  238. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 223 It holds reference data, which we also sometimes call internal projections.
  239. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 224 The Reactor watches the stream of events, and reacts when its conditions are met.
  240. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 225 Example: Undertake async side effects that logically follow from an event.
  241. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 226 Another Reactor subscribes to the Gold Class Promotion event. Reactor Event Store Customer Promoted To Gold Class
  242. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 227 It interacts with an email server to send a welcome email to the customer. Reactor Event Store Customer Promoted To Gold Class Email Server
  243. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 228 It then emits an event indicating the action it’s just taken. Reactor Event Store Customer Promoted To Gold Class Gold Class Welcome Email Sent Email Server
  244. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 229 This Reactor executes a side effect from something that has happened, and then emits an event to that effect.
  245. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 230 Given that it’s the only responsibility of the Reactor, is it still a side effect?
  246. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 231 What matters is that we get an audit trail of all the actions the system has undertaken.
  247. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 232 We use the following Event Store feature(s): ‣Put event. ‣Get all events, in order, from an offset. ‣ Get all events, in order, scoped by aggregate_id.
  248. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 233 Reactors are decoupled from each other. (As are projectors.)
  249. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 234 Reactors run as single-threaded processes.
  250. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 235 Like projectors, Reactors are asynchronous and subject to Eventual Consistency.
  251. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 236 Reactors typically do not share code.
  252. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 237 Reactors generally do not share any state.
  253. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 238 Reactors do not share their “internal projections.” Nor do they access any of the “external projections” that Clients use.
  254. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 239 If a Reactor needs state, it should be responsible for managing that state.
  255. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 240 Sometimes there are exceptions.
  256. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 241 Reactors sometimes access a canonical view. Beware temporal coupling.
  257. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 242 Reactors should be built so they can be deleted or lost with no loss of system integrity.
  258. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 243 (Except delays in completing operations.)
  259. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 244 Reactors behave a lot like Microservices.
  260. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 245 Reactors → idempotence. (They are not fully idempotent.)
  261. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 246 For the most part, Reactors will not re-trigger external behaviour when rebuilt against event history.
  262. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 247 Which means that we can reset them back to 0 as well. Client Event Store Command Handler Query Handler Reactor 0
  263. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 248 Reactors should do one thing well.
  264. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 249 Reactors encourage a business- oriented decomposition of the system, rather than a technical one.
  265. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 250 Sagas are built up by reactors.
  266. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 251 Sometimes also referred to as “Process Managers.”
  267. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 252 Reactors are really freaking cool.
  268. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 253 Reactors are where you find most of the complexity, whether it’s necessary complexity or accidental complexity.
  269. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 254 Summary: Reactors
  270. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 254 Summary: Reactors ‣ Reactors process the event stream, like projectors.
  271. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 254 Summary: Reactors ‣ Reactors process the event stream, like projectors. ‣ Reactors reacts to events in several ways:
  272. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 254 Summary: Reactors ‣ Reactors process the event stream, like projectors. ‣ Reactors reacts to events in several ways: ‣ Reactors can raise new events at a different level of abstraction.
  273. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 254 Summary: Reactors ‣ Reactors process the event stream, like projectors. ‣ Reactors reacts to events in several ways: ‣ Reactors can raise new events at a different level of abstraction. ‣ Reactors can trigger side effects that logically follow from an event.
  274. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 254 Summary: Reactors ‣ Reactors process the event stream, like projectors. ‣ Reactors reacts to events in several ways: ‣ Reactors can raise new events at a different level of abstraction. ‣ Reactors can trigger side effects that logically follow from an event. ‣ Reactors leave an audit trail in the form of events.
  275. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 254 Summary: Reactors ‣ Reactors process the event stream, like projectors. ‣ Reactors reacts to events in several ways: ‣ Reactors can raise new events at a different level of abstraction. ‣ Reactors can trigger side effects that logically follow from an event. ‣ Reactors leave an audit trail in the form of events. ‣ Reactors are decoupled from each other and don’t share any state.
  276. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 254 Summary: Reactors ‣ Reactors process the event stream, like projectors. ‣ Reactors reacts to events in several ways: ‣ Reactors can raise new events at a different level of abstraction. ‣ Reactors can trigger side effects that logically follow from an event. ‣ Reactors leave an audit trail in the form of events. ‣ Reactors are decoupled from each other and don’t share any state. ‣ Reactors create the foundation for implementing sagas.
  277. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 254 Summary: Reactors ‣ Reactors process the event stream, like projectors. ‣ Reactors reacts to events in several ways: ‣ Reactors can raise new events at a different level of abstraction. ‣ Reactors can trigger side effects that logically follow from an event. ‣ Reactors leave an audit trail in the form of events. ‣ Reactors are decoupled from each other and don’t share any state. ‣ Reactors create the foundation for implementing sagas. ‣ Reactors are often a source of necessary or accidental complexity.
  278. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 255 Okay, deep breath. Time check. Nearly there.
  279. SATURN 2017 Title of the Presentation Goes Here © 2017

    [Copyright Owner[s]] An In-Depth Look at Event Sourcing with CQRS © 2017 Sebastian von Conrad 256 Recap
  280. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 257 Event Sourcing with CQRS
  281. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 257 Event Sourcing with CQRS ‣ Event Sourcing makes you store business facts as the source of truth.
  282. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 257 Event Sourcing with CQRS ‣ Event Sourcing makes you store business facts as the source of truth. ‣ Event Sourcing makes the system deterministic.
  283. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 257 Event Sourcing with CQRS ‣ Event Sourcing makes you store business facts as the source of truth. ‣ Event Sourcing makes the system deterministic. ‣ CQRS and the Circular Architecture works well with Event Sourcing.
  284. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 257 Event Sourcing with CQRS ‣ Event Sourcing makes you store business facts as the source of truth. ‣ Event Sourcing makes the system deterministic. ‣ CQRS and the Circular Architecture works well with Event Sourcing. ‣ Clients express intent via commands, which if accepted become events.
  285. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 257 Event Sourcing with CQRS ‣ Event Sourcing makes you store business facts as the source of truth. ‣ Event Sourcing makes the system deterministic. ‣ CQRS and the Circular Architecture works well with Event Sourcing. ‣ Clients express intent via commands, which if accepted become events. ‣ Asynchronous projectors process the event stream and build up denormalised projections.
  286. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 257 Event Sourcing with CQRS ‣ Event Sourcing makes you store business facts as the source of truth. ‣ Event Sourcing makes the system deterministic. ‣ CQRS and the Circular Architecture works well with Event Sourcing. ‣ Clients express intent via commands, which if accepted become events. ‣ Asynchronous projectors process the event stream and build up denormalised projections. ‣ Clients query the projections when they want know something.
  287. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 257 Event Sourcing with CQRS ‣ Event Sourcing makes you store business facts as the source of truth. ‣ Event Sourcing makes the system deterministic. ‣ CQRS and the Circular Architecture works well with Event Sourcing. ‣ Clients express intent via commands, which if accepted become events. ‣ Asynchronous projectors process the event stream and build up denormalised projections. ‣ Clients query the projections when they want know something. ‣ Asynchronous reactors process the event stream and reacts to events according to business logic, outputting more events.
  288. SATURN 2017 Title of the Presentation Goes Here © 2017

    [Copyright Owner[s]] An In-Depth Look at Event Sourcing with CQRS © 2017 Sebastian von Conrad 258 Upside
  289. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 259 Upside ‣ Treats the core of your business with respect. ‣ Compatibility with reality. ‣ Rapid iteration and reinterpretation. ‣ Encourages Good Practices™. ‣ Supports (some) Quality Attributes. ‣ Avoids common pain points. ‣ Lets you use the best tool for the job. ‣ You get a free time machine.
  290. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 260 Treats the core of your business with respect.
  291. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 261 You can place no more respect to your data than placing it at the core of your system and then never change it.
  292. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 262 We’re not losing data we can’t value.
  293. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 263 We can’t predict the future.
  294. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 264 We don’t know how to value the data we throw away.
  295. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 265 Events represent fundamental business happenings that always have been (and always will remain) of fundamental interest to the business.
  296. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 266 Compatibility with reality.
  297. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 267 Event Sourcing borrows a lot from the real world.
  298. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 268 Just like in the real world, time is a first class citizen.
  299. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 269 In the real world, the past is done and if you don’t like it, all you can do is compensate for it.
  300. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 270 The real world is eventually consistent because there is no central control.
  301. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 271 The real world is made up of independent actors that do the best with the information they have.
  302. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 272 The real world doesn't have distributed transactions across autonomous agents to keep them in sync.
  303. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 273 (There is no evidence that this is a problem, and event sourced systems don’t assume there is one either.)
  304. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 274 In the real world, core concepts change very very slowly.
  305. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 275 In the real world, we frantically innovate at the edges of systems.
  306. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 276 Event Sourcing, surrounded by lots of disposable and replaceable interpretations of what’s happening, is just like that.
  307. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 277 We seek to emulate the real world.
  308. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 278 Rapid iteration and reinterpretation.
  309. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 279 Just because some things change quickly, doesn’t mean everything does.
  310. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 280 The key concepts in business models stay relatively stable. ‣ Customers ‣ Suppliers ‣ Contracts ‣ Products ‣ Accounts ‣ Services ‣ Orders ‣ Payments ‣ Invoices ‣ Etc.
  311. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 281 If designed well*, the rate of change for events is low. * By designers with understanding of the business domain.
  312. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 282 Meanwhile, we can separate recording of what happened from interpreting what it means.
  313. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 283 Interpretations and experiments are easy to get rid of when no longer relevant.
  314. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 284 Even experiments that create events can be discarded (we can ignore events).
  315. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 285 Encourages Good Practices™.
  316. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 286 Separation of concerns through isolated, asynchronous components.
  317. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 287 If a Reactor or Projector breaks down, it only affects itself. When it comes back, it picks up where it left off.
  318. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 288 New features tend to involve extending code, rather than changing code.
  319. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 289 Encourages code that is expressed in business and user terms.
  320. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 290 Removes abstractions. Forms ubiquitous language.
  321. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 291 Business owners love this.
  322. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 292 We get unidirectional coupling.
  323. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 293 Provides logical seams for Microservices. Slice by capability.
  324. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 294 Supports (some) Quality Attributes.
  325. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 295 Eventual consistency is inherently more scalable and available than approaches which mandate strong consistency.
  326. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 296 Reads scale horizontally and can achieve high availability.
  327. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 297 Writes can be very fast while remain single-threaded (with all of those benefits).
  328. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 298 For the integrity-minded, consider WORM media for your Event Store.
  329. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 299 Append-only data capturing facts provides auditability.
  330. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 300 Avoids common pain points.
  331. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 301 Database migrations.
  332. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 302 ORMs.
  333. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 303 Tightly coupled, lazy-loaded object graphs that are hard to understand and reason about.
  334. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 304 Lets you use the best tool for the job.
  335. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 305 Different building blocks can be written in different languages.
  336. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 306 We can use the right data storage for individual projections.
  337. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 307 Provides seams for team organisation. E.g. one team works on the write-side, another on read-side.
  338. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 308 You can use Event Sourcing for parts of systems, as well.
  339. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 309 You get a free time machine.
  340. SATURN 2017 Title of the Presentation Goes Here © 2017

    [Copyright Owner[s]] An In-Depth Look at Event Sourcing with CQRS © 2017 Sebastian von Conrad 310 Downside
  341. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 311 Downside ‣ Eventual consistency. ‣ Handing over the crown jewels. ‣ Opacity. ‣ Mistakes are forever. ‣ It’s not mainstream. ‣ The J curve of productivity. ‣ Complexity. ‣ Cultures of control.
  342. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 312 Eventual consistency.
  343. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 313 Commands are synchronous. Client Event Store Command Handler Query Handler
  344. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 314 Clients issue a command and receives an ACK/NACK.
  345. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 315 Queries are synchronous. Client Event Store Command Handler Query Handler
  346. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 316 Clients make a query and receive data.
  347. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 317 Projectors are asynchronous. Client Event Store Command Handler Query Handler
  348. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 318 It can take some time for changes to show up in projections.
  349. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 320 Reactors are asynchronous too. Client Event Store Command Handler Query Handler Reactor
  350. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 322 And because they are autonomous, changes can show up at different times.
  351. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 324 When not all parts of a system has the same idea of current state, this is called eventual consistency.
  352. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 325 The real world is eventually consistent.
  353. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 326 Independent actors can act without consulting each other.
  354. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 327 Example: your car getting stolen
  355. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 327 Example: your car getting stolen ‣ Your car may get stolen right now while you’re listening to me.
  356. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 327 Example: your car getting stolen ‣ Your car may get stolen right now while you’re listening to me. ‣ Your “copy” of the world is not in lockstep with the real world.
  357. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 327 Example: your car getting stolen ‣ Your car may get stolen right now while you’re listening to me. ‣ Your “copy” of the world is not in lockstep with the real world. ‣ In your "copy" of the world, your car will be exactly where you left it.
  358. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 327 Example: your car getting stolen ‣ Your car may get stolen right now while you’re listening to me. ‣ Your “copy” of the world is not in lockstep with the real world. ‣ In your "copy" of the world, your car will be exactly where you left it. ‣ Hours from now, you find out that it’s gone.
  359. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 327 Example: your car getting stolen ‣ Your car may get stolen right now while you’re listening to me. ‣ Your “copy” of the world is not in lockstep with the real world. ‣ In your "copy" of the world, your car will be exactly where you left it. ‣ Hours from now, you find out that it’s gone. ‣ That’s when your “copy” of the world is made consistent.
  360. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 327 Example: your car getting stolen ‣ Your car may get stolen right now while you’re listening to me. ‣ Your “copy” of the world is not in lockstep with the real world. ‣ In your "copy" of the world, your car will be exactly where you left it. ‣ Hours from now, you find out that it’s gone. ‣ That’s when your “copy” of the world is made consistent. ‣ This is eventual consistency.
  361. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 328 Example: your bank loan
  362. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 328 Example: your bank loan ‣ Your loan application may be approved by the loan department right now.
  363. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 328 Example: your bank loan ‣ Your loan application may be approved by the loan department right now. ‣ This evening, you may discuss with your partner about whether or when the application will get approved.
  364. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 328 Example: your bank loan ‣ Your loan application may be approved by the loan department right now. ‣ This evening, you may discuss with your partner about whether or when the application will get approved. ‣ Not knowing it already has been.
  365. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 328 Example: your bank loan ‣ Your loan application may be approved by the loan department right now. ‣ This evening, you may discuss with your partner about whether or when the application will get approved. ‣ Not knowing it already has been. ‣ You get a letter in the post days later, informing you of the approval.
  366. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 328 Example: your bank loan ‣ Your loan application may be approved by the loan department right now. ‣ This evening, you may discuss with your partner about whether or when the application will get approved. ‣ Not knowing it already has been. ‣ You get a letter in the post days later, informing you of the approval. ‣ That’s when your “copy” of the world is made consistent.
  367. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 328 Example: your bank loan ‣ Your loan application may be approved by the loan department right now. ‣ This evening, you may discuss with your partner about whether or when the application will get approved. ‣ Not knowing it already has been. ‣ You get a letter in the post days later, informing you of the approval. ‣ That’s when your “copy” of the world is made consistent. ‣ This is eventual consistency.
  368. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 329 If you look for eventual consistency, you will find it everywhere. (The light from the Sun takes 8 mins to get to Earth.)
  369. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 330 Modelling systems based on lessons from the real world is very helpful when dealing with eventual consistency.
  370. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 331 But we’re better than this, right?
  371. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 332 Software can push the natural boundaries of the real world, can it not?
  372. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 333 Well, actually…
  373. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 334 Your system is already eventually consistent.
  374. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 335 As soon as you let go of a database connection, you don’t know the data you drew from it is still correct.
  375. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 336 While returning data to the client, the data may have been changed.
  376. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 337 You can narrow the time window, but never eliminate it.
  377. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 338 If we can’t eliminate eventual consistency, we have to manage it.
  378. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 339 Risk is a function of time.
  379. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 340 Is 1 nanosecond okay? What about 1 month?
  380. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 341 Your threshold is contextual.
  381. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 342 Showing order status to a user? Threshold: seconds.
  382. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 343 Sales report? Threshold: hours.
  383. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 344 It’s a business risk, not a technical one.
  384. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 345 Ask: what’s the risk of making decisions with out-of-date information?
  385. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 346 Unlike other architectures, however, this one emphasises eventual consistency and makes it a first class concern.
  386. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 347 Call it a “feature.”
  387. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 348 This presents challenges, especially for developers used to not dealing with it. (Even though it’s always been there.)
  388. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 349 There are ways to manage eventual consistency.
  389. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 350 Managing eventual consistency
  390. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 350 Managing eventual consistency ‣ Educate your users.
  391. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 350 Managing eventual consistency ‣ Educate your users. ‣ Lie to your users.
  392. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 350 Managing eventual consistency ‣ Educate your users. ‣ Lie to your users. ‣ Read your own writes.
  393. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 350 Managing eventual consistency ‣ Educate your users. ‣ Lie to your users. ‣ Read your own writes. ‣ Build new user interfaces.
  394. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 350 Managing eventual consistency ‣ Educate your users. ‣ Lie to your users. ‣ Read your own writes. ‣ Build new user interfaces. ‣ Slow down the user.
  395. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 350 Managing eventual consistency ‣ Educate your users. ‣ Lie to your users. ‣ Read your own writes. ‣ Build new user interfaces. ‣ Slow down the user. ‣ Speed up the backend.
  396. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 351 Though it is manageable, it can be an absolute blocker.
  397. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 352 Handing over the crown jewels.
  398. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 353 There is nothing more important than the data we store.
  399. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 354 Quote from FP workshop: “Apps age like fish, data age like wine.”
  400. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 355 Data persists even as technologies are replaced.
  401. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 356 In any organisation, fundamentally changing the way data is stored is a risk.
  402. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 357 The default answer to whether you should fundamentally change how to store data is “no.”
  403. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 358 Opacity.
  404. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 359 Traditional relational models means any developer can poke around in the data to answer questions.
  405. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 360 Not just developers! Other folks with basic SQL experience as well.
  406. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 361 An Event Store renders all of those tools and ubiquitous access irrelevant. This is a big deal.
  407. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 362 Your data is more opaque in an Event Store.
  408. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 363 You have to create projections to make sense of the data.
  409. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 364 This can be burdensome, particularly for ad-hoc queries.
  410. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 365 You have to create compensating events rather than quick “fixups.” Both an advantage and a disadvantage.
  411. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 366 Mistakes are forever.
  412. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 367 Natural evolution of event schemas do happen.
  413. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 368 You can’t change events after-the-fact.
  414. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 369 Upcasting and compensating events become the norm.
  415. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 370 Confidentiality can be issue.
  416. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 371 There are strategies but they can be painful. Some are not for the faint of heart.
  417. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 372 It’s not mainstream.
  418. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 373 There are not enough resources.
  419. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 374 Books, courses, presentations, etc. But also libraries and frameworks.
  420. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 375 There may never be.
  421. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 376 It can be difficult to hire.
  422. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 377 In my experience, many teams with little prior knowledge is gonna struggle hard.
  423. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 378 You almost have to have someone who’s done it before.
  424. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 379 The J curve of productivity.
  425. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 380 Time -ive +ive
  426. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 381 It will get worse before it gets better.
  427. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 382 It takes time before the benefits kick in.
  428. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 383 Particularly if the choice was made for the team, not by the team.
  429. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 384 Don’t impose Event Sourcing on a team that: ‣ Lacks buy-in to try it. ‣ Lacks stakeholder support. ‣ Lacks intestinal fortitude.
  430. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 385 Complexity.
  431. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 386 First, let’s not conflate inherent necessary domain complexity with accidental complexity.
  432. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 387 We can’t avoid complexity, but we can choose how to implement it.
  433. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 388 Monoliths are: a simple arrangement of complex things.
  434. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 389 Event Sourcing with CQRS is: a complex arrangement of simple things.
  435. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 390 The same goes for Microservices.
  436. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 391 Don’t listen to people who tell you Microservices make things simpler without acknowledging it also makes other things more complex.
  437. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 392 Event Sourcing with CQRS has lots of moving parts.
  438. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 393 Event Sourcing with CQRS usually means difficulty tracing across async components.
  439. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 394 Beware devolving into a complex arrangement of complex things.
  440. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 395 It’s easy to get here with the cognitive load introduced by a fundamental paradigm shift.
  441. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 396 Most developers will struggle.
  442. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 397 This is the number one issue I’ve encountered. (YMMV.)
  443. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 398 Cultures of control.
  444. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 399 Not appropriate to allow systems you have less control of to consume events.
  445. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 400 Events contain all the data, and you might not want (or be allowed) to share that data.
  446. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 401 Event Sourcing with CQRS favours environments where autonomy is a cultural norm, rather than control.
  447. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 402 You can do it in tighter control environments: ‣ Keep Event Store behind high walls. ‣ Build more standard APIs. ‣ Only contain data you want clients to have.
  448. SATURN 2017 Title of the Presentation Goes Here © 2017

    [Copyright Owner[s]] An In-Depth Look at Event Sourcing with CQRS © 2017 Sebastian von Conrad 403 This is a pattern
  449. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 404 It’s not appropriate for every use case.
  450. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 405 It’s not a silver bullet.
  451. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 406 It has lots of awesome benefits, but also significant disadvantages.
  452. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 407 If you can overcome the challenges, it can be incredibly powerful.
  453. SATURN 2017 An In-Depth Look at Event Sourcing with CQRS

    © 2017 Sebastian von Conrad 408 “Event Sourcing is a multiplier. Done well, it gives you magical powers in terms of flexibility and scalability. Done poorly, it becomes worse than the crappiest balls of mud.”
  454. SATURN 2017 Title of the Presentation Goes Here © 2017

    [Copyright Owner[s]] An In-Depth Look at Event Sourcing with CQRS © 2017 Sebastian von Conrad 409 Thank you!