用 redis 做分布式锁这种骚操作是怎么流行起来的?

2021 年 3 月 8 日
 nagatoism
Redis 根本就不是设计出来干这个的,为什么网上这么多垃圾文章讲这个?

老老实实用 ZK 或者 ETCD 不好吗?

感觉大量的垃圾技术文章的内容都可以总结为三句

1 我没读文档瞎 jb 用,结果不符合预期。
2 出 bug 了受不了才老老实实读文档,原来是我用的不对。
3 按照文章老老实实的写代码,终于对了。


看多了会觉得中文技术社区真是垃圾堆。
25939 次点击
所在节点    程序员
159 条回复
ryd994
2021 年 3 月 8 日
@emSaVya
@monsterxx03
如果你们说的系统时钟问题是指系统时间可能前后调整,那么你们的担心是不必要的。CLOCK_MONOTONIC 是基于硬件时间,通常是开机时间,调整系统时间不影响。
开机时间够用了,因为你不是真的在乎现在是几点几分,只是在检测时间是否流逝而已。

@iseki “感觉这个问题根本是逻辑上不能避免的”
正解,cap 定理决定了总得放弃一个
bolide2005
2021 年 3 月 8 日
LZ 老哥一棍子要把“中文技术社区”这个垃圾堆里的人都抡倒的时候,至少也隐藏一下发过的帖子吧,真就不怕隐私泄露人肉社工啊。以及,把简历放到 GitHub 上的操作是为个啥,让人点评简历总可以放个脱敏版本的吧
ryd994
2021 年 3 月 8 日
@liudaolunhuibl Martin 提出的问题是真实存在的,但是是无解的。
他提出的解决方法是无效的。
他提出,由服务器维护一个计数器,然后 request 只有携带比当前计数器高的 token 才能被执行。

那么问题是,谁来维护这个计数器呢?如何维护这个计数器呢?如何保证这个全局计数器的 cap 呢?如果有办法实现这个计数器,那直接找服务器要 token 不就完事了,有什么必要用另一个锁呢?

这就有点 ex machina 的味道了。搞不定某问题,直接假设一个神奇的全局一致计数器,然后说问题解决了.
dmcslt
2021 年 3 月 8 日
都博士了,你是怎么发出这个帖子的,
pisc
2021 年 3 月 8 日
贴个 yc 的讨论好了,有感兴趣的可以去看看,我还蛮好奇这个楼有多少人是正经做分布式系统的,https://news.ycombinator.com/item?id=11065933
karloku
2021 年 3 月 8 日
脱离场景谈方案都是耍流氓. 别人的技术文章碰到的问题就是给你在不同的场景下趟坑. 要不大家都看官方 doc 就好了.

且和 lz 不同, 别人的"垃圾堆"文章, 在自己场景下遇到问题起码会思考一下原来自己用的不对, 而不是想都不想下个结论.
Zzdex
2021 年 3 月 8 日
看了楼主的简历,竟然是美国的博士.....
ryd994
2021 年 3 月 8 日
@Zzdex 博士又怎么了。隔行如隔山。他又不是搞分布式系统的,至少简历上不是。
但凡学过点分布式都知道分布式系统的 cap 定理。Martin 那篇文章就是外行什么都想要。
redlock 的说明里就写了,它所提供的是有限时间的 correctness 。自己程序 gc 时间太长这是实现的问题而不是算法的问题。
pisc
2021 年 3 月 8 日
@ryd994 全局时钟怎么到你这就便“神奇”了,我寻思着分布式系统中全局时钟不是最常见的组件之一?原来全世界分布式数据库都依赖一个“神奇”的东西呗? CAP 的问题很简单,大多数场景 C 是一定要要的,A 是 C 的前提下尽量保证,保 A 弃 C 的系统一定是要三思的,特别是基础组件,因为这把复杂性暴露给用户了,这就是为什么一谈分布式锁就无脑 redis 的毛病,因为用户有可能屁的不懂。
nagatoism
2021 年 3 月 8 日
Martin 那篇文章就是外行什么都想要

@ryd994 你比 Martin 内行,行,你赢了
nagatoism
2021 年 3 月 8 日
@pisc 明白人+1. 没有 C 的分布式锁,不如一开始就不存在
NUT
2021 年 3 月 8 日
这种瞎 jb 嘲讽的的确是 v2 的一个特色。
哈哈哈

哥来一次手动置顶。
ryd994
2021 年 3 月 8 日
@pisc 分布式系统中不存在一致的全局时钟,这是基本的基本。
但是可以有算法实现全局一致的时间戳。但这个时间戳是只有事后才会知道。而你不可能知道现在是什么时间。
换句话说,Martin 提出的,数据库自动拒绝无效的 token,这是无法做到的。

实践中,分布式的储存系统可以选择 eventual consistency 。而不依赖全局锁。

至于 cap 的取舍,这得看需求。你怎么就钦定了 a 就比 c 重要呢?小白 p 都不懂那就不该瞎 jb 用。就这么简单。用户 p 都不懂就得给用户擦屁股? redlock 那篇文章一开始就写了时间限制,自己不看说明还怪别人?
ryd994
2021 年 3 月 8 日
@nagatoism 然而没有 c 的分布式系统却很正常。
你要放弃 a 或者 p 去换 c 可以。用别的算法就行了。
我说的,martin 指出的问题是真实存在的。但他提出的解是无效的。
如果你觉得他提出的解有效,不妨找个实现出来。即可以 reject 旧 token 的分布式储存系统。
yzbythesea
2021 年 3 月 8 日
我觉得 Redlock 不好(那篇 hackernews 的链接说得很具体)。但是你可以自己用 Redis 写一个 distributed lock manager 。不过性能不太清楚。我之前呆过的公司用的是 DynamoDB 。

另外我觉得挂官方文档来反驳真的很 low 。
nagatoism
2021 年 3 月 8 日
@ryd994 感觉你没有细看 martin 的文章。Martin 的意思是,Redis 目前的实现里缺乏实现正确分布式锁的基础设施,是救不回来的。你什么地方看出 marin 有解法的?

redis 的作者根本不懂怎么设计分布式系统。他算法成立的前提是,系统良好,网络良好,时钟良好。但是这正是分布式系统的大忌。
正如 yc 的一个帖子说的,在分布式系统设计里,现有证明,再有实现。没有证明的实现正确性接近于 0.
gBurnX
2021 年 3 月 8 日
主要还是穷,加上科技不发达的设备性能低,不然直接 jpa 到 mysql,不香嘛。
sprite82
2021 年 3 月 8 日
@pisc @nagatoism 不是很理解,分布式和集群不就是为了性能问题,要让服务在某些节点挂掉之后仍然可用吗?既然要可用,P 不是最重要的吗
liudaolunhuibl
2021 年 3 月 8 日
@ryd994 马丁的论点是两个:
1. Distributed locks with an auto-release feature (the mutually exclusive lock property is only valid for a fixed amount of time after the lock is acquired) require a way to avoid issues when clients use a lock after the expire time, violating the mutual exclusion while accessing a shared resource. Martin says that Redlock does not have such a mechanism.

2. Martin says the algorithm is, regardless of problem “1”, inherently unsafe since it makes assumptions about the system model that cannot be guaranteed in practical systems.

你说的应该是第二个把,作者的态度是:
I want to mention again that, what is strange about all this, is that it is assumed that you always must have a way to handle the fact that mutual exclusion is violated. Actually if you have such a system to avoid problems during race conditions, you probably don’t need a distributed lock at all, or at least you don’t need a lock with strong guarantees, but just a weak lock to avoid, most of the times, concurrent accesses for performances reasons.

意思应该是遇到这种情况也许不需要分布式锁了,但是我没有找到你说的 token 部分,我只找到 For example you can implement Check and Set. When starting to work with a shared resource, we set its state to “`<token>`”, then we operate the read-modify-write only if the token is still the same when we write. 这段但是和你描述的有误差

总而言之马丁的论点之一就是如果持有锁的客户端在锁过期时间之后还没有释放锁的话回有安全问题:
In particular, the algorithm makes dangerous assumptions about timing and system clocks (essentially assuming a synchronous system with bounded network delay and bounded execution time for operations), and it violates safety properties if those assumptions are not met. Moreover, it lacks a facility for generating fencing tokens (which protect a system against long delays in the network or in paused processes).

作者的论点是:Just a quick note. In server-side implementations of a distributed lock with auto-release, the client may ask to acquire a lock, the server may allow the client to do so, but the process can stop into a GC pause or the network may be slow or whatever, so the client may receive the "OK, the lock is your" too late, when the lock is already expired. However you can do a lot to avoid your process sleeping for a long time, and you can't do much to avoid network delays, so the steps to check the time before/after the lock is acquired, to see how much time is left, should actually be common practice even when using other systems implementing locks with an expiry.

大概就是你应该避免客户端 A 进行长时间的工作,或者让客户端对时间进行检查:However you can do a lot to avoid your process sleeping for a long time, and you can't do much to avoid network delays, so the steps to check the time before/after the lock is acquired, to see how much time is left, should actually be common practice even when using other systems implementing locks with an expiry.

所以我觉得马丁的论点是有道理的,但是太过于绝对了,极端情况下也许 redlock 是不安全的但是大多数情况下是可以使用的
wangyzj
2021 年 3 月 8 日
强一致性会慢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/759583

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX