Tuning endpoint message processing

NServiceBus will by default allow the transport to optimize for maximum performance when it comes to message processing. While this is usually the preferred mode of operation there are situations where tuning needs to be applied.

Tuning concurrency

NServiceBus Version 5 and below will by default limit concurrency to 1 if not configured by the user

You can define a maximum concurrency setting that will make sure that no more messages than the specified value is ever being processed at the same time. Set this value to 1 to process messages sequentially. If not specified the transport will choose an optimal value.

Examples where concurrency tuning is relevant are

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

Tuning throughput

Throughput throttling options have been removed in NServiceBus Version 6.

You can define a maximum value for the number of messages per second that the endpoint will process at any given time. This will help you to avoid the endpoint overloading sensitive resources that it's using like web-services, databases, other endpoints etc. A concrete example here could be an integration endpoint calling a web api, like api.github.com, that have restrictions on the number or requests per unit of time allowed.

NServiceBus will not enforce any throughput restrictions by default.

Configuration

The default limits of an endpoint can be changed in both code and via app.config.

Via Code

By overriding app.config settings.

6-pre NServiceBus
endpointConfiguration.LimitMessageProcessingConcurrencyTo(5);
4.x - 5.x NServiceBus
public class ProvideConfiguration :
    IProvideConfiguration<TransportConfig>
{
    public TransportConfig GetConfiguration()
    {
        return new TransportConfig
        {
            MaximumConcurrencyLevel = 5,
            MaximumMessageThroughputPerSecond = 10
        };
    }
}
3.x NServiceBus
public class ProvideConfiguration :
    IProvideConfiguration<MsmqTransportConfig>
{
    public MsmqTransportConfig GetConfiguration()
    {
        return new MsmqTransportConfig
        {
            NumberOfWorkerThreads = 5
        };
    }
}

Via app.config

By using raw xml.

4.x - 5.x NServiceBus
<configSections>
  <section name="TransportConfig"
           type="NServiceBus.Config.TransportConfig, NServiceBus.Core"/>
</configSections>
<TransportConfig MaximumConcurrencyLevel="5"
                 MaximumMessageThroughputPerSecond="10"/>
3.x NServiceBus
<configSections>
  <section name="MsmqTransportConfig"
           type="NServiceBus.Config.TransportConfig, NServiceBus.Core"/>
</configSections>
<MsmqTransportConfig NumberOfWorkerThreads="5" />

Run time settings

Version 5 and below allowed both concurrency and throughput throttling to be changed and read at run time using the code below.

Optimizing at run time

4.x - 5.x NServiceBus
unicastBus.Transport.ChangeMaximumMessageThroughputPerSecond(10);
unicastBus.Transport.ChangeMaximumConcurrencyLevel(5);

Reading current values at run time

4.x - 5.x NServiceBus
int messageThroughputPerSecond = unicastBus.Transport.MaximumMessageThroughputPerSecond;
int maximumConcurrencyLevel = unicastBus.Transport.MaximumConcurrencyLevel;

Related Articles


Last modified 2016-03-04 09:29:13Z