All Projects → samcook → Redlock.net

samcook / Redlock.net

Licence: mit
An implementation of the Redlock algorithm in C#

Projects that are alternatives of or similar to Redlock.net

Lada Cache
A Redis based, fully automated and scalable database cache layer for Laravel
Stars: ✭ 424 (-8.03%)
Mutual labels:  redis
Vedis
An Embedded Implementation of Redis
Stars: ✭ 436 (-5.42%)
Mutual labels:  redis
Elasticell
Elastic Key-Value Storage With Strong Consistency and Reliability
Stars: ✭ 453 (-1.74%)
Mutual labels:  redis
Cookbook
🎉🎉🎉JAVA高级架构师技术栈==任何技能通过 “刻意练习” 都可以达到融会贯通的境界,就像烹饪一样,这里有一份JAVA开发技术手册,只需要增加自己练习的次数。🏃🏃🏃
Stars: ✭ 428 (-7.16%)
Mutual labels:  redis
Redis
Vapor provider for RediStack
Stars: ✭ 434 (-5.86%)
Mutual labels:  redis
Logstash Logger
Ruby logger that writes logstash events
Stars: ✭ 442 (-4.12%)
Mutual labels:  redis
Framework Learning
计算机学习资料(Java , Jvm , Linux , Mysql , Netty , Redis , Netty , Spring , SpringBoot , Mybatis , Rabbitmq ,计算机网络 , 数据结构与算法 , 设计模式 )Github网页阅读:https://guang19.github.io/framework-learning , Gitee网页版阅读: https://qsjzwithguang19forever.gitee.io/framework-learning
Stars: ✭ 416 (-9.76%)
Mutual labels:  redis
Api Boot
“ ApiBoot”是为接口服务而生的,基于“ SpringBoot”完成扩展和自动配置,内部封装了一系列的开箱即用Starters。
Stars: ✭ 460 (-0.22%)
Mutual labels:  redis
Redis Admin
redis client tool,redis web client,redis web ui,spring-boot support
Stars: ✭ 436 (-5.42%)
Mutual labels:  redis
Redis source annotation
redis 3.2.8 的源码注释
Stars: ✭ 452 (-1.95%)
Mutual labels:  redis
Redis Plus Plus
Redis client written in C++
Stars: ✭ 428 (-7.16%)
Mutual labels:  redis
Opserver
Stack Exchange's Monitoring System
Stars: ✭ 4,126 (+795.01%)
Mutual labels:  redis
Centrifuge
Real-time messaging library for Go with scalability in mind
Stars: ✭ 446 (-3.25%)
Mutual labels:  redis
Pika
Pika is a nosql compatible with redis, it is developed by Qihoo's DBA and infrastructure team
Stars: ✭ 4,439 (+862.91%)
Mutual labels:  redis
Bdp Dataplatform
大数据生态解决方案数据平台:基于大数据、数据平台、微服务、机器学习、商城、自动化运维、DevOps、容器部署平台、数据平台采集、数据平台存储、数据平台计算、数据平台开发、数据平台应用搭建的大数据解决方案。
Stars: ✭ 456 (-1.08%)
Mutual labels:  redis
Permissions2
🔐 Middleware for keeping track of users, login states and permissions
Stars: ✭ 423 (-8.24%)
Mutual labels:  redis
Spring Boot Study
SpringBoot框架源码实战(已更新到springboot2版本实现)~基本用法,Rest,Controller,事件监听,连接数据库MySQL,jpa,redis集成,mybatis集成(声明式与xml两种方式~对应的添删查改功能),日志处理,devtools配置,拦截器用法,资源配置读取,测试集成,Web层实现请求映射,security安全验证,rabbitMq集成,kafka集成,分布式id生成器等。项目实战:https://github.com/hemin1003/yfax-parent 已投入生产线上使用
Stars: ✭ 440 (-4.56%)
Mutual labels:  redis
Testcontainers Spring Boot
Container auto-configurations for spring-boot based integration tests
Stars: ✭ 460 (-0.22%)
Mutual labels:  redis
Onemall
芋道 mall 商城,基于微服务的思想,构建在 B2C 电商场景下的项目实战。核心技术栈,是 Spring Boot + Dubbo 。未来,会重构成 Spring Cloud Alibaba 。
Stars: ✭ 5,312 (+1052.28%)
Mutual labels:  redis
Middleware development learning
中间件、高性能服务器、分布式存储等(redis、memcache、nginx、大容量redis pika、rocksdb、mongodb、wiredtiger存储引擎、高性能代理中间件)二次开发、性能优化,逐步整理文档说明并配合demo指导--每周末定时更新2-3篇技术文章及程序demo--(技术交流QQ群:568892619)
Stars: ✭ 449 (-2.6%)
Mutual labels:  redis

RedLock.net

Build status GitHub NuGet GitHub release GitHub release

An implementation of the Redlock distributed lock algorithm in C#.

Makes use of the excellent StackExchange.Redis library.

Distributed locks are useful for ensuring only one process is using a particular resource at any given time (even if the processes are running on different machines).

RedLock.net is available using NuGet - search for RedLock.net.

Note: RedLock 2.2.0+ requires StackExchange.Redis 2.0+ - if you need to use StackExchange.Redis 1.x please continue to use RedLock.net 2.1.0.

Usage

Construct a RedLockFactory, passing in a set of Redis endpoints. Each endpoint passed to the constructor should be independent (i.e. not replicated masters/slaves). See below for more information on using RedLock.net with replicated instances.

You should keep hold of the RedLockFactory and reuse it in your application. Each instance maintains its own connections with the configured Redis instances. Remember to dispose it when your app shuts down.

With the factory, create a RedLock in a using block, making sure to check that that the lock IsAcquired inside the block before performing any actions. A lock is acquired if RedLock can set a lock key in more than half of the redis instances.

Internally RedLock has a timer that automatically tries to keep the redis lock key alive. In the worst case of a process crashing without releasing the lock, it will automatically be expired by redis after the specified expiry time.

On app init:

To have RedLock maintain its own redis connections:

var endPoints = new List<RedLockEndPoint>
{
	new DnsEndPoint("redis1", 6379),
	new DnsEndPoint("redis2", 6379),
	new DnsEndPoint("redis3", 6379)
};
var redlockFactory = RedLockFactory.Create(endPoints);

To have RedLock use existing redis connections:

var existingConnectionMultiplexer1 = ConnectionMultiplexer.Connect("redis1:6379");
var existingConnectionMultiplexer2 = ConnectionMultiplexer.Connect("redis2:6379");
var existingConnectionMultiplexer3 = ConnectionMultiplexer.Connect("redis3:6379");

var multiplexers = new List<RedLockMultiplexer>
{
	existingConnectionMultiplexer1,
	existingConnectionMultiplexer2,
	existingConnectionMultiplexer3
};
var redlockFactory = RedLockFactory.Create(multiplexers);

If you require more detailed configuration for the redis instances you are connecting to (e.g. password, SSL, connection timeout, redis database, key format), you can use the RedLockEndPoint or RedLockMultiplexer classes.

When you want to lock on a resource (giving up immediately if the lock is not available):

var resource = "the-thing-we-are-locking-on";
var expiry = TimeSpan.FromSeconds(30);

using (var redLock = await redlockFactory.CreateLockAsync(resource, expiry)) // there are also non async Create() methods
{
	// make sure we got the lock
	if (redLock.IsAcquired)
	{
		// do stuff
	}
}
// the lock is automatically released at the end of the using block

When you want to lock on a resource (blocking and retrying every retry seconds until the lock is available, or wait seconds have passed):

var resource = "the-thing-we-are-locking-on";
var expiry = TimeSpan.FromSeconds(30);
var wait = TimeSpan.FromSeconds(10);
var retry = TimeSpan.FromSeconds(1);

// blocks until acquired or 'wait' timeout
using (var redLock = await redlockFactory.CreateLockAsync(resource, expiry, wait, retry)) // there are also non async Create() methods
{
	// make sure we got the lock
	if (redLock.IsAcquired)
	{
		// do stuff
	}
}
// the lock is automatically released at the end of the using block

You can also pass a CancellationToken to the blocking CreateLock methods if you want to be able to cancel the blocking wait.

On app shutdown:

redlockFactory.Dispose();

Azure Redis Cache:

If you are connecting to Azure Redis Cache, use the following configuration settings:

var azureEndPoint = new RedLockEndPoint
{
	EndPoint = new DnsEndPoint("YOUR_CACHE.redis.cache.windows.net", 6380),
	Password = "YOUR_ACCESS_KEY",
	Ssl = true
};

Usage with replicated instances:

The Redlock algorithm is designed to be used with multiple independent Redis instances (see http://redis.io/topics/distlock#why-failover-based-implementations-are-not-enough for more detail on why this is the case).

However, since RedLock.net 1.7.3 there is support for replicated master/slave instances. To do so, configure RedLock using the RedLockEndPoint class, providing the replicated redis instances with the EndPoints property. Each RedLockEndPoint that is passed to the RedLockFactory constructor will be treated as a single unit as far as the RedLock algorithm is concerned.

If you have multiple independent sets of replicated Redis instances, you can use those with RedLock.net in the same way you would use multiple non-replicated independent Redis instances:

var redlockEndPoints = new List<RedLockEndPoint>
{
	new RedLockEndPoint
	{
		EndPoints =
		{
			new DnsEndPoint("replicatedset1-server1", 6379),
			new DnsEndPoint("replicatedset1-server2", 6379),
			new DnsEndPoint("replicatedset1-server3", 6379)
		}
	},
	new RedLockEndPoint
	{
		EndPoints =
		{
			new DnsEndPoint("replicatedset2-server1", 6379),
			new DnsEndPoint("replicatedset2-server2", 6379),
			new DnsEndPoint("replicatedset2-server3", 6379)
		}
	},
	new RedLockEndPoint
	{
		EndPoint = new DnsEndPoint("independent-server", 6379)
	}
};

var redlockFactory = RedLockFactory.Create(redlockEndPoints);

or, if you have existing redis connections:

var existingConnectionMultiplexer1 = ConnectionMultiplexer.Connect("replicatedset1-server1:6379,replicatedset1-server2:6379,replicatedset1-server3:6379");
var existingConnectionMultiplexer2 = ConnectionMultiplexer.Connect("replicatedset2-server1:6379,replicatedset2-server2:6379,replicatedset2-server3:6379");
var existingConnectionMultiplexer3 = ConnectionMultiplexer.Connect("independent-server:6379");

var multiplexers = new List<RedLockMultiplexer>
{
	existingConnectionMultiplexer1,
	existingConnectionMultiplexer2,
	existingConnectionMultiplexer3
};
var redlockFactory = RedLockFactory.Create(multiplexers);

Considerations when using replicated instances

Using replicated instances is not the suggested way to use RedLock, however if your environment is configured that way and you are aware of the potential risks it is possible to configure.

Since all operations that RedLock.net performs in Redis (Lock, Extend and Unlock) require writing, they can only be performed on the master. If your replicated Redis instances are not configured to automatically promote one of the slaves to master in the case of the master becoming inaccessible, the replicated instances will be unable to be used by RedLock until there is manual intervention to reinstate a master.

There is the potential for a master to become inaccessible after acquiring a lock, but before that lock is propogated to the replicated slaves. If one of the slaves is then promoted to master, the lock will not have been acquired within Redis, and there is the possibility that another process could also acquire the lock, resulting in two processes running within the lock section at the same time. Running with multiple independent instances (or multiple independent replicated instances) should mitigate this somewhat, as the RedLock quorum is still required before another process can acquire the lock.

There is the potential for a master to become inaccessible after releasing a lock, but before the lock removal is propogated to the replicated slaves. This will result in the lock continuing to be held until the Redis key expires after the specified expiry time.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].