Search code examples
multithreadingnservicebusnservicebus-sagasmessage-handlers

Should NServiceBus handlers always quickly complete?


[Edited for clairty]

I'm not sure I understand correctly.

Inside the Saga everything should be concise and quick, according to these instructive posts:

  • Jonathan Oliver's summary: blog.jonathanoliver.com/…
  • Udi Dahan's original: skillsmatter.com/skillscasts/…
  • and some earlier posts
  • lostechies.com/jimmybogard/2013/03/26/scaling-nservicebus-sagas
  • docs.particular.net/nservicebus/architecture/principles

That is to say that your saga should have no business logic, and no if-else instructions inside. It should be only an orchestrator, and calling it should be planned to be 'success-oriented' - that is: as much validation as possible should have been done before calling the Saga as possible.

But what about the separate handlers (do you call them "independent handlers"?), those handlers that are not inside a saga? Which of the following is correct for them:

a. Should the NServiceBus message handlers outside a saga always complete quickly, and if there is a time-consuming action pass that on to a thread and complete?

b. Or is it better to "hog" the handler, and that way the NServiceBus "knows" that this message is being used with a heavy toll, and can take action accordingly - i.e. with automatic load balancing, create another instance of the handling process on a different process or even different machine?

What is the correct way to go?

And can you supply some sample code along with your answer, calling a Foo.DoTimeConsumingBar() method.

Thank you.


Solution

  • @pashute, your statements about Sagas are correct, the should do no work (I/O) and should act as choreographers of long running business processes.

    Handlers are supposed to follow SRP Single Responsibility Principle, and that should still keep them lean,even if they are I/O intensive. In the case of I/O intensive operations a distributor/load balancer would help to scale out the load.

    Does that make sense?