npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-redlock

v2.2.0

Published

A distributed locking algorithm used to manage distributed resources in a system.

Downloads

956

Readme

Redlock Implementation in TypeScript

This library provides a robust and distributed locking mechanism using the Redlock algorithm implemented in TypeScript. It allows you to acquire, release, and renew locks on shared resources across multiple Redis instances.


Features

  • Acquire a distributed lock with configurable retry logic.
  • Release a lock safely to ensure resource consistency.
  • Renew locks to extend the time-to-live (TTL) for a lock.
  • Support for custom retry strategies.
  • Compatibility with multiple Redis clients for high availability.

Installation

  1. Clone the repository or copy the Redlock.ts file into your project.
  2. Install the required dependencies using npm or yarn:
npm install ioredis

Usage

Setup

Create Redis clients that implement the RedisClient interface and pass them to the Redlock class. For example, using ioredis:

import Redis from 'ioredis';
import { Redlock } from './Redlock';

const redisClients = [
  new Redis({ host: '127.0.0.1', port: 6379 }),
  new Redis({ host: '127.0.0.1', port: 6380 }),
  new Redis({ host: '127.0.0.1', port: 6381 }),
];

const redlock = new Redlock(redisClients);

Acquiring a Lock

You can acquire a lock on a resource with a specific TTL:

(async () => {
  const lock = await redlock.acquireLock('my-resource', 5000); // Lock for 5000ms
  if (lock) {
    console.log('Lock acquired:', lock);
  } else {
    console.log('Failed to acquire lock');
  }
})();

Acquiring a Lock with Custom Retry Strategy

To customize the retry strategy, pass a function to acquireLockWithCustomRetry:

(async () => {
  const retryStrategy = (attempt: number) => 100 * (attempt + 1); // Exponential backoff
  const lock = await redlock.acquireLockWithCustomRetry('my-resource', 5000, retryStrategy);
  if (lock) {
    console.log('Lock acquired with custom retry:', lock);
  } else {
    console.log('Failed to acquire lock with custom retry');
  }
})();

Releasing a Lock

To release a lock after use:

(async () => {
  const lock = await redlock.acquireLock('my-resource', 5000);
  if (lock) {
    await redlock.releaseLock(lock.resource, lock.value);
    console.log('Lock released');
  }
})();

Renewing a Lock

Extend the TTL of an existing lock:

(async () => {
  const lock = await redlock.acquireLock('my-resource', 5000);
  if (lock) {
    const renewed = await redlock.renewLock(lock.resource, lock.value, 5000);
    if (renewed) {
      console.log('Lock renewed');
    } else {
      console.log('Failed to renew lock');
    }
  }
})();

API Reference

acquireLock(resource: string, ttl: number): Promise<Lock | null>

Attempts to acquire a lock on the specified resource.
Parameters:

  • resource: The resource to lock.
  • ttl: The time-to-live for the lock in milliseconds.

Returns: A Lock object if successful, otherwise null.


acquireLockWithCustomRetry(resource: string, ttl: number, retryStrategy: (attempt: number) => number): Promise<Lock | null>

Attempts to acquire a lock using a custom retry strategy.
Parameters:

  • resource: The resource to lock.
  • ttl: The time-to-live for the lock in milliseconds.
  • retryStrategy: A function that determines the delay before the next retry attempt.

Returns: A Lock object if successful, otherwise null.


releaseLock(resource: string, value: string): Promise<void>

Releases a lock on the specified resource.
Parameters:

  • resource: The resource to unlock.
  • value: The unique lock value.

Returns: A Promise that resolves when the lock is released.


renewLock(resource: string, value: string, ttl: number): Promise<boolean>

Renews the TTL for an existing lock.
Parameters:

  • resource: The resource to renew the lock on.
  • value: The unique lock value.
  • ttl: The new TTL in milliseconds.

Returns: true if the lock was successfully renewed, otherwise false.


Notes

  • Ensure all Redis clients are connected and synchronized for optimal performance.
  • Use a unique value for each lock to prevent accidental unlocking by other processes.

License

This project is licensed under the MIT License. See the LICENSE file for details.