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

lambda-throttle

v0.2.0

Published

Rate limiting for AWS Lambda functions with DynamoDB-backed persistence, per-key configurable limits, and SQS batch integration.

Readme

lambda-throttle

Rate limiting for AWS Lambda functions with DynamoDB-backed persistence, per-key configurable limits, and SQS batch integration.

Designed to work with @aws-lambda-powertools/batch and Middy middleware.

Installation

npm install lambda-throttle @aws-sdk/client-dynamodb

Quick Start

SQS Batch Processing

Throttle message processing per key, returning rate limited messages back to the queue.

import { processPartialResponse, SqsFifoPartialProcessor } from '@aws-lambda-powertools/batch';
import { makeThrottled, ThrottleConfig } from 'lambda-throttle';
import { DynamoDBThrottlePersistenceLayer } from 'lambda-throttle/dynamodb';
import type { SQSEvent, SQSRecord } from 'aws-lambda';

const processor = new SqsFifoPartialProcessor();

const processRecord = makeThrottled(
  async (record: SQSRecord) => {
    const body = JSON.parse(record.body);
    await callDownstreamService(body);
  },
  {
    persistenceStore: new DynamoDBThrottlePersistenceLayer({
      tableName: 'throttling-table',
    }),
    config: new ThrottleConfig({
      throttleKeyJmesPath: 'body.selectedKey',
      maxRequestsPerWindow: 50,
      windowSeconds: 60,
    }),
  }
);

export const handler = async (event: SQSEvent) => {
  return processPartialResponse(event, processRecord, processor);
};

When a record exceeds the limit, ThrottleExceededError is thrown. The batch processor reports it as a batch item failure, and SQS makes the message available again after the visibility timeout.

Dynamic Per-Key Rate Limits

Set different limits based on a field in the message (e.g., tier):

import { ThrottleConfig } from 'lambda-throttle';

new ThrottleConfig({
  throttleKeyJmesPath: 'body.tenantId',
  maxRequestsPerWindow: {
    jmesPath: 'body.tier',
    mapping: { basic: 10, standard: 50, premium: 500 },
    default: 20,
  },
  windowSeconds: 60,
})

Middy Middleware

For API Gateway or direct invocations:

import middy from '@middy/core';
import { makeHandlerThrottled } from 'lambda-throttle/middleware';
import { DynamoDBThrottlePersistenceLayer } from 'lambda-throttle/dynamodb';
import { ThrottleConfig } from 'lambda-throttle';

export const handler = middy(async (event) => {
  return { statusCode: 200, body: 'OK' };
}).use(
  makeHandlerThrottled({
    persistenceStore: new DynamoDBThrottlePersistenceLayer({ tableName: 'throttle-table' }),
    config: new ThrottleConfig({
      throttleKeyJmesPath: 'requestContext.identity.sourceIp',
      maxRequestsPerWindow: 100,
      windowSeconds: 60,
    }),
  })
);

Configuration

| Option | Type | Default | Description | |--------|------|---------|-------------| | throttleKeyJmesPath | string | undefined | JMESPath to extract the throttle key. Omit for global limit. | | maxRequestsPerWindow | number \| object | — | Static limit or dynamic config with jmesPath, mapping, default. | | windowSeconds | number | 60 | Fixed window duration in seconds. | | hashAlgorithm | string | 'md5' | Hash algorithm for key generation ('md5', 'sha256', etc.). |

Dynamic Rate Limits

The maxRequestsPerWindow option accepts either a number or an object:

// Static
maxRequestsPerWindow: 100

// Extract a numeric limit directly from the payload
maxRequestsPerWindow: { jmesPath: 'body.rateLimit' }

// Map a string field to a limit
maxRequestsPerWindow: {
  jmesPath: 'body.tier',
  mapping: { basic: 10, standard: 50, premium: 500 },
  default: 20,
}

Per-Invocation Cache

When processing a batch of SQS records, once a key is throttled, all subsequent records with the same key in that batch are immediately rejected without hitting DynamoDB. This reduces costs and latency.

Disabling

Set the environment variable to disable throttling (e.g., for local development):

POWERTOOLS_THROTTLE_DISABLED=true

Development

# Install
npm install

# Build
npm run build

# Unit tests
npm run test:unit

# Integration tests (requires LocalStack on localhost:4566)
npm run test:integration