Getting Started
Architecture
NServiceBus
Transports
Persistence
Hosting
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Modernization
Samples

Tuning message throughput performance and concurrency

Component: NServiceBus
NuGet Package: NServiceBus 10.x

NServiceBus uses defaults that ensure good performance in common cases. This is usually the preferred mode of operation, but tuning might be required in some situations.

Concurrency tuning might be relevant in these situations:

  • Non-thread-safe code that needs to run sequentially
  • Databases that might deadlock when handling too many concurrent requests

Configuring concurrency limit

You can change an endpoint's default concurrency setting in code:

endpointConfiguration.LimitMessageProcessingConcurrencyTo(5);

Limit maximum concurrency so that no more than the specified number of messages are processed at the same time. If a maximum concurrency is not specified, the transport chooses an optimal value that balances throughput and effective resource usage. The concurrency limit cannot be changed at runtime. It can only be applied when creating an endpoint instance, and the endpoint instance must be restarted for concurrency changes to take effect.

Set up infrastructure monitoring for the environment that hosts the endpoint instance, as well as remote resources such as databases. Monitor CPU, RAM, network, and storage to validate that concurrency changes are not negatively affecting the overall system.

Parallelism

If you have long-running compute-bound code or synchronous code in your handler and want to achieve higher parallelism, refer to the calling long-running code in async handlers section of the documentation.

Sequential processing

Set the concurrency limit value to 1 to process messages sequentially. Sequential processing is not a guarantee for ordered processing. For example, processing failures and recoverability will result in out-of-order processing.

Throttling

Throughput throttling options are deprecated. To enable throttling on version 6 and later, use a custom behavior. The throttling sample demonstrates how to implement one.

Related Articles