I'm building a service using the familiar event sourcing pattern:
- A request is received.
- The aggregate's history is loaded.
- The aggregate is rebuilt (from its history).
- New events are prepared and the aggregate is updated in response to the incoming request from Step 1.
- These events are written to the log, and are made available (published) to any subscribers.
In my case, Step 5 is accomplished in two parts. The events are written to the event log. A background process reads from the event log and publishes all events starting from an offset.
In some cases, I need to publish side effects in addition to events related to the aggregate. As far as the system is concerned, these are events too because they are consumed by and affect the state of other services. However, they don't affect the history of the aggregate in this service and are not needed to rebuild it.
How should I handle these in the code?
Option 1- Don't write side-effecting events to the event log. Publish these in the main process prior to Step 5.
Option 2- Write everything to the event log and ignore side-effecting events when the history is loaded. (These aren't part of the history!)
Option 3- Write side-effecting events to a dummy aggregate so they are published, but never loaded.
Option 4- ?
In the first option, there may be trouble if there is a concurrency violation. If the write fails in Step 5, the side effect cannot be easily rolled back. The second option write events that are not part of the aggregate's history. When loading in Step 2, these side-effecting events would have to be ignored. The 3rd option feels like a hack.
Which of these seems right to you?