---
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: "nodejs"
---

# Data subscriptions: Concurrent subscriptions

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/7.2/data-subscriptions/concurrent-subscriptions.md), [Java](https://docs.ravendb.net/7.2/data-subscriptions/concurrent-subscriptions.java.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.nodejs.md#worker-strategies)
>   to `"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 [subscription worker options](https://docs.ravendb.net/7.2/data-subscriptions/consuming-subscription.nodejs.md#classes) object whose `strategy` is
`"Concurrent"` to [getSubscriptionWorker](https://docs.ravendb.net/7.2/data-subscriptions/consuming-subscription.nodejs.md#methods).  
Run the workers the same way you run single workers, by attaching a handler to each worker's `batch` event.

#### Example: Running two concurrent workers

```javascript
// Two workers, both using the Concurrent strategy,
// so they connect to the "All Orders" subscription at the same time
const options = {
    subscriptionName: "All Orders",
    strategy: "Concurrent",
    maxDocsPerBatch: 20
};

const worker1 = documentStore.subscriptions.getSubscriptionWorker(options);
const worker2 = documentStore.subscriptions.getSubscriptionWorker(options);

// Run both workers. The server assigns each one a different batch of documents.
worker1.on("batch", (batch, callback) => {
    try {
        for (const item of batch.items) {
            processOrder(item.result); // your processing logic
        }
        callback();
    } catch (err) {
        callback(err);
    }
});

worker2.on("batch", (batch, callback) => {
    try {
        for (const item of batch.items) {
            processOrder(item.result); // your processing logic
        }
        callback();
    } catch (err) {
        callback(err);
    }
});
```

## 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

```javascript
// Disconnect worker2; its documents become available to the other workers
await documentStore.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.nodejs.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.nodejs.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.

```javascript
dropSubscriptionWorker(worker);
dropSubscriptionWorker(worker, database);
```

Usage:

```javascript
await documentStore.subscriptions.dropSubscriptionWorker(worker2);
```

| Parameter | Type | Description |
| - | - | - |
| **worker** | `SubscriptionWorker` | The worker to disconnect from the subscription. |
| **database** | `string` | The database where the subscription resides. If not specified, the document store's default database is used. |

| Return value | |
| - | - |
| `Promise<void>` | Resolves once the worker is disconnected. |

### Classes

The strategy set on the subscription worker options `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.nodejs.md#worker-strategies).

```javascript
// SubscriptionOpeningStrategy values:
"OpenIfFree"   // Connect only if no other worker is connected
"TakeOver"     // Take over the connection from the current worker
"WaitForFree"  // Wait for the current worker to disconnect, then take over
"Concurrent"   // Connect concurrently with the other workers
```
