> For the complete documentation index, see [llms.txt](https://brightercommand.gitbook.io/paramore-brighter-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://brightercommand.gitbook.io/paramore-brighter-documentation/outbox-and-inbox/distributedlock/dynamodbdistributedlock.md).

# DynamoDB Distributed Lock

The DynamoDB locking provider implements Brighter's distributed lock on top of Amazon DynamoDB, so a single Outbox Sweeper and Archiver run when you scale out.

> **Reference** · Applies to **Brighter V10**

The DynamoDB locking provider implements Brighter's [distributed lock](/paramore-brighter-documentation/outbox-and-inbox/distributedlock.md) on top of Amazon DynamoDB, so a single [Outbox Sweeper](/paramore-brighter-documentation/outbox-and-inbox/brighteroutboxsupport.md#implicit-clear) and Archiver run when you scale out. It is a natural fit when you already use the [DynamoDB Outbox](/paramore-brighter-documentation/outbox-and-inbox/brighteroutboxsupport/dynamooutbox.md).

## DynamoDB Distributed Lock Package

Add the locking package for AWS SDK v4:

* **Paramore.Brighter.Locking.DynamoDB.V4**

Prefer the **V4** package for new projects. The earlier `Paramore.Brighter.Locking.DynamoDB` package targets AWS SDK v3, which is out of support on AWS, and exists only to aid migration.

The provider stores its locks in a DynamoDB table that you must create in advance (see [Provisioning](#dynamodb-distributed-lock-provisioning)).

## DynamoDB Distributed Lock Configuration

Configure the provider with `DynamoDbLockingProvider`, passing your `IAmazonDynamoDB` client and a `DynamoDbLockingProviderOptions`:

```csharp
using Paramore.Brighter.Locking.DynamoDB.V4;

new DynamoDbLockingProvider(
    dynamoDb,
    new DynamoDbLockingProviderOptions(
        lockTableName: "brighter-locks",
        leaseholderGroupId: "sweeper-group"));
```

`DynamoDbLockingProviderOptions` takes two required values in its constructor and exposes two optional settings:

| Option                | Type       | Default    | Description                                                                                                     |
| --------------------- | ---------- | ---------- | --------------------------------------------------------------------------------------------------------------- |
| `LockTableName`       | `string`   | `none`     | Names the DynamoDB table that holds the locks.                                                                  |
| `LeaseholderGroupId`  | `string`   | `none`     | Identifies the group of instances that share the lock; every instance that must coordinate uses the same value. |
| `LeaseValidity`       | `TimeSpan` | `60000 ms` | How long the lease is held before it expires automatically.                                                     |
| `ManuallyReleaseLock` | `bool`     | `false`    | Whether the provider releases the lock on completion rather than letting it expire.                             |

Both constructor parameters are required, and both properties are `init`-only: set them in the constructor call or in an object initialiser, not afterwards. Set `LeaseValidity` comfortably longer than a Sweeper or Archiver cycle.

## DynamoDB Distributed Lock Example

```csharp
using System;
using Amazon.DynamoDBv2;
using Microsoft.Extensions.DependencyInjection;
using Paramore.Brighter.DynamoDb.V4;
using Paramore.Brighter.Extensions.DependencyInjection;
using Paramore.Brighter.Locking.DynamoDB.V4;
using Paramore.Brighter.Outbox.DynamoDB.V4;
using Paramore.Brighter.Outbox.Hosting;

var dynamoDb = new AmazonDynamoDBClient();

services
    .AddSingleton<IAmazonDynamoDB>(dynamoDb)
    .AddBrighter()
    .AddProducers(opt =>
    {
        opt.Outbox = new DynamoDbOutbox(dynamoDb, new DynamoDbConfiguration { /* ... */ });
        opt.ConnectionProvider = typeof(DynamoDbUnitOfWork);
        opt.TransactionProvider = typeof(DynamoDbUnitOfWork);

        opt.DistributedLock = new DynamoDbLockingProvider(
            dynamoDb,
            new DynamoDbLockingProviderOptions("brighter-locks", "sweeper-group")
            {
                LeaseValidity = TimeSpan.FromMinutes(2)
            });
    })
    .UseOutboxSweeper(opt => { opt.BatchSize = 10; });
```

## DynamoDB Distributed Lock Provisioning

The lock table must exist before the provider runs. Create a table whose partition key matches the provider's lock items, with the name you pass as `LockTableName`. If you already provision a DynamoDB Outbox table, provision the lock table the same way (for example with the AWS SDK, CDK, or Terraform). Consider enabling DynamoDB's [time-to-live](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html) feature so expired lock items are cleaned up automatically.

## DynamoDB Distributed Lock Notes

* Use the same `LeaseholderGroupId` for every instance that must share the lock; a different value creates an independent lock.
* Keep `LeaseValidity` longer than a typical Sweeper or Archiver batch so the lease does not expire mid-run. See [Lease Expiry vs Manual Release](/paramore-brighter-documentation/outbox-and-inbox/distributedlock.md#lease-expiry-vs-manual-release).

## Further Reading

* [Distributed Lock](/paramore-brighter-documentation/outbox-and-inbox/distributedlock.md) — concepts and the full provider list
* [DynamoDB Outbox](/paramore-brighter-documentation/outbox-and-inbox/brighteroutboxsupport/dynamooutbox.md) — the matching Outbox
* [Outbox Support](/paramore-brighter-documentation/outbox-and-inbox/brighteroutboxsupport.md) — the Sweeper and Archiver


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://brightercommand.gitbook.io/paramore-brighter-documentation/outbox-and-inbox/distributedlock/dynamodbdistributedlock.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
