Faactory.Leases.Redis 1.0.3

dotnet add package Faactory.Leases.Redis --version 1.0.3
                    
NuGet\Install-Package Faactory.Leases.Redis -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Faactory.Leases.Redis" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Faactory.Leases.Redis" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Faactory.Leases.Redis" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Faactory.Leases.Redis --version 1.0.3
                    
#r "nuget: Faactory.Leases.Redis, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Faactory.Leases.Redis@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Faactory.Leases.Redis&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Faactory.Leases.Redis&version=1.0.3
                    
Install as a Cake Tool

Distributed Coordination

Minimal abstractions for coordinating work across multiple instances.

Provides two independent primitives:

  • Distributed leases: coordinate who runs
  • Distributed timers: coordinate when work runs

They can be used separately or combined.


Concepts

Lease

A lease represents temporary ownership of a resource.

Used for:

  • leader election
  • singleton work
  • coordinated execution across instances

Timer

A timer represents a shared periodic schedule.

Used for:

  • periodic background tasks
  • coordinated execution across instances
  • restart-safe scheduling without cron

Behavior:

  • first execution occurs after the configured period
  • subsequent executions occur at the same interval, aligned to the initial schedule
  • timer state survives restarts while the backend persists the timer key
  • all instances align on the same schedule

Registration

Depending on the underlying implementation, different dependencies are required.

Redis

To use Redis, install both Redis and the distributed coordination packages:

dotnet add package StackExchange.Redis
dotnet add package Faactory.Leases.Redis

Redis implementations require IConnectionMultiplexer to be registered.

Leases
services.AddRedisDistributedLeases( options =>
{
    options.KeyPrefix = "leases";
} );
Timers
services.AddRedisDistributedTimers( options =>
{
    options.KeyPrefix = "timers";
} );

NATS

To use NATS, install both NATS and the distributed coordination packages:

dotnet add package NATS.Client
dotnet add package Faactory.Leases.NATS

NATS implementations require NatsClient to be registered.

Leases
services.AddNatsDistributedLeases( options =>
{
    options.BucketName = "leases";
    options.BucketMarkerTtl = TimeSpan.FromMinutes( 5 );
    options.KeyPrefix = "leases";
} );
Timers
services.AddNatsDistributedTimers( options =>
{
    options.BucketName = "timers";
    options.KeyPrefix = "timers";
} );

Usage

Acquiring a lease

public sealed class MyService( IDistributedLeaseStore leaseStore )
{
    public async Task ExecuteAsync( CancellationToken cancellationToken )
    {
        var leaseHandle = await leaseStore.TryAcquireAsync(
            "my-lease",
            Guid.NewGuid().ToString(),
            TimeSpan.FromSeconds( 30 ),
            cancellationToken
        );

        if ( leaseHandle is null )
        {
            // failed to acquire lease
            return;
        }

        await using var lease = new DistributedLease(
            leaseStore,
            leaseHandle,
            TimeSpan.FromSeconds( 10 )
        );

        // do work while lease is held
    }
}

Periodic execution

public sealed class MyService( IDistributedTimerStore timerStore )
{
    public async Task ExecuteAsync( CancellationToken cancellationToken )
    {
        var timer = new DistributedTimer(
            timerStore,
            "my-timer",
            TimeSpan.FromMinutes( 10 )
        );

        while ( await timer.WaitForNextTickAsync( cancellationToken ) )
        {
            // periodic work
        }
    }
}

Combining leases and timers

public sealed class MyService(
    IDistributedLeaseStore leaseStore,
    IDistributedTimerStore timerStore
)
{
    public async Task ExecuteAsync( CancellationToken cancellationToken )
    {
        var timer = new DistributedTimer(
            timerStore,
            "my-timer",
            TimeSpan.FromMinutes( 10 )
        );

        while ( await timer.WaitForNextTickAsync( cancellationToken ) )
        {
            // non-leader work can be done here (if any)

            var leaseHandle = await leaseStore.TryAcquireAsync(
                "my-lease",
                Guid.NewGuid().ToString(),
                TimeSpan.FromSeconds( 30 ),
                cancellationToken
            );

            if ( leaseHandle is null )
            {
                continue;
            }

            await using var lease = new DistributedLease(
                leaseStore,
                leaseHandle,
                TimeSpan.FromSeconds( 10 )
            );

            // leader-only periodic work
        }
    }
}

Convenience APIs

The library also provides optional convenience base classes for hosted services.

LeasedService

Convenience base class for leased services with opiniated defaults. Handles lease acquisition and renewal, allowing you to focus on the leader-only work.

public sealed class MyService( ILoggerFactory loggerFactory, IDistributedLeaseStore leaseStore )
    : LeasedService( loggerFactory, leaseStore )
{
    protected override async Task ExecuteLeaderAsync( CancellationToken cancellationToken )
    {
        // leader-only work
    }
}

PeriodicLeasedService

Convenience base class for periodic leased services with opiniated defaults. Handles lease acquisition, renewal, and periodic execution, allowing you to focus on the leader-only work.

public sealed class MyService(
    ILoggerFactory loggerFactory,
    IDistributedLeaseStore leaseStore,
    IDistributedTimerStore timerStore
)
    : PeriodicLeasedService( loggerFactory, leaseStore, timerStore )
{
    protected override TimeSpan ExecutionInterval
        => TimeSpan.FromMinutes( 10 );

    protected override Task RunAsync( CancellationToken cancellationToken )
    {
        // leader-only periodic work

        return Task.CompletedTask;
    }
}

Redis Implementation

Leases

  • SET NX PX for acquisition
  • Lua scripts for safe renew/release
  • owner-checked operations

Timers

  • Redis TTL defines the timer window
  • Lua script creates the timer if missing
  • key expiration drives the next tick

NATS Implementation

Uses NATS JetStream KV storage for leases and timers.

Leases rely on bucket markers, which require NATS Server v2.11+.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 209 8/12/2026
1.0.2 121 5/14/2026
1.0.1 109 5/13/2026
1.0.0 104 5/13/2026
0.3.1 101 5/13/2026
0.3.0 110 5/12/2026
0.3.0-preview1 101 5/8/2026
0.2.0 146 4/29/2026
0.1.0 120 4/24/2026