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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tawandotorg/nestjs-async-lock

v1.1.10

Published

A simple, extensible, and framework-agnostic async lock (mutex) solution for NestJS. This package provides atomic locks for synchronizing access to shared resources, supporting both Redis and in-memory stores. In other words, it prevents several processes

Downloads

73

Readme

NestJS Async Lock

A simple, extensible, and framework-agnostic async lock (mutex) solution for NestJS. This package provides atomic locks for synchronizing access to shared resources, supporting both Redis and in-memory stores. In other words, it prevents several processes, or concurrent code, from executing a section of code at the same time.

Features

  • Atomic locks (mutexes) for critical section protection
  • Multiple backends: Redis (distributed) and in-memory (single process)
  • Automatic and manual lock management
  • NestJS integration: Injectable, testable, and idiomatic
  • TypeScript support

Installation

npm install @tawandotorg/nestjs-async-lock
# or
yarn add @tawandotorg/nestjs-async-lock

Quick Start

1. Import the Module

import { AsyncLockModule, MemoryStore } from '@tawandotorg/nestjs-async-lock';
import Redis from 'ioredis';

@Module({
  imports: [
    AsyncLockModule.forRoot({
      default: 'memory', // or 'redis'
      stores: {
        // choose one
        memory: { driver: new MemoryStore() },
        redis: { driver: new Redis() },
      },
    }),
  ],
})
export class AppModule {}

2. Inject and Use the Lock Service

Example

import { Injectable } from '@nestjs/common';
import { AsyncLockService } from '@tawandotorg/nestjs-async-lock';

@Injectable()
export class OrderService {
  constructor(private readonly lockService: AsyncLockService) {}

  async processOrder(orderId: string) {
    // Manual locking
    const lock = await this.lockService.acquireLock(`order.processing.${orderId}`);
    try {
      // ...process the order...
    } finally {
      await this.lockService.releaseLock(lock);
    }
  }

  async processOrderAuto(orderId: string) {
    // Automatic locking
    return this.lockService.runWithLock(`order.processing.${orderId}`, 10000, async () => {
      // ...process the order...
      return 'Order processed successfully';
    });
  }
}

Configuration

Configure the lock store and options via AsyncLockModule.forRoot().

Supported Stores

  • redis: Distributed, recommended for multi-instance deployments. When using Redis, this package uses Redlock under the hood for distributed locking. See the Redlock documentation for more details on the algorithm and guarantees.
  • memory: Fast, for single-process or testing environments.

Example: Redis Store

import Redis from 'ioredis';

AsyncLockModule.forRoot({
  default: 'redis',
  stores: {
    redis: { driver: new Redis() },
  },
});

Example: Memory Store

import { MemoryStore } from '@tawandotorg/nestjs-async-lock';

AsyncLockModule.forRoot({
  default: 'memory',
  stores: {
    memory: { driver: new MemoryStore() },
  },
});

API

acquireLock(resource: string, ttl = 10000)

Acquires a lock for the given resource. Throws if the lock cannot be acquired.

runWithLock(resource: string, ttl: number, fn: () => Promise<T>): Promise<T>

Runs the provided function with a lock held for the resource. Releases the lock automatically after execution.

releaseLock(lock)

Releases the acquired lock.


License

MIT


Contributing

Pull requests and issues are welcome! Please open an issue to discuss your ideas or report bugs.


Inspiration

This package is inspired by the AdonisJS Locks concept, but is a custom implementation for NestJS.