2

我希望在 Azure 上的 Redis 中为我们的多实例辅助角色创建一个分布式锁。我需要一种创建“关键部分”的方法,对于这些部分,一次只能有一个线程可以跨多个 Worker Role 实例进行访问。

我正在使用 StackExchange.Redis 客户端来执行此操作,而且,它已经实现了 transactional TakeLock\ReleaseLock 并且这个关于 SO 的答案让我很好地了解了要使用的模式以及有关如何创建锁的详细信息。

进一步阅读该主题,我还阅读了有关 distlock 的 Redis 文章,该文章描述了尝试实现分布式锁机制时基于故障转移的Redis 节点的弱点。

Azure Redis 缓存实现了主/从故障转移(除了基本层),这是否意味着我需要实现红锁模式以保证只有一件事会拥有锁?

另外,我想知道:

4

1 回答 1

3

I'm the author of the RedLock.net library that you linked in your question. The reason the documentation specifies connecting to independent redis instances is based on the reasoning in the Redis Distlock documentation. By forcing writes only to master nodes, we hopefully avoid the situation where a user might misconfigure Redlock to connect to multiple replicated hosts.

According to Azure Redis Cache 103 - Failover and Monitoring there is a load balancer in front of an Azure Redis Cache (at the standard tier and above) that ensures that you are always connected to the master.

Connecting to multiple redis instances (either replicated or not) should give a fairly good guarantee that no two processes end up running at the same time (moreso than a single replicated instance).

In order for another process to 'steal' the lock before the first had finished, more than half of the independent redis instances would need to lose their lock keys (e.g. by restarting without persistence), then have process two gain the lock before the timer in process one reacquired it during its extend timer.

于 2016-01-25T17:20:32.103 回答