Saga parallelism - rebus-org/Rebus GitHub Wiki

Sometimes when working with sagas, you will know beforehand that there will be contention around certain saga instances.

This often happens e.g. if a saga spawns off work in the form of a bunch of requests, to which it will receive (and keep track of) a bunch of replies – all these replies will hit the same saga instance, in which case parallel execution does not make any sense at all.

If you often experience ConcurrencyExceptionss because of this (or another similar) problem, you can avoid parallel execution completely within the current process by enabling in-process locking with the EnforceExclusiveAccess() extension like this:

Configure.With(...)
    .(...)
    .Sagas(s => {
        s.StoreIn(...);

        s.EnforceExclusiveAccess();
    })
    .Start();

This will enable an incoming message middleware that grabs locks on each (saga type, correlation property name, correlation property value) tuple relevant for the incoming message – possibly waiting for the locks to become available – before creating/loading the saga data.

This way, exclusive access is guaranteed for this particular bus instance.