NOTE: this is still a draft, so it's subject to changes. This alert will be removed as soon as final version will be available

⚑@hsuite/throttler - Advanced Rate Limiting for NestJS

⚑ Powerful and flexible rate limiting library for NestJS applications with Redis-based distributed storage support

Enterprise-grade rate limiting solution providing robust protection against abuse and optimal resource utilization with support for Redis distributed storage, IP-based protection, and comprehensive request tracking.


πŸ“š Table of Contents


✨ Quick Start

Installation

npm install @hsuite/throttler

Basic Setup

Protected Routes


πŸ—οΈ Architecture

Core Component Areas

⚑ Rate Limiting Engine

  • IP-Based Tracking - Track and limit requests based on client IP addresses

  • Time Window Management - Configurable TTL for request counting windows

  • Request Counting - Efficient increment and threshold checking

  • Automatic Blocking - Immediate protection when limits are exceeded

πŸ—„οΈ Storage Backends

  • Redis Storage - Distributed rate limiting across multiple server instances

  • In-Memory Storage - Local storage for development and single-instance deployments

  • Persistent Tracking - Reliable request count persistence with Redis

  • High Performance - Optimized for high-throughput applications

πŸ›‘οΈ Security Features

  • Abuse Protection - Prevent API abuse and resource exhaustion

  • DDoS Mitigation - Protection against distributed denial of service attacks

  • Response Headers - Informative rate limit headers for client guidance

  • Graceful Degradation - Proper error handling and retry mechanisms

πŸ”§ NestJS Integration

  • Global Guards - Automatic protection for all routes

  • Custom Guards - Flexible guard implementation for specific use cases

  • Decorator Support - Easy route-specific configuration

  • Module Configuration - Comprehensive async configuration support

Module Structure


πŸ”§ API Reference

Core Module Types

Configuration Interface

IThrottler.IOptions

  • Purpose: Complete throttler configuration interface

  • Properties: enabled, storage, settings, redis

  • Usage: Module configuration and factory pattern implementation

Storage Selection

Storage Type
Use Case
Performance
Scalability
Complexity

Redis

Production, Distributed

High

Excellent

Medium

Default

Development, Single Instance

Very High

Limited

Low

Settings Configuration

Parameter
Type
Description
Default
Range

ttl

number

Time window (seconds)

60

1-3600

limit

number

Max requests per window

100

1-10000

Response Headers

The library automatically sets informative headers:

Header
Description
Example

X-RateLimit-Limit

Maximum requests per window

100

X-RateLimit-Remaining

Remaining requests in window

47

X-RateLimit-Reset

Seconds until window reset

23

Retry-After

Wait time when blocked (seconds)

37

Core Services

SecurityThrottlerModule

  • Purpose: Main module providing rate limiting functionality

  • Methods: forRootAsync(), forRoot()

  • Usage: Application module configuration

SecurityThrottlerService

  • Purpose: Injectable service for programmatic throttling access

  • Features: Rate limit checking, configuration access

  • Usage: Custom throttling logic implementation

CustomThrottlerGuard

  • Purpose: IP-based rate limiting guard

  • Features: Automatic request tracking, header injection, blocking

  • Usage: Route protection and global application security


πŸ“– Guides

Rate Limiting Setup Guide

Complete guide to setting up rate limiting with different storage backends. Comprehensive setup instructions covering Redis and in-memory storage configuration, TTL and limit settings, environment-specific configurations, and performance optimization for enterprise-scale rate limiting systems.

Redis Configuration Guide

Comprehensive Redis setup for distributed rate limiting. Advanced configuration guide covering Redis cluster setup, high availability configuration, performance tuning, security settings, and enterprise-grade Redis deployment for scalable rate limiting.

Custom Guards Implementation Guide

Learn how to create custom throttling guards for specific use cases. Detailed implementation guide covering custom guard development, request filtering, user-based rate limiting, tier-based throttling, and advanced rate limiting patterns for enterprise applications.

Production Deployment Guide

Best practices for deploying rate limiting in production environments. Enterprise deployment guide covering scalability considerations, monitoring setup, failover strategies, performance optimization, and production-grade rate limiting architecture.


🎯 Examples

Advanced Module Configuration

Custom Throttling Guards

Dynamic Rate Limiting Service

Redis Cluster Configuration

Monitoring and Analytics Integration


πŸ”— Integration

Required Dependencies

Module Integration

Documentation Generation

Environment Configuration

Integration with HSuite Ecosystem

Best Practices

πŸ”§ Configuration Best Practices

  • Use Redis storage for production and distributed systems

  • Set appropriate limits based on endpoint resource usage

  • Monitor and adjust limits based on actual usage patterns

  • Implement proper error handling for rate limit exceptions

πŸ›‘οΈ Security Best Practices

  • Consider user-authenticated rate limiting for different tiers

  • Implement IP whitelisting for trusted sources

  • Monitor for potential abuse patterns and automated attacks

  • Log rate limiting events for security analysis

⚑ Performance Best Practices

  • Use Redis connection pooling for high-traffic applications

  • Configure appropriate TTL values for Redis keys

  • Use Redis clustering for high availability

  • Monitor system performance under load

πŸ“Š Monitoring Best Practices

  • Track rate limiting metrics and trends

  • Set up alerts for unusual traffic patterns

  • Generate regular reports for optimization

  • Monitor system health and Redis performance


⚑ Enterprise Rate Limiting: Powerful and flexible rate limiting with Redis-based distributed storage for high-performance applications.

πŸ›‘οΈ Advanced Security: IP-based protection, abuse prevention, and comprehensive request tracking with informative headers.

πŸ”§ NestJS Integration: Seamless integration with guards, decorators, and async configuration for enterprise applications.


Built with ❀️ by the HSuite Team Copyright © 2025 HSuite. All rights reserved.

Last updated