Skip to main content

Rate Limiting & Traffic Control

The rate limiting utilities provide comprehensive traffic control, API protection, and resource management for edge environments. These utilities help prevent abuse, ensure fair resource allocation, and maintain service stability under load.

Features

  • Multiple Algorithms: Fixed window, sliding window, token bucket, leaky bucket
  • Flexible Keys: IP-based, user-based, API key-based, custom keys
  • Real-time Monitoring: Request tracking and rate limit status
  • Distributed: Works across multiple edge locations
  • High Performance: Low-overhead rate limiting with caching
  • Auto-scaling: Dynamic limit adjustment based on load
  • Analytics: Rate limiting metrics and reporting
  • Security: DDoS protection and abuse prevention

Quick Start

Rate Limiting Algorithms

Fixed Window

Divides time into fixed intervals and counts requests within each window.

Sliding Window

Provides smoother rate limiting by considering a rolling time window.

Token Bucket

Allows bursts of traffic while maintaining average rate limits.

Leaky Bucket

Smooths traffic by processing requests at a constant rate.

Key Generators

IP-based Limiting

Rate limit based on client IP address.

User-based Limiting

Rate limit based on authenticated user ID.

API Key-based Limiting

Rate limit based on API keys.

Custom Key Generators

Create custom keys based on any request attributes.

Rate Limit Store

Storage backend for rate limiting data with multiple implementations.

In-Memory Store

Simple in-memory storage for single-instance deployments.

Distributed Store

Distributed storage for multi-instance deployments.

Advanced Configuration

Burst Handling

Allow temporary bursts while maintaining average limits.

Dynamic Limits

Adjust limits based on time, load, or other factors.

Multi-tier Rate Limiting

Apply multiple rate limits with different scopes.

Middleware Integration

Rate Limiting Middleware

Per-Endpoint Rate Limiting

User Tier-based Limiting

Advanced Examples

DDoS Protection

API Gateway Rate Limiting

Adaptive Rate Limiting

Rate Limit Analytics

Performance Optimization

Caching Rate Limit Results

Batch Operations

Async Rate Limiting

Platform-Specific Notes

Cloudflare Workers

  • Use Cloudflare KV for distributed rate limiting
  • Leverage Cloudflare’s global network for consistency
  • Compatible with Cloudflare Rate Limiting

Vercel Edge Functions

  • Use Vercel KV for distributed storage
  • Compatible with Vercel’s edge network
  • Support for Vercel’s deployment regions

Deno Deploy

  • Native support for distributed rate limiting
  • Compatible with Deno KV for storage
  • Support for Web APIs for rate limiting

Best Practices

Rate Limit Headers

Always include standard rate limit headers in responses:

Graceful Degradation

Handle rate limit violations gracefully:

Monitoring and Alerting

Monitor rate limiting effectiveness:

Testing

Run rate limiting tests with:

API Reference

RateLimiter Methods

  • check(request) - Check if request is allowed
  • reset(key) - Reset rate limit for a key
  • getRemaining(key) - Get remaining requests for a key
  • getResetTime(key) - Get reset time for a key

RateLimitStore Methods

  • get(key) - Get value for key
  • set(key, value, ttl) - Set value with TTL
  • increment(key, amount) - Increment value
  • delete(key) - Delete key
  • cleanup() - Clean up expired entries

MultiTierRateLimiter Methods

  • check(request) - Check all tiers
  • addTier(config) - Add rate limiting tier
  • removeTier(name) - Remove rate limiting tier
  • getTierStatus(name) - Get status of specific tier

Contributing

When contributing to rate limiting utilities:
  1. Maintain backward compatibility
  2. Add comprehensive tests for new algorithms
  3. Update documentation for new features
  4. Consider performance impact of changes
  5. Test across all supported platforms

License

MIT