【发布时间】:2022-01-10 06:47:15
【问题描述】:
我正在研究Redis上的加锁方法,找到了这个官方文档 https://redis.io/commands/setnx#handling-deadlocks
我的理解是SETNX 是一个原子操作,只有一个客户端可以成功(获取1)而其他客户端失败(获取0)
但是在本文档的Handling deadlocks 部分中,它说:
1) C1 and C2 read lock.foo to check the timestamp, because they both received 0 after executing SETNX, as the lock is still held by C3 that crashed after holding the lock.
2) C1 sends DEL lock.foo
3) C1 sends SETNX lock.foo and it succeeds
4) C2 sends DEL lock.foo
5) C2 sends SETNX lock.foo and it succeeds
ERROR: both C1 and C2 acquired the lock because of the race condition.
我的问题是,为什么 3) 和 5) 可以成功?我认为只有 C1 或 C2 可以成功,但不能同时成功。
请纠正我的理解,谢谢。
【问题讨论】:
标签: redis