Simple RavenDB Persistence Usage

NuGet Package: NServiceBus.RavenDB (7-pre)
This page targets a pre-release version. Pre-releases are subject to change and samples are not guaranteed to be fully functional.
RavenDB's implementation of distributed transactions contains a bug that could cause an endpoint, in certain (rare) conditions, to lose data. If RavenDB is configured to enlist in distributed transactions, read DTC not supported for RavenDB Persistence.
Using RavenDB version 4 and higher in a cluster configuration with multiple nodes is not supported. For more information, read cluster configuration with multiple nodes not supported.

Code walk-through

This sample shows a simple Client + Server scenario.

  1. Client sends a StartOrder message to Server.
  2. Server starts an OrderSaga.
  3. OrderSaga requests a timeout with a CompleteOrder data.
  4. When the CompleteOrder timeout fires the OrderSaga publishes a OrderCompleted event.
  5. The Server then publishes a message that the client subscribes to.
  6. Client handles OrderCompleted event.

Raven Config

  1. In the RavenDB studio, create a database named RavenSimpleSample. RavenDB does not create the database automatically.
  2. Configure the endpoint to use RavenDB persistence. The URL in the Urls collection may need to be changed to match the database host/port in use.
var endpointConfiguration = new EndpointConfiguration("Samples.RavenDB.Server");
using (var documentStore = new DocumentStore
    Urls = new[] { "http://localhost:8080" },
    Database = "RavenSimpleSample",

    var persistence = endpointConfiguration.UsePersistence<RavenDBPersistence>();
    // Only required to simplify the sample setup
When the RavenDB DocumentStore is created by the user at endpoint configuration time it's important to dispose it, by calling the Dispose() method, before shutting down the endpoint process.

Order Saga Data

public class OrderSagaData :
    public Guid OrderId { get; set; }
    public string OrderDescription { get; set; }

Order Saga

public class OrderSaga :
    static ILog log = LogManager.GetLogger<OrderSaga>();

    protected override void ConfigureHowToFindSaga(SagaPropertyMapper<OrderSagaData> mapper)
        mapper.ConfigureMapping<StartOrder>(message => message.OrderId)
            .ToSaga(sagaData => sagaData.OrderId);

    public Task Handle(StartOrder message, IMessageHandlerContext context)
        var orderDescription = $"The saga for order {message.OrderId}";
        Data.OrderDescription = orderDescription;
        log.Info($"Received StartOrder message {Data.OrderId}. Starting Saga");

        var shipOrder = new ShipOrder
            OrderId = message.OrderId

        log.Info("Order will complete in 5 seconds");
        var timeoutData = new CompleteOrder
            OrderDescription = orderDescription

        return Task.WhenAll(
            RequestTimeout(context, TimeSpan.FromSeconds(5), timeoutData)

    public Task Timeout(CompleteOrder state, IMessageHandlerContext context)
        log.Info($"Saga with OrderId {Data.OrderId} completed");
        var orderCompleted = new OrderCompleted
            OrderId = Data.OrderId
        return context.Publish(orderCompleted);

Handler Using Raven Session

The handler access the same Raven ISession via ISessionProvider.

public class ShipOrderHandler :
    public Task Handle(ShipOrder message, IMessageHandlerContext context)
        var session = context.SynchronizedStorageSession.RavenSession();
        var orderShipped = new OrderShipped
            OrderId = message.OrderId,
            ShippingDate = DateTime.UtcNow,
        return session.StoreAsync(orderShipped, context.CancellationToken);

The Data in RavenDB

The data in RavenDB is stored in three different collections.

By default, this sample uses the Learning Transport, which has built-in support for timeouts and subscriptions. To see the data for timeouts and subscriptions, it's necessary to change the sample to a different transport that does not have these native features such as MSMQ.

The Saga Data

  • IContainSagaData.Id maps to the native RavenDB document Id.
  • IContainSagaData.Originator and IContainSagaData.OriginalMessageId map to simple properties.
  • Custom properties on the SagaData, in this case OrderDescription and OrderId, are also mapped to simple properties.

The Timeouts

  • The subscriber is stored in a Destination with the nested properties Queue and Machine.
  • The endpoint that initiated the timeout is stored in the OwningTimeoutManager property.
  • The connected saga ID is stored in a SagaId property.
  • The serialized data for the message is stored in a State property.
  • The scheduled timestamp for the timeout is stored in a Time property.
  • Any headers associated with the timeout are stored in an array of key value pairs.

The Subscriptions

Note that the message type maps to multiple subscriber endpoints.

  • The Subscription message type and version are stored in the MessageType property.
  • The list of subscribers is stored in a array of objects each containing Queue and MachineName properties.

The Handler Stored data

Related Articles

  • RavenDB Persistence
  • Sagas
    NServiceBus uses event-driven architecture to include fault-tolerance and scalability in long-term business processes.

Last modified