---
title: "Data subscriptions: Concurrent subscriptions"
description: "Run several workers on one RavenDB data subscription so they consume its documents in parallel, and manage their connections."
url: "https://docs.ravendb.net/7.2/data-subscriptions/concurrent-subscriptions"
version: "7.2"
language: "csharp"
---

# Data subscriptions: Concurrent subscriptions

> Client language: C#. Also available for [Java](https://docs.ravendb.net/7.2/data-subscriptions/concurrent-subscriptions.java.md), [Node.js](https://docs.ravendb.net/7.2/data-subscriptions/concurrent-subscriptions.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - 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](https://docs.ravendb.net/7.2/data-subscriptions/consuming-subscription.md#worker-strategies)
>   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](#connection-failure-and-reassignment) below.
>
> - In this article:
>   - [Defining concurrent workers](#defining-concurrent-workers)
>   - [Dropping a worker](#dropping-a-worker)
>   - [Connection failure and reassignment](#connection-failure-and-reassignment)
>   - [Syntax](#syntax)

## Defining concurrent workers {#defining-concurrent-workers}

To run workers concurrently, set each worker's strategy to `Concurrent`.  
To create each worker, pass a [SubscriptionWorkerOptions](https://docs.ravendb.net/7.2/data-subscriptions/consuming-subscription.md#classes) object whose `Strategy` is
`SubscriptionOpeningStrategy.Concurrent` to [GetSubscriptionWorker](https://docs.ravendb.net/7.2/data-subscriptions/consuming-subscription.md#methods).  
[Run the workers](https://docs.ravendb.net/7.2/data-subscriptions/consuming-subscription.md#running-the-worker) the same way you run single workers.

#### Example: Running two concurrent workers

```csharp
// 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 {#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

```csharp
// 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](https://docs.ravendb.net/7.2/data-subscriptions/maintenance-operations.md#dropping-connections).

## Connection failure and reassignment {#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.

> **Note**
>
> Plan for [at-least-once delivery](https://docs.ravendb.net/7.2/data-subscriptions/consuming-subscription.md#at-least-once-delivery).
> Make each worker's processing idempotent, so reprocessing a document the server reassigns has no
> unintended effect.

## Syntax {#syntax}

### Methods

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

```csharp
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:

```csharp
store.Subscriptions.DropSubscriptionWorker(worker2);
```

| Parameter | Type | Description |
| - | - | - |
| **worker** | `SubscriptionWorker<T>` | The worker to disconnect from the subscription. |
| **database** | `string` | The database where the subscription resides. If `null`, the document store's default database is used. |
| **token** | `CancellationToken` | A token used to cancel the asynchronous operation. |

| Return value | |
| - | - |
| `void` / `Task` | `DropSubscriptionWorker` returns once the worker is disconnected; `DropSubscriptionWorkerAsync` is its asynchronous version. |

### Classes

The strategy set on a worker's [SubscriptionWorkerOptions](https://docs.ravendb.net/7.2/data-subscriptions/consuming-subscription.md#classes)
`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](https://docs.ravendb.net/7.2/data-subscriptions/consuming-subscription.md#worker-strategies).

```csharp
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
}
```
