Skip to content

Configuration

Full reference for all Idempotency Plugin options.

Basic Configuration

typescript
import { RedisModule } from '@nestjs-redisx/core';
import { IdempotencyPlugin } from '@nestjs-redisx/idempotency';

@Module({
  imports: [
    RedisModule.forRoot({
      clients: { host: 'localhost', port: 6379 },
      plugins: [
        new IdempotencyPlugin({
          // Your options here
        }),
      ],
    }),
  ],
})
export class AppModule {}

Complete Options Reference

typescript
new IdempotencyPlugin({
  // Basic Settings
  defaultTtl: 86400,
  keyPrefix: 'idempotency:',
  headerName: 'Idempotency-Key',

  // Timeout Settings
  lockTimeout: 30000,
  waitTimeout: 60000,

  // Fingerprinting
  validateFingerprint: true,
  fingerprintFields: ['method', 'path', 'body'],
  fingerprintGenerator: async (context) => {
    const req = context.switchToHttp().getRequest();
    return createHash('sha256')
      .update(`${req.method}|${req.path}|${JSON.stringify(req.body)}`)
      .digest('hex');
  },

  // Error Handling
  errorPolicy: 'fail-closed',
})

Configuration by Use Case

Payment Processing (Strict)

typescript
new IdempotencyPlugin({
  defaultTtl: 86400,         // 24 hours
  lockTimeout: 60000,        // 1 minute (payments can be slow)
  waitTimeout: 120000,       // 2 minutes
  validateFingerprint: true, // Strict validation
  errorPolicy: 'fail-closed',
})

Order Creation (Standard)

typescript
new IdempotencyPlugin({
  defaultTtl: 3600,          // 1 hour
  lockTimeout: 30000,        // 30 seconds
  waitTimeout: 60000,        // 1 minute
  validateFingerprint: true,
})

Webhook Handling (Lenient)

typescript
new IdempotencyPlugin({
  defaultTtl: 86400,
  headerName: 'X-Webhook-ID',  // Custom header
  validateFingerprint: false,  // Body may vary
  errorPolicy: 'fail-open',    // Don't require key
})

Environment Configuration

typescript
// config/idempotency.config.ts
import { registerAs } from '@nestjs/config';

export default registerAs('idempotency', () => ({
  ttl: parseInt(process.env.IDEMPOTENCY_TTL || '86400', 10),
  headerName: process.env.IDEMPOTENCY_HEADER || 'Idempotency-Key',
  keyPrefix: process.env.IDEMPOTENCY_PREFIX || 'idempotency:',
  lockTimeout: parseInt(process.env.IDEMPOTENCY_LOCK_TIMEOUT || '30000', 10),
  waitTimeout: parseInt(process.env.IDEMPOTENCY_WAIT_TIMEOUT || '60000', 10),
}));
bash
# .env
IDEMPOTENCY_TTL=86400
IDEMPOTENCY_HEADER=Idempotency-Key
IDEMPOTENCY_LOCK_TIMEOUT=30000
IDEMPOTENCY_WAIT_TIMEOUT=60000

Options Deep Dive

TTL Settings

OptionTypeDefaultDescription
defaultTtlnumber86400Record lifetime (seconds)

TTL Guidelines:

Operation TypeRecommended TTLReason
Payments24-48 hoursImportant, may retry next day
Orders1-24 hoursSession-based
Notifications1-4 hoursTime-sensitive
Webhooks24-72 hoursMay replay

Timeout Settings

OptionTypeDefaultDescription
lockTimeoutnumber30000Max processing time (ms)
waitTimeoutnumber60000Max wait for concurrent (ms)

Timeout Relationship:

waitTimeout >= lockTimeout + safety_margin

Recommended: waitTimeout = lockTimeout * 2

Fingerprint Settings

OptionTypeDefaultDescription
validateFingerprintbooleantrueCheck request matches
fingerprintFieldsarray['method', 'path', 'body']Fields to hash
fingerprintGeneratorfunctionundefinedCustom hash function

Next Steps

Released under the MIT License.