Dattatray Satpute
Dattatray Satpute

Reputation: 263

Not able lock key using node-redlock it gives error

I want lock the key ("ProjectData:GSTest") which is present in redis database. I am using below code to achive this goal but it gives error please let me know what is going wrog here.

const redlock = new Redlock(
  // You should have one client for each independent redis node
  // or cluster.
  [client],
  {
    // The expected clock drift; for more details see:
    // http://redis.io/topics/distlock
    driftFactor: 0.01, // multiplied by lock ttl to determine drift time

    // The max number of times Redlock will attempt to lock a resource
    // before erroring.
    retryCount: 20,

    // the time in ms between attempts
    retryDelay: 200, // time in ms

    // the max time in ms randomly added to retries
    // to improve performance under high contention
    // see https://www.awsarchitectureblog.com/2015/03/backoff.html
    retryJitter: 200, // time in ms

    // The minimum remaining time on a lock before an extension is automatically
    // attempted with the `using` API.
    automaticExtensionThreshold: 500, // time in ms
  }
);
console.log('Before acquire lock');
let lock = await redlock.acquire(["ProjectData:GSTest"], 120000);
console.log('Afetr  acquire lock');
try {
  // Do something...
  //await something();

  // Extend the lock. Note that this returns a new `Lock` instance.
  lock = await lock.extend(5000);

  // Do something else...
  //await somethingElse();
} finally {
  // Release the lock.
  await lock.release();
}
}

Error

Before acquire lock (node:20868) UnhandledPromiseRejectionWarning: ExecutionError: The operation was unable to achieve a quorum during its retry window. at Redlock._execute (Test\node_modules\redlock\dist\cjs\index.js:296:23) at async Redlock.acquire (Test\node_modules\redlock\dist\cjs\index.js:213:34) (Use node --trace-warnings ... to show where the warning was created)

Upvotes: 2

Views: 4799

Answers (2)

Hamidreza Vakilian
Hamidreza Vakilian

Reputation: 860

This error might also happen when you choose a very short ttl when calling acquire().

Upvotes: 0

emmanuel-bonin
emmanuel-bonin

Reputation: 21

Do you use a Redis in cluster mode? This error indicates that redlock tries to reach multiple nodes / cluster in order to lock a variable through all connected nodes. The Redlock algorithm, which is a distributed lock implementation, is designed to run on multiple Redis instances. The algorithm uses a quorum of Redis instances to achieve consensus on lock ownership and to ensure that the lock is only held by a single client at a time. Because it requires multiple instances to work, it would not be able to function properly on a single Redis instance.

Upvotes: 2

Related Questions