Transport Transactions

Component: NServiceBus
Nuget Package: NServiceBus Version: 5.x

This article covers various levels of consistency guarantees NServiceBus provides with regards to:

  • receiving messages
  • updating user data
  • sending messages

It does not discuss the transaction isolation aspect. Transaction isolation applies only to the process of updating the user data, it does not affect the overall coordination and failure handling.

Transactions

NServiceBus offers four levels of guarantees with regards to message processing. Levels availability depends on the selected transport. See also Transaction handling in Azure.

Transaction levels supported by NServiceBus transports

The implementation details for each transport are discussed in the dedicated documentation sections. They can be accessed by clicking the links with the transport name in the following table:

Transaction scope (Distributed transaction)Transport transactionUnreliable (Transactions Disabled)
MSMQ
SQL Server
RabbitMQ
Azure Storage Queues
Azure Service Bus

Transaction scope (Distributed transaction)

In this mode the transport receive operation is wrapped in a TransactionScope. Other operations inside this scope, both sending messages and manipulating data, are guaranteed to be executed (eventually) as a whole or rolled back as a whole.

If required the transaction is escalated to a distributed one (following two-phase commit protocol). That depends on the transport and persistence choice, for example it is not needed when using SQL Server transport with NHibernate persistence both using the same database. In this case the ADO.NET driver guarantees that everything happens inside a single database transaction, ACID guarantees are held for the whole processing.

MSMQ will escalate to a distributed transaction right away since it doesn't support promotable transaction enlistments.

Transaction scope mode is enabled by default for the transports that support it (i.e. MSMQ and SQL Server transport). It can be enabled explicitly via

var transactionSettings = busConfiguration.Transactions();
transactionSettings.Enable();
transactionSettings.EnableDistributedTransactions();

Consistency guarantees

In this mode handlers will execute inside the TransactionScope created by the transport. This means that all the data updates are executed as a whole or rolled back as a whole.

Distributed transactions do not guarantee atomicity. Changes to the database might be visible before the messages disappear from the queue or vice-versa, but they are guaranteed to eventually all sync up to reflect the outcome of the transaction.

This mode requires the selected storage to support participating in distributed transactions.

Transport transaction

In this mode the receive operation is wrapped in a transport's native transaction. This mode guarantees that the message is not permanently deleted from the incoming queue until at least one processing attempt (including storing user data and sending out messages) is finished successfully. See also recoverability for more details on retries.

Use the following code to use this mode:

var transactionSettings = busConfiguration.Transactions();
transactionSettings.DisableDistributedTransactions();

Consistency guarantees

In this mode some (or all) handlers might get invoked multiple times and partial results might be visible:

  • partial updates - where one handler succeeded updating its data but the other didn't
  • partial sends - where some of the messages has been sent but others not

When using this mode all handlers must be idempotent. In other words the result needs to be consistent from a business perspective even when the message is processed more than once.

See the Outbox section below for details on how NServiceBus can handle idempotency at the infrastructure level.

Not all the transport support batched dispatch and this means that messages could be sent out without a matching update to business data, depending on the order in which statements were executed. Such messages are called ghost messages. To avoid this situation make sure to perform all bus operations only after modifications to business data. When reviewing the code remember that there can be multiple handlers for a given message and that handlers are executed in a certain order.

Unreliable (Transactions Disabled)

Disabling transactions is generally not recommended, because it might lead to the message loss. It might be considered if losing some messages is not problematic and if the messages get outdated quickly, e.g. when sending readings from sensors at regular intervals.

In this mode, when encountering a critical failure such as system or endpoint crash, the message is permanently lost.
var transactionSettings = busConfiguration.Transactions();
transactionSettings.Disable();
When transactions are disabled, no retries will be performed and messages will not be forwarded to the error queue in the event of any failure and the message will be permanently lost.

Outbox

The Outbox feature provides idempotency at the infrastructure level and allows running in transport transaction mode while still getting the same semantics as Transaction scope mode.

Outbox data needs to be stored in the same database as business data to achieve the exactly-once delivery.

When using the Outbox, any messages resulting from processing a given received message are not sent immediately but rather stored in the persistence database and pushed out after the handling logic is done. This mechanism ensures that the handling logic can only succeed once so there is no need to design for idempotency.

Avoiding partial updates

In this mode there is a risk of partial updates since one handler might succeed in updating business data while another handler fails. To avoid this configure NServiceBus to wrap all handlers in a TransactionScope that will act as a unit of work and make sure that there is no partial updates. Use following code to enable a wrapping scope:

var transactionSettings = busConfiguration.Transactions();
transactionSettings.DisableDistributedTransactions();
transactionSettings.WrapHandlersExecutionInATransactionScope();
This requires that all the data stores used by the handler support enlisting in a distributed transaction (e.g. SQL Server), including the sage store when using sagas.
This might escalate to a distributed transaction if data in different databases are updated.
This API has no effect when used in combination with transports running in a transaction scope mode.

Controlling transaction scope options

The following options for transaction scopes used during message processing can be configured.

Isolation level

NServiceBus will by default use the ReadCommitted isolation level. Change the isolation level using

var transactionSettings = busConfiguration.Transactions();
transactionSettings.IsolationLevel(IsolationLevel.RepeatableRead);

Transaction timeout

NServiceBus will use the default transaction timeout of the machine the endpoint is running on.

Change the transaction timeout using

var transactionSettings = busConfiguration.Transactions();
transactionSettings.DefaultTimeout(TimeSpan.FromSeconds(30));

Or via .config file using a example DefaultSettingsSection.


Last modified