Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

redlock

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redlock

A node.js redlock implementation for distributed redis locks

  • 3.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
515K
decreased by-8.12%
Maintainers
1
Weekly downloads
 
Created

What is redlock?

Redlock is a distributed lock manager for Node.js using Redis. It implements the Redlock algorithm, which is a method for achieving distributed mutual exclusion. This is useful for ensuring that only one process can access a shared resource at a time, even in a distributed system.

What are redlock's main functionalities?

Acquiring a Lock

This feature allows you to acquire a lock on a resource. The lock is held for a specified duration (2000 ms in this example). If the lock is successfully acquired, you can perform your operations and then release the lock.

const Redlock = require('redlock');
const redis = require('redis');
const client = redis.createClient();
const redlock = new Redlock([client]);

redlock.lock('locks:resource', 2000).then(function(lock) {
  // Do something with the lock
  console.log('Lock acquired');
  // Release the lock
  return lock.unlock();
}).catch(function(err) {
  console.error('Failed to acquire lock', err);
});

Extending a Lock

This feature allows you to extend the duration of an existing lock. This is useful if you need more time to complete your operations while holding the lock.

const Redlock = require('redlock');
const redis = require('redis');
const client = redis.createClient();
const redlock = new Redlock([client]);

redlock.lock('locks:resource', 2000).then(function(lock) {
  // Extend the lock
  return lock.extend(2000);
}).then(function(lock) {
  console.log('Lock extended');
  // Release the lock
  return lock.unlock();
}).catch(function(err) {
  console.error('Failed to extend lock', err);
});

Handling Lock Failures

This feature demonstrates how to handle failures when acquiring or maintaining a lock. If an error occurs, it is caught and handled appropriately.

const Redlock = require('redlock');
const redis = require('redis');
const client = redis.createClient();
const redlock = new Redlock([client]);

redlock.lock('locks:resource', 2000).then(function(lock) {
  // Do something with the lock
  console.log('Lock acquired');
  // Simulate a failure
  throw new Error('Something went wrong');
}).catch(function(err) {
  console.error('Failed to acquire or maintain lock', err);
});

Other packages similar to redlock

Keywords

FAQs

Package last updated on 16 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc