β‘@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/throttlerBasic 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
Redis
Production, Distributed
High
Excellent
Medium
Default
Development, Single Instance
Very High
Limited
Low
Settings Configuration
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:
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