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

@redis-kit/lock

v0.1.0-3

Published

Production-ready distributed locking library for Redis with Redlock algorithm implementation

Readme

@redis-kit/lock

Production-ready distributed locking for Redis with automatic lifecycle management

npm version npm downloads License: MIT TypeScript Node.js

Fault-tolerant • Auto-extension • Redlock algorithm • Production-tested

Features

  • Distributed Locking: Implements the official Redlock algorithm for multi-instance Redis deployments
  • Automatic Extension: Prevents lock expiration during long-running operations with built-in auto-extension
  • Fault Tolerance: Requires majority consensus from Redis instances, works even when some instances fail
  • TypeScript Native: Full type safety with comprehensive TypeScript definitions included
  • Flexible API: Choose between manual lock management or automatic lifecycle with withLock
  • Production Ready: Clock drift compensation, retry logic with jitter, and battle-tested reliability

Installation

npm install @redis-kit/lock

Quick Start

import { Redlock } from '@redis-kit/lock';
import { createClient } from 'redis';

const clients = [
  createClient({ host: 'redis1.example.com' }),
  createClient({ host: 'redis2.example.com' }),
  createClient({ host: 'redis3.example.com' }),
];

await Promise.all(clients.map((client) => client.connect()));

const redlock = new Redlock(clients);
await redlock.withLock('user:123:profile', 30000, async () => {
  // Critical section - only one process can execute this
  await updateUserProfile(userId, profileData);
});

API Reference

Redlock

Constructor

new Redlock(redisClients: RedisClientType[], options?: RedlockOptions)

Methods

acquire(key: string, ttlMs: number): Promise<RedlockInstance | null>

Attempts to acquire a distributed lock.

withLock<T>(key: string, ttlMs: number, fn: () => Promise<T>): Promise<T>

Executes a function within a lock context with automatic management.

RedlockInstance

Properties

  • isValid: boolean - Whether the lock is currently valid

Methods

  • release(): Promise<boolean> - Releases the lock
  • extend(newTtlMs?: number): Promise<boolean> - Extends the lock's TTL
  • startAutoExtension(thresholdMs?: number): void - Starts automatic extension

Configuration

interface RedlockOptions {
  driftFactor?: number; // Clock drift compensation (default: 0.01)
  retryDelayMs?: number; // Base retry delay (default: 200)
  retryJitterMs?: number; // Random jitter (default: 100)
  maxRetryAttempts?: number; // Maximum retries (default: 3)
}

Advanced Usage

Manual Lock Management

const lock = await redlock.acquire('payment:order:456', 10000);

if (lock) {
  try {
    await processPayment(orderId);
    await updateInventory(items);
  } finally {
    await lock.release();
  }
} else {
  throw new Error('Could not acquire lock for payment processing');
}

Auto-Extension for Long Operations

const lock = await redlock.acquire('data-migration', 30000);

if (lock) {
  lock.startAutoExtension(5000); // Extend 5 seconds before expiry

  try {
    for (const batch of dataBatches) {
      await migrateBatch(batch);
    }
  } finally {
    await lock.release(); // Stops auto-extension automatically
  }
}

Custom Configuration

const redlock = new Redlock(clients, {
  driftFactor: 0.01,
  retryDelayMs: 200,
  retryJitterMs: 100,
  maxRetryAttempts: 5,
});

try {
  await redlock.withLock('critical-resource', 60000, async () => {
    await performCriticalOperation();
  });
} catch (error) {
  console.error('Lock operation failed:', error.message);
}

MIT