I’m planning out the events that my application will be creating, but am running into problems with my design. I believe I’m not applying the event-sourcing concepts correctly.
Take the bank account example. My application receives a command to deposit cash, accepts it, and then creates the event CashDeposited
. Based on the business logic, the higher account balance triggers the interest rate to be increased, so I create the event InterestRateChanged
.
Now if I record both of these events to my event store, upon restart, both events will be replayed. But because the CashDeposited
event triggers the InterestRateChanged
event, won’t the InterestRateChanged
be replayed twice: once from the event store and once as a result of CashDeposited
being replayed?
1
My application receives a command to deposit cash, accepts it, and then creates the event CashDeposited. Based on the business logic, the higher account balance triggers the interest rate to be increased, so I create the event InterestRateChanged.
Now if I record both of these events to my event store, upon restart, both events will be replayed.
Good. That’s exactly what you want.
But because the CashDeposited event triggers the InterestRateChanged event, won’t the InterestRateChanged be replayed twice: once from the event store and once as a result of CashDeposited being replayed?
Yeah, if you are having this problem, your business logic may be in the wrong place.
Very broadly: the event handler part of your entity is supposed to be simple; apply the state changes implied by the event, validate the business invariant, and nothing else.
The events themselves are typically created by running a command on your entity — it’s the command that has the business logic to say “increase the balance, and then if the trigger fires then change the interest rate”. In your specific example, that one command will generate two events (perfectly reasonable).
When you reload the entity, you are going to replay the events, but not the command(s) that produced them. So the replay includes all the changes to state, but you don’t replay the business logic.
I recommend a proper implementation of Gateway pattern. As Martin Fowler states:
Many of the advantages of Event Sourcing stem from the ability to
replay events at will, but if these events cause update messages to be
sent to external systems, then things will go wrong because those
external systems don’t know the difference between real processing and
replays.To handle this you’ll need to wrap any external systems with a
Gateway. This in itself isn’t too onerous since it’s a thoroughly good
idea in any case. The gateway has to be a bit more sophisticated so it
can deal with any replay processing that the Event Sourcing system is
doing.
E.g.:
public class VerySimpleGateway()
{
public bool IsReplayMode;
public void FilterProcessingEvent(IEvent event)
{
if (IsReplayMode) {
// for example delete event from event processing queue or adjust event state itself
// next processing component in the pipeline is informed not to touch this event
}
}
}
In event processing pipeline you can treat such gateway as a step in Chain-responsibility pattern.
2
You have two choices
- Only store trigger/first/external events in the event store
In this case your “CashDeposited” event. So when you replay the
this all the subsequent events will be triggered as per normal processing. - Store all events but check an “isReplay” flag before triggering a subsequent event.
I like the first option best. If you are replaying because an error occurred (say you set the threshold for an increase in interest rate too low) then the system will replay correctly with the fixed parameter.
Plus you really don’t want to complicate your code and testing with all those extra paths for “isReplay”.
I believe you are looking for the Process Manager pattern which listens for events and issues commands. In a replay scenario, the commands are ignored.
(This came from an informative answer to another question.)
In short, the PM listens for the right kind of CashDeposited event and issues a ChangeInterestRate command if necessary.
I think the problem here is that InterestRateChanged
is not strictly an event from an event sourcing perspective – it’s a consequence of the CashDeposited
event that might not happen if, for example, the business logic changes.
Remember that the key benefit of event sourcing is resilience in the face of changes in the rules. Your cash will always have been deposited, but how that results in interest being calculated is dependent on the business rules. Supposing there was a bug in interest rate calculation, then, simply changing that logic will fix the account automagically the next time the write model is loaded.
That’s the main reason you’re storing events and not a state snapshot. Not so you can “replay” them manually later, but so they’re always replayed and accounting for changes (or more likely – corrections) in the rules.
1