How replicated Redis instances work with RedLock.net
masterThe Redlock algorithm is designed for independent Redis instances. However, RedLock.net supports replicated master/slave sets by treating each RedLockEndPoint as a single unit.
To use replicated instances, provide a RedLockEndPoint where the EndPoints property contains the list of servers in that replication set.
Risks of using replication:
- Write operations: All Redlock operations (Lock, Extend, Unlock) are writes and must be performed on the master. If a master fails and a slave is not automatically promoted, the instance becomes unusable.
- Race conditions: If a master fails after acquiring a lock but before propagating it to slaves, a newly promoted master might not have the lock, potentially allowing another process to acquire it simultaneously.
- Stale locks: If a master fails after releasing a lock but before propagation, the lock may persist in the slaves until the original expiry time is reached.
// Example: Using multiple independent replicated sets
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);