Skip to main content

Data subscriptions: Concurrent subscriptions

  • With concurrent subscriptions, multiple workers can consume the same subscription simultaneously.

  • The server assigns each worker a different batch of documents to process.

  • By processing different batches in parallel, multiple workers can significantly accelerate the consumption of the subscription's contents.

  • To run workers concurrently, set their strategy to SubscriptionOpeningStrategy.Concurrent.
    Other strategies (OpenIfFree, TakeOver, WaitForFree) let only one worker consume a subscription at a time.

  • If a concurrent worker disconnects, the server may reassign the documents this worker was processing to other available workers consuming the same subscription.
    A document can reach more than one worker this way.
    See Connection failure and reassignment below.

  • In this article:

Defining concurrent workers​

To run workers concurrently, set each worker's strategy to Concurrent.
To create each worker, pass a SubscriptionWorkerOptions object whose Strategy is SubscriptionOpeningStrategy.Concurrent to GetSubscriptionWorker.
Run the workers the same way you run single workers.

Example: Running two concurrent workers​

// Two workers, both using the Concurrent strategy,
// so they connect to the "All Orders" subscription at the same time
var worker1 = store.Subscriptions.GetSubscriptionWorker<Order>(
new SubscriptionWorkerOptions("All Orders")
{
Strategy = SubscriptionOpeningStrategy.Concurrent,
MaxDocsPerBatch = 20
});

var worker2 = store.Subscriptions.GetSubscriptionWorker<Order>(
new SubscriptionWorkerOptions("All Orders")
{
Strategy = SubscriptionOpeningStrategy.Concurrent,
MaxDocsPerBatch = 20
});

// Run both workers. The server assigns each one a different batch of documents.
var subscriptionRuntimeTask1 = worker1.Run(batch =>
{
foreach (var item in batch.Items)
{
ProcessOrder(item.Result); // your processing logic
}
});

var subscriptionRuntimeTask2 = worker2.Run(batch =>
{
foreach (var item in batch.Items)
{
ProcessOrder(item.Result); // your processing logic
}
});

Dropping a worker​

To forcefully disconnect a worker from its subscription, pass the worker object to Subscriptions.DropSubscriptionWorker.
The documents the worker was processing then become available for reassignment to the remaining workers.

Example: Dropping one worker​

// Disconnect worker2; its documents become available to the other workers
store.Subscriptions.DropSubscriptionWorker(worker2);

To disconnect every worker on a subscription at once, by subscription name rather than by a specific worker, use DropConnection.
See Maintenance operations.

Connection failure and reassignment​

When a concurrent worker's connection ends unexpectedly, the server may reassign the documents the worker was processing to the subscription's other available workers.
A worker that reconnects to the subscription after a failure receives a new batch, which is not guaranteed to hold the documents it was processing before.

Because a worker can disconnect before acknowledging the documents it already handled, the same document can be processed more than once: first by the worker that dropped, and again by the worker the document is reassigned to.

Plan for at-least-once delivery. Make each worker's processing idempotent, so reprocessing a document the server reassigns has no unintended effect.

Syntax​

Methods​

Forcefully disconnect a specific worker from the subscription it is connected to.

void DropSubscriptionWorker<T>(SubscriptionWorker<T> worker,
string database = null) where T : class;

Task DropSubscriptionWorkerAsync<T>(SubscriptionWorker<T> worker,
string database = null, CancellationToken token = default) where T : class;

Usage:

store.Subscriptions.DropSubscriptionWorker(worker2);

ParameterTypeDescription
workerSubscriptionWorker<T>The worker to disconnect from the subscription.
databasestringThe database where the subscription resides. If null, the document store's default database is used.
tokenCancellationTokenA token used to cancel the asynchronous operation.
Return value
void / TaskDropSubscriptionWorker returns once the worker is disconnected; DropSubscriptionWorkerAsync is its asynchronous version.

Classes​

The strategy set on a worker's SubscriptionWorkerOptions Strategy property. Concurrent is the value that lets multiple workers consume the subscription at once; the other values allow a single worker at a time, covered in Worker strategies.

public enum SubscriptionOpeningStrategy
{
// Connect only if no other worker is connected
OpenIfFree,

// Take over the connection from the current worker
TakeOver,

// Wait for the current worker to disconnect, then take over
WaitForFree,

// Connect concurrently with the other workers
Concurrent
}

In this article