efacilitation/eventric

Repository should publish events always in series

alexlawrence opened this issue · 0 comments

Currently the repository has two ways of publishing domain events.

  1. Without waiting mode: The repository will call the publish function on the EventBus in a synchronous for-loop for each domain event to publish.
  2. With waiting mode: The repository will call the publish function on the EventBus in an asynchronous series. This means it will wait until one event is completely published before continuing with the next one.

The waiting for an event to be completely published actually consists of two things:

  1. Waiting until all subscribers for one particular event have been notified
  2. Waiting until all subscribers have executed their handle functions (which may be async)

Without waiting mode, the context does not even wait until the subscribers have been notified. This can lead to unwanted behaviour.

Example:

  • One context, one command, one aggregate, two behaviours
  • The command executes both behaviours on the aggregate, therefore two domain events are published at once
  • One remote, two remote projections
  • The first projection only listens to the first event
  • The second projection listens to both events

Now when executing the command and therefore generating two domain events the second projection may receive the events in the wrong order. Even worse, depending on the registration order of the projections this might be change.

Therefore we always (with or without waiting mode) need at least to wait until all subscribers have been notified for one event before continuing with the next one.