Skip to content

Elders/RedLock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RedLock

A C# implementation of a distributed lock algorithm with Redis based on this documentation

Setup

  1. Register RedLock to your container
services.AddRedLock();
  1. Configure
{
    "RedLock": {
        "ConnectionString": "{YOUR_REDIS_CONNECTION_STRING}",
        "LockRetryCount": 2,
        "LockRetryDelay": "00:00:00.500",
        "ClockDriveFactor": 0.02
    }
}
Setting Comment Default
ConnectionString Your Redis connection string (required)
LockRetryCount Total amount of retries to aquire lock 1
LockRetryDelay Time to wait between retries 10 ms
ClockDriveFactor Factor to determine the clock drift. clock_drift = (lock_ttl_milliseconds * ClockDriveFactor) + 2. Adding 2 milliseconds to the drift to account for Redis expires precision (1 ms) plus the configured allowable drift factor. Read this for details. 0.01

Usage

var redlock = serviceProvider.GetRequiredService<IRedisLockManager>();

var resource = "resource_key";
if (await redlock.LockAsync(resource, TimeSpan.FromSeconds(2)))
{
    try
    {
        // do stuff
        if (await redlock.IsLockedAsync(resource))
        {
            // do more stuff
            if (await redlock.ExtendLockAsync(resource, TimeSpan.FromSeconds(2)))
            {
                // do even more stuff
            }
            else
            {
                // failed to extend resource lock
                // fallback
            }
        }
        else
        {
            // do something else
        }
    }
    finally
    {
        await redlock.UnlockAsync(resource);
    }
}
else
{
    // failed to lock resource
    // fallback
}