For the complete documentation index, see llms.txt. This page is also available as Markdown.

Postgres Distributed Lock

The Postgres locking provider implements Brighter's distributed lock using PostgreSQL advisory locks, so a single Outbox Sweeper and Archiver run when you scale out. It pairs naturally with the Postgres Outbox.

Package

  • Paramore.Brighter.Locking.PostgresSql

Advisory locks are a built-in PostgreSQL feature, so there is no table to provision — see Provisioning.

Configuration

Configure the provider with PostgresLockingProvider, passing a PostgresLockingProviderOptions that carries the connection string:

new PostgresLockingProvider(
    new PostgresLockingProviderOptions(
        connectionString: "Host=localhost;Database=orders;Username=app;Password=secret"));
Setting
Type
Default
Description

ConnectionString

string

(required)

The connection string used to open the session that holds the advisory lock.

Example

const string connectionString = "Host=localhost;Database=orders;Username=app;Password=secret";

services
    .AddBrighter()
    .AddProducers(opt =>
    {
        opt.Outbox = /* your Postgres Outbox */;
        opt.ConnectionProvider = typeof(PostgreSqlConnectionProvider);
        opt.TransactionProvider = typeof(PostgreSqlTransactionProvider);

        opt.DistributedLock = new PostgresLockingProvider(
            new PostgresLockingProviderOptions(connectionString));
    })
    .UseOutboxSweeper(opt => { opt.BatchSize = 10; });

Provisioning

None. PostgreSQL advisory locks are session-scoped locks that live in the server's memory, so the provider needs no table or schema. The lock is held for the lifetime of the database session and is released when that session closes — including if the holding instance crashes and its connection drops.

Notes

  • Because the lock is tied to a session rather than a timed lease, there is no LeaseValidity to tune. Recovery after a crash happens as soon as the dropped connection is detected by the server.

  • Point the provider at the same PostgreSQL instance as your Outbox so the Sweeper, Archiver, and lock all share infrastructure.

Further Reading

Last updated

Was this helpful?