Endpoints running on the Azure Storage Queues transport using a single storage account are subject to potential throttling once the maximum number of concurrent requests to the storage account is reached. Multiple storage accounts can be used to overcome this limitation. To better understand scale out options with storage accounts, it is advised to first read carefully the Azure storage account scalability and performance targets article.
Azure Storage Scalability and Performance
All messages in a queue are accessed via a single queue partition. A single queue is targeted to process up to 2,000 messages per second. Scalability targets for storage accounts can vary based on the region, reaching up to 20,000 messages per second (throughput achieved using an object size of 1KB). This is subject to change and should be periodically verified.
When the number of messages per second exceeds this quota, the storage service responds with an HTTP 503 Server Busy message. This message indicates that the platform is throttling the queue. If a single storage account is unable to handle an application's request rate, the application could leverage several different storage accounts using a storage account per endpoint. This ensures application scalability without saturating a single storage account. This also gives a discrete control over queue processing, based on the sensitivity and priority of the messages that are handled by different endpoints. For example, high priority endpoints could have more dedicated workers than low priority endpoints.
Using multiple storage accounts is currently NOT compatible with ServiceControl, it is necessary to use ServiceControl transport adapter or multiple installations of ServiceControl for monitoring in such situation.
Scaling Out
A typical implementation uses a single storage account to send and receive messages. All endpoints are configured to receive and send messages using the same storage account.
When the number of instances of endpoints is increased, all endpoints continue reading and writing to the same storage account. Once the limit of 2,000 message/sec per queue or 20,000 message/sec per storage account is reached, Azure storage service throttles messages throughput.
While an endpoint can only read from a single Azure storage account, it can send messages to multiple storage accounts. This way one can set up a solution using multiple storage accounts where each endpoint uses its own Azure storage account, thereby increasing message throughput.
Scale Units
Scaleout and splitting endpoints over multiple storage accounts work to a certain extent, but it cannot be applied infinitely while expecting throughput to increase linearly. Each resource and group of resources has certain throughput limitations.
A suitable technique to overcome this problem includes resource partitioning and usage of scale units. A scale unit is a set of resources with well determined throughput, where adding more resources to this unit does not result in increased throughput. When the scale unit is determined, to improve throughput more scale units can be created. Scale units do not share resources.
An example of a partitioned application with a different number of deployed scale units is an application deployed in various regions.
Use real Azure storage accounts. Do not use Azure storage emulator as it only supports a single fixed account named devstoreaccount1.".
Cross namespace routing
NServiceBus allows to specify destination addresses using an "endpoint@physicallocation"
when messages are dispatched, in various places such as the Send and Routing API or the MessageEndpointMappings
. In this notation the physicallocation
section represents the location where the endpoint's infrastructure is hosted, such as a storage account.
Using this notation it is possible to route messages to any endpoint hosted in any storage account.
Using send options
The use of send options enables routing messages to any endpoint hosted in another storage account by specifying the storage account using the @
notation. The @
notation is used to point to a connection string represented by a specified alias.
await endpointInstance.Send(
destination: "sales@accountAlias",
message: new MyMessage());
Aliases instead of connection strings
To avoid connection strings leaking, aliases are always used, using an empty string as the default. Therefore, when multiple accounts are used, an alias has to be registered for each storage account.
To enable sending from account_A
to account_B
, the following configuration needs to be applied in the account_A
endpoint:
var transport = endpointConfiguration.UseTransport<AzureStorageQueueTransport>();
transport.ConnectionString("account_A_connection_string");
transport.DefaultAccountAlias("account_A");
var accountRouting = transport.AccountRouting();
accountRouting.AddAccount("account_B", "account_B_connection_string");
Aliases can be provided for both the endpoint's connection strings as well as other accounts' connection strings. This enables using the @
notation for destination addresses like queue_name@accountAlias
.
await endpointInstance.Send(
destination: "sales@accountAlias",
message: new MyMessage());
The examples above use different values for the default account aliases. Using the same name, such as default
, to represent different storage accounts in different endpoints is highly discouraged as it introduces ambiguity in resolving addresses like queue@default
and may cause issues when e.g. replying. In that case an address is interpreted as a reply address, the name default
will point to a different connection string.
This feature is currently NOT compatible with ServiceControl. A ServiceControl transport adapter is required in order to leverage both.
Using registered endpoints
In order to route message to endpoints without having to specify the destination at all times, it is also possible to register the endpoint for a given command type, assembly or namespace
var transportConfig = configuration.UseTransport<AzureStorageQueueTransport>();
var routing = transportConfig
.ConnectionString("connectionString")
.AccountRouting();
var anotherAccount = routing.AddAccount("AnotherAccountName", "anotherConnectionString");
anotherAccount.AddEndpoint("Receiver");
transportConfig.Routing().RouteToEndpoint(typeof(MyMessage), "Receiver");
Once the endpoint is registered no send options need to be specified.
await endpointInstance.Send(message: new MyMessage());
Publishers
Similar to sending to an endpoint, the transport can also be configured to subscribe to events published by endpoints in another storage account, using:
var transportConfig = configuration.UseTransport<AzureStorageQueueTransport>();
transportConfig.DefaultAccountAlias("subscriber");
var routing = transportConfig
.ConnectionString("connectionString")
.AccountRouting();
var anotherAccount = routing.AddAccount(alias: "publisher", connectionString: "anotherConnectionString");
anotherAccount.AddEndpoint(endpointName: "Publisher1", publishedEvents: new[] { typeof(OrderAccepted) }, subscriptionTableName: "overrideForSubscriptionTableName");