I need to connect to a single Redis instance from an application client.
Since the client will be replicated in Kubernetes, I'm studying Redis documentation about locks to prevent races between the client replicas.
After some googling and reading around, I zeroed in on these two resources:
- the
SETNX
command described here: https://redis.io/commands/setnx - the Redlock algorithm described here: https://redis.io/topics/distlock
Interestingly the SETNX
docs explicitly advise against using SETNX
to implement locks, stating that it has basically become obsolete:
The following pattern is discouraged in favor of the Redlock algorithm [...]
We document the old pattern anyway because certain existing implementations link to this page as a reference.
However the Redlock algorithm is specifically tailored for distributed locks, thus when one seeks to lock on multiple Redis instances - they actually refer to multiple masters.
To go a bit further, the library redsync (golang) declares the New
function as follows:
func New(pools []Pool) *Redsync {
return &Redsync{
pools: pools,
}
}
It looks unmistakably designed to support locking on a Redis cluster.
In my use case, I'm going to connect to only one Redis instance.
Probably I can just use the redsync package and pass an slice of length one, yet to me it looks like the SETNX
pattern could work equally fine on a single Redis instance.
Am I seeing this correctly? Thank you