jaytwo.DistributedLocks.RedLock 0.1.0-beta-20251101220659

This is a prerelease version of jaytwo.DistributedLocks.RedLock.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package jaytwo.DistributedLocks.RedLock --version 0.1.0-beta-20251101220659
                    
NuGet\Install-Package jaytwo.DistributedLocks.RedLock -Version 0.1.0-beta-20251101220659
                    
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="jaytwo.DistributedLocks.RedLock" Version="0.1.0-beta-20251101220659" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="jaytwo.DistributedLocks.RedLock" Version="0.1.0-beta-20251101220659" />
                    
Directory.Packages.props
<PackageReference Include="jaytwo.DistributedLocks.RedLock" />
                    
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 jaytwo.DistributedLocks.RedLock --version 0.1.0-beta-20251101220659
                    
#r "nuget: jaytwo.DistributedLocks.RedLock, 0.1.0-beta-20251101220659"
                    
#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 jaytwo.DistributedLocks.RedLock@0.1.0-beta-20251101220659
                    
#: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=jaytwo.DistributedLocks.RedLock&version=0.1.0-beta-20251101220659&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=jaytwo.DistributedLocks.RedLock&version=0.1.0-beta-20251101220659&prerelease
                    
Install as a Cake Tool

jaytwo.DistributedLocks

Package
jaytwo.DistributedLocks NuGet Version NuGet Downloads
jaytwo.DistributedLocks.Postgres NuGet Version NuGet Downloads
jaytwo.DistributedLocks.MySql NuGet Version NuGet Downloads
jaytwo.DistributedLocks.RedLock NuGet Version NuGet Downloads
jaytwo.DistributedLocks.SqlServer NuGet Version NuGet Downloads

jaytwo.DistributedLocks is a .NET abstraction layer for distributed locking across multiple providers including in-memory, MySQL, PostgreSQL, and Redis (via RedLock), and SQL Server. It provides a consistent interface for acquiring and managing distributed locks in both sync and async contexts.

View on GitHub License: MIT

Features

  • Unified interface for multiple distributed lock backends
  • Support for both IDisposable and IAsyncDisposable
  • Customizable acquisition timeouts
  • Built-in health checks with HealthCheckAsync()
  • Available providers:
    • In-memory (single process)
    • MySQL
    • PostgreSQL
    • Redis (RedLock.NET)
    • SqlServer

Installation

Install the NuGet package(s) that match your backend:

PM> Install-Package jaytwo.DistributedLocks              # Core abstractions
PM> Install-Package jaytwo.DistributedLocks.MySql        # MySQL provider
PM> Install-Package jaytwo.DistributedLocks.Postgres     # PostgreSQL provider
PM> Install-Package jaytwo.DistributedLocks.RedLock      # Redis (RedLock.NET) provider
PM> Install-Package jaytwo.DistributedLocks.SqlServer    # SQL Server provider

Usage

Lock Namespaces

Lock namespaces let you isolate lock keys by environment and app so different services (or dev/test/prod) don't fight over the same "resource" name. Each provider qualifies your resource as "{LockNamespace}:{resource}" (or a hashed equivalent) before acquiring the lock—e.g., orders:charge-123—so "orders:charge-123" in stage never collides with "orders:charge-123" in dev.

If you don't specify one, helpers build a sensible default from your application name plus ASPNETCORE_ENVIRONMENT / DOTNET_ENVIRONMENT, and they'll log a warning if no environment is present.

Some backends also hash the qualified name to meet engine limits—MySQL and SQL Server convert to stable hex strings (for MySQL's shorter GET_LOCK name and SQL Server's sp_getapplock nvarchar limit), while Postgres maps the qualified name to a 64-bit integer advisory lock key.

Acquire and Release a Lock

Locks are automatically released when disposed:

await using (var myLock = await provider.CreateLockAsync("my-key"))
{
    if (!myLock.IsAcquired)
    {
        Console.WriteLine("Could not acquire lock");
        return;
    }

    // Critical section
}

Set a Custom Wait Timeout

await using var myLock = await provider.CreateLockAsync("my-key", waitTime: TimeSpan.FromSeconds(5));

Health Check

You can check the health of the underlying lock provider. This is useful for monitoring and diagnostics:

var healthCheckResult = await provider.HealthCheckAsync();

Background

RedLock.NET is great, but I don't love the polling nature of the RedLock algorithm, and many smaller apps aren't connected to a Redis instance. Most SQL-based systems already support advisory locking or built-in mutex mechanisms. This library provides a consistent API across different database backends, giving you the flexibility to use what you already have deployed.

When to Use

Use jaytwo.DistributedLocks when you need:

  • Coordination across distributed services or processes
  • A lightweight alternative to full orchestration frameworks
  • Backend flexibility without rewriting business logic
  • Locking via familiar SQL databases or Redis

Typical scenarios:

  • Distributed job scheduling
  • Coordinating background workers
  • Ensuring single execution of batch jobs
  • Preventing double-processing across instances

Made with ♥ by Jake — Licensed under the MIT License

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
0.1.0-beta-20251122220232 113 11/23/2025
0.1.0-beta-20251113173008 238 11/14/2025
0.1.0-beta-20251103210610 156 11/4/2025
0.1.0-beta-20251102111756 172 11/2/2025
0.1.0-beta-20251101220659 129 11/2/2025
0.1.0-beta-20250521200959 165 5/22/2025