mike-marcacci / node-redlock

A node.js redlock implementation for distributed, highly-available redis locks
MIT License
1.78k stars 163 forks source link

Configure Redis DB # explicitly? #298

Open rbonestell opened 1 month ago

rbonestell commented 1 month ago

How can I choose which Redis DB number is used by node-redlock?

I have a perfectly good redis instance I'd like to use for redlock, but I don't want to pollute the data in DB #0 and I'd like to configure node-redlock to use another DB. Is this possible?

rbonestell commented 1 month ago

For anyone else curious about the same functionality, at this point I am manually selecting the DB # on my ioredis Redis client instance prior to calling using() on my redlock instance which uses the Redis instance:

await this.redisClient.select(8);
await this.redlock.using(ids, lockDuration, func);

My only remaining concern is that I intended to share the Redis client instance as a singleton across my application, and there's a non-zero chance for a race condition where another call to the same Redis client could set the DB to something other than 8 prior to the using() executing.

In the meantime, I have initialized another bespoke instance of the Redis client specifically for use with the redlock instance.

I would like to see a configurable dbNumber value in node-redlock which accepts the DB # and utilizes it in the Lua scripts directly to ensure no susceptibility to race conditions on the same Redis client instance. If dbNumber is not provided then it should default to 0 for backwards compatibility and to ensure support for services such as AWS Serverless Elasticache, which only support 1 Redis DB (DB 0).