-
-
Notifications
You must be signed in to change notification settings - Fork 364
Workers and parallelism
Since everything in Rebus is async
, one single worker can perform a ridiculous amount of work in parallel if that work can be await
ed.
Therefore – to avoid doing too much work – the "max parallelism" concept is present, which puts a global max cap on how many messages to process in parallel.
You can configure those options by going
services.AddRebus(
configure => configure
.(...)
.Options(o => {
o.SetNumberOfWorkers(1);
o.SetMaxParallelism(10);
})
);
Which values to use for the settings depends on the type of work you want to perform. The following are some reasonable settings that maybe can give you some food for thought:
- Work is predominantly asynchronous – use a few worker threads and fairly high parallelism, e.g.
o.SetNumberOfWorkers(2); o.SetMaxParallelism(20);
- Work is fast and synchronous – use a few worker threads and a matching parallelism, e.g.
o.SetNumberOfWorkers(5); o.SetMaxParallelism(5);
(although setting the parallelism higher will not affect anything in this case) - Work is slow and synchronous – use more worker threads and a matching parallelism, e.g.
o.SetNumberOfWorkers(15); o.SetMaxParallelism(15);
(again: setting the parallelism higher will not affect anything in this case) - Work must be performed in a synchronous manner, effectively serializing access to some particular resource (or to make an endpoint easier to debug during development) – use one single worker thread and a matching parallelism:
o.SetNumberOfWorkers(1); o.SetMaxParallelism(1);
Since the parallelism setting puts an absolute upper cap on how many messages can be handled in parallel, it does not make sense to set the parallelism lower than the number of threads. It does not create any problems on the other hand though, it is just a waste of resources.
Rebus will default to 1 worker thread and a max parallelism of 5.
Rebus will actually spawn the number of worker threads as dedicated threads. The threads will be used for polling the transport for messages.
Depending on whether the transport is asynchronous or not, the continuation after having received a message will either execute on the worker thread or on the thread pool.
Using dedicated worker threads is Rebus' default mode of operation.
Rebus also provides the options of using an alternative TPL-based worker factory, which will NOT spawn dedicated worker threads - instead, it will simply make a number of async calls to the transport.
For this to work properly, the transport must support an API for proper, asynchronous receive (which most modern transports do - but, incidentally, which the MSMQ transport does NOT!).
It can be enabled by going
services.AddRebus(
configure => configure
.(...)
.Options(o => {
o.UseTplToReceiveMessages();
})
);
When using the TPL-based worker factory, the parallelism setting becomes redundant - the "number of workers" will define how many parallel receive operations will be commenced, and thus how many messages can potentially end up being handled concurrently.
Basic stuff
- Home
- Introduction
- Getting started
- Different bus modes
- How does rebus compare to other .net service buses?
- 3rd party extensions
- Rebus versions
Configuration
Scenarios
Areas
- Logging
- Routing
- Serialization
- Pub sub messaging
- Process managers
- Message context
- Data bus
- Correlation ids
- Container adapters
- Automatic retries and error handling
- Message dispatch
- Thread safety and instance policies
- Timeouts
- Timeout manager
- Transactions
- Delivery guarantees
- Idempotence
- Unit of work
- Workers and parallelism
- Wire level format of messages
- Handler pipeline
- Polymorphic message dispatch
- Persistence ignorance
- Saga parallelism
- Transport message forwarding
- Testing
- Outbox
- Startup/shutdown
Transports (not a full list)
Customization
- Extensibility
- Auto flowing user context extensibility example
- Back off strategy
- Message compression and encryption
- Fail fast on certain exception types
Pipelines
- Log message pipelines
- Incoming messages pipeline
- Incoming step context
- Outgoing messages pipeline
- Outgoing step context
Prominent application services