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

@mohamedsaba/idempotent

v1.1.1

Published

Distributed idempotency protection for NestJS

Readme

NestJS Idempotency

A production-grade idempotency library for NestJS. This package provides a declarative way to handle duplicate requests in distributed systems, ensuring that non-idempotent operations (like payments or order creation) are only executed once.

Features

  • Distributed Locking & Fencing Tokens: Uses Redis with atomic Lua scripts to prevent race conditions and "lock hijacking," ensuring strict ownership of idempotency keys.
  • Robust Fingerprinting: Cycle-safe, type-aware hashing of request payloads to prevent key collisions when different data is sent with the same key.
  • Automatic Stream Detection: Gracefully handles StreamableFile, Buffer, and response streams by bypassing the cache to avoid data corruption.
  • Pluggable Storage: Includes a high-performance in-memory store for local development and an enterprise-grade Redis store for production.
  • Safety Limits: Protection against storage exhaustion with configurable body size limits and status code filtering.
  • Observability: Standardized logging for cache hits, conflicts, and storage health.
  • Tenant Isolation: Optional namespacing to keep idempotency keys unique across different users or tenants.

Installation

npm i @mohamedsaba/idempotent

Quick Start

1. Register the Module

import { IdempotencyModule } from '@mohamedsaba/idempotent';
import { Redis } from 'ioredis';

@Module({
  imports: [
    IdempotencyModule.forRoot({
      store: {
        provide: IdempotencyStore,
        useFactory: (redis: Redis) => new RedisStore(redis),
        inject: ['REDIS_CLIENT'],
      },
      ttl: 86400, // Cache for 24 hours
    }),
  ],
})
export class AppModule {}

2. Protect Routes

Add the @Idempotent() decorator to any controller method that requires idempotency protection.

import { Idempotent } from '@mohamedsaba/idempotent';

@Controller('orders')
export class OrderController {
  @Post()
  @Idempotent()
  async createOrder(@Body() dto: CreateOrderDto) {
    // This logic will only run once per Idempotency-Key
    return this.orderService.create(dto);
  }
}

Configuration

| Option | Description | Default | | :--- | :--- | :--- | | headerName | Header to read the key from | idempotency-key | | ttl | Expiration time in seconds | 86400 | | lockTtl | How long to hold the 'in-progress' lock | 60 | | maxBodySize | Maximum response size to cache (bytes) | undefined | | cacheableStatuses | HTTP statuses that should be cached | [200, 201, 202, 204] | | storageFailureStrategy | What to do if Redis is down (fail-open or fail-closed) | fail-closed |

How it Works

  1. Check: When a request arrives, the interceptor checks if the idempotency key exists in the store.
  2. Replay: If a completed response is found, it is immediately replayed to the client with an x-idempotency-replayed header.
  3. Lock: If no response is found, it attempts to acquire an atomic IN_PROGRESS lock. If another request is already processing the same key, it returns a 409 Conflict.
  4. Execute: The controller logic runs.
  5. Save: The response is hashed and saved to the store, and the lock is released.

Production Recommendations

  1. Storage: Always use RedisStore in production. MemoryStore is intended for local development and does not support distributed environments.
  2. TTL: Set a reasonable ttl for your use case. 24 hours is often a safe default, but consider your business logic (e.g., payment windows).
  3. Fail-Open Strategy: If your application must remain available even if Redis is down, set storageFailureStrategy: 'fail-open'. Be aware that this temporarily disables idempotency protection.
  4. Header Safety: The library automatically filters sensitive headers like set-cookie, but ensure any custom headers you return are also safe to cache.

License

MIT