background
I realize a redis client(support cluster) and an issue is raised for the support of distributed lock made by redis cluster.
I've read post of redlock algorithm and related debate
problems
Actually it is impossible to make one key hashed to different nodes in a redis cluster and hard to generate keys in a specific rule and make sure they do not migrated in cluster. In the worst situation, all key slots may exists in one node. Then the availability will be the same as one key in one node.
my algorithm
My solution is to take advantage of READONLY
mode of slaves to ensure
the lock key is synced from master to N/2 + 1 of its slaves to avoid the fail-over problem.
Since it is a single-key solution, the migration problem also do no matter.
- random token +
SETNX
+ expire time to acquire a lock in cluster master node - if lock is acquired successfully then check the lock in slave nodes(may there be N slave nodes) using
READONLY
mode, if N/2+1 is synced successfully then break the check and return True - Use lua script described in redlock algorithm to release lock with the client which has the randomly generated token, if the client crashes, then wait until the lock key expired.
Could you please do me a favour to see if the algorithm is wrong? I have thought several corner cases about it but i am still not so sure.