In RavenDB 3.5, the client implementation of distributed transactions contains a bug that could cause an endpoint to lose data under rare conditions. If RavenDB is configured to enlist in distributed transactions with RavenDB 3.5, read DTC not supported for RavenDB Persistence.
Using RavenDB version 5 and higher in a cluster configuration with multiple nodes is only supported from version 7 or higher of the NServiceBus.RavenDB persistence package. For more information, read cluster configuration with multiple nodes.
This sample demonstrates how to configure RavenDB Persistence to store tenant-specific data in separate databases. The tenant-specific information includes the saga state, the business documents that are accessed using RavenDB-managed session, and the outbox records.
This sample uses Outbox to guarantee consistency between the saga state and the business entity.
The sample assumes that the tenant information is passed as a custom message header tenant_id
.
Prerequisites
The databases created by this sample are:
MultiTenantSamples
MultiTenantSamples-A
MultiTenantSamples-B
Running the project
- Start the Sender project (right-click on the project, select the
Debug > Start new instance
option). - The text
Press
should be displayed in the Sender's console window.<enter> to send a message - Start the Receiver project (right-click on the project, select the
Debug > Start new instance
option). - The Sender should display subscription confirmation
Subscribe from Receiver on message type OrderSubmitted
. - Press
A
orB
on the Sender console to send a new message either to one of the tenants.
Verifying that the sample works correctly
- The Receiver displays information that an order was submitted.
- The Sender displays information that the order was accepted.
- Finally, after a couple of seconds, the Receiver displays confirmation that the timeout message has been received.
- Open SQL Server Management Studio and go to the tenant databases. Verify that there are rows in saga state table (
dbo.
) and in the orders table (OrderLifecycleSagaData dbo.
) for each message sent.Orders
If used with a message transport that does not support native timeouts, timeout data is stored in a shared database so make sure to not include any sensitive information. Keep such information in saga data and only use timeouts as notifications.
Code walk-through
This sample contains three projects:
- Shared - A class library containing common code including messages definitions.
- Sender - A console application responsible for sending the initial
OrderSubmitted
message and processing the follow-upOrderAccepted
message. - Receiver - A console application responsible for processing the
OrderSubmitted
message, sendingOrderAccepted
message and randomly generating exceptions.
Sender project
The Sender does not store any data. It mimics the front-end system where orders are submitted by the users and passed via the bus to the back-end.
Receiver project
The Receiver mimics a back-end system. It is configured to use SQL persistence in multi-tenant mode.
Outbox cleanup
The built-in periodic Outbox cleanup does not work in a multi-tenant environment because it’s executed in the context of the shared database, while Outbox documents are stored in the tenants’ database.
The simplest way to ensure that the dispatched Outbox documents are removed is to use the RavenDB Document expiration feature. The following code ensures that tenants’ databases are created and the feature is enabled:
var id = $"MultiTenantSamples-{tenant}";
try
{
await documentStore.Maintenance.ForDatabase(id).SendAsync(new GetStatisticsOperation());
}
catch (DatabaseDoesNotExistException)
{
try
{
await documentStore.Maintenance.Server.SendAsync(new CreateDatabaseOperation(new DatabaseRecord(id)));
}
catch (ConcurrencyException)
{
}
}
Connecting to the tenant database
To allow for database isolation between the tenants the connection to the database needs to be created based on the message being processed. RavenDB persistence offers an extension point which allows for a customized database name to be used when opening a session.
persistence.SetMessageToDatabaseMappingConvention(headers =>
{
return headers.TryGetValue("tenant_id", out var tenantId)
? $"MultiTenantSamples-{tenantId}"
: "MultiTenantSamples";
});
The code above ensures that when the tenant_id
header is present, the session will point to the tenant database.
Propagating the tenant information downstream
In order to propagate the tenant information to the outgoing messages (including timeouts) this sample uses the same approach as the tenant information propagation sample: a pair of behaviors, one in the incoming pipeline and the other in the outgoing pipeline.