Soenneker.Redis.Semaphores 4.0.12

Prefix Reserved
dotnet add package Soenneker.Redis.Semaphores --version 4.0.12
                    
NuGet\Install-Package Soenneker.Redis.Semaphores -Version 4.0.12
                    
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="Soenneker.Redis.Semaphores" Version="4.0.12" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Redis.Semaphores" Version="4.0.12" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Redis.Semaphores" />
                    
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 Soenneker.Redis.Semaphores --version 4.0.12
                    
#r "nuget: Soenneker.Redis.Semaphores, 4.0.12"
                    
#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 Soenneker.Redis.Semaphores@4.0.12
                    
#: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=Soenneker.Redis.Semaphores&version=4.0.12
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Redis.Semaphores&version=4.0.12
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

alternate text is missing from this package README image Soenneker.Redis.Semaphores

A utility library providing distributed semaphores backed by Redis.

Installation

dotnet add package Soenneker.Redis.Semaphores

Registration

services.AddRedisSemaphoreAsScoped();

AddRedisSemaphoreAsSingleton() is also available. Both registrations add Soenneker.Redis.Util and its dependencies.

Usage

Try to acquire a permit without waiting:

RedisSemaphoreHandle? handle = await semaphore.TryAcquire(
    "imports",
    maxCount: 4,
    cancellationToken: cancellationToken);

if (handle is null)
    return;

await using (handle)
{
    await RunImport(cancellationToken);
}

Or wait until a permit becomes available:

await using RedisSemaphoreHandle handle = await semaphore.Acquire(
    "imports",
    maxCount: 4,
    cancellationToken: cancellationToken);

Permits automatically renew while their handles are alive, so the same API supports jobs lasting milliseconds, minutes, or hours. The default one-minute lease measures runner health rather than expected job duration. Disposing the handle stops renewal and releases only its permit. If a runner crashes, renewal stops and Redis recovers the abandoned permit when its lease expires.

Pass the handle's loss token into cooperative work so it stops if Redis ownership can no longer be maintained:

await using RedisSemaphoreHandle handle = await semaphore.Acquire(
    "automation-jobs",
    maxCount: 10,
    cancellationToken: cancellationToken);

using CancellationTokenSource jobCancellation = CancellationTokenSource.CreateLinkedTokenSource(
    cancellationToken,
    handle.PermitLostToken);

await RunJob(jobCancellation.Token);

Lease and retry behavior can be tuned when necessary:

var options = new RedisSemaphoreOptions
{
    LeaseDuration = TimeSpan.FromMinutes(1),
    RenewalInterval = TimeSpan.FromSeconds(20),
    RenewalSafetyMargin = TimeSpan.FromSeconds(10),
    RetryInterval = TimeSpan.FromMilliseconds(100)
};

await using RedisSemaphoreHandle handle = await semaphore.Acquire(
    "automation-jobs",
    maxCount: 10,
    options,
    cancellationToken);

Renewal is owner-safe: Soenneker.Redis.Util uses a Redis transaction to compare the unique permit token and extend its expiration atomically. An expired handle cannot renew or release a permit subsequently acquired by another runner. No Lua scripts are used.

All callers for a semaphore name must use the same maxCount. If PermitLostToken is canceled, the runner should stop the job before the lease can be acquired elsewhere. Semaphore names cannot contain { or } because the library uses Redis hash tags to keep all permit keys for a semaphore in the same cluster slot.

Capacity visibility

Query the distributed permit usage at any time:

RedisSemaphoreStatus status = await semaphore.GetStatus(
    "automation-jobs",
    maxCount: 10,
    cancellationToken);

// status.MaxCount       = 10
// status.AcquiredCount  = permits currently held across all runners
// status.AvailableCount = permits currently available
// status.Utilization    = fraction from 0 through 1
// status.IsFull
// status.IsAvailable

The result is a point-in-time snapshot. Permit state can change immediately after it is read.

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 (1)

Showing the top 1 NuGet packages that depend on Soenneker.Redis.Semaphores:

Package Downloads
Soenneker.Redis.WorkQueue

A durable, partition-aware Redis work queue with scheduling, leases, retries, deduplication, and round-robin fairness.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.12 0 8/19/2026
4.0.11 0 8/18/2026
4.0.10 0 8/18/2026
4.0.9 0 8/18/2026
4.0.8 44 8/18/2026
4.0.7 44 8/18/2026
4.0.6 44 8/18/2026
4.0.5 111 8/14/2026
4.0.4 75 8/13/2026
4.0.3 52 8/13/2026