Distributed Locks with Redis

Redis 分布式锁

A distributed lock pattern with Redis 使用 Redis 的分布式锁模式

Distributed locks are a very useful primitive in many environments where different processes must operate with shared resources in a mutually exclusive way. 在许多环境中,分布式锁是非常有用的原语,在这些环境中,不同的进程必须以互斥的方式操作共享资源。

There are a number of libraries and blog posts describing how to implement a DLM (Distributed Lock Manager) with Redis, but every library uses a different approach, and many use a simple approach with lower guarantees compared to what can be achieved with slightly more complex designs. 有许多库和博客文章描述了如何使用 Redis 实现 DLM(分布式锁管理器),但每个库都使用不同的方法,并且许多库使用简单的方法,与稍微复杂一点的方法相比,其保证较低设计。

This page describes a more canonical algorithm to implement distributed locks with Redis. We propose an algorithm, called Redlock, which implements a DLM which we believe to be safer than the vanilla single instance approach. We hope that the community will analyze it, provide feedback, and use it as a starting point for the implementations or more complex or alternative designs. 本页描述了使用 Redis 实现分布式锁的更规范的算法。我们提出了一种名为 Redlock 的算法,它实现了 DLM,我们认为它比普通的单实例方法更安全。我们希望社区能够对其进行分析、提供反馈,并将其用作实现或更复杂或替代设计的起点。

Implementations 实施

Before describing the algorithm, here are a few links to implementations already available that can be used for reference. 在描述算法之前,这里有一些已经可用的实现的链接,可供参考。