Skip to content

muditkalra/Rate-Limiting-Guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Rate Limiting: A Comprehensive Guide

A complete guide to rate limiting algorithms, implementations, and best practices for building robust APIs and distributed systems.

πŸ“š Table of Contents

Introduction

Rate limiting is a critical technique for controlling the rate of requests sent or received by a system. It helps protect services from abuse, ensures fair resource allocation, and maintains system stability under high load.

This repository provides:

  • Detailed explanations of rate limiting algorithms
  • Production-ready code examples in multiple languages
  • Best practices and patterns
  • Distributed system considerations
  • Testing strategies

Why Rate Limiting?

Rate limiting serves several crucial purposes:

  1. DoS Protection: Prevent abuse and denial-of-service attacks
  2. Cost Control: Manage infrastructure costs by controlling usage
  3. Fair Usage: Ensure equitable resource distribution among users
  4. Service Quality: Maintain consistent performance for all users
  5. API Monetization: Enable tiered pricing models
  6. Resource Management: Protect downstream services and databases

Core Concepts

Key Terminology

  • Rate: Maximum number of requests allowed in a time window
  • Window: Time period for measuring the rate (e.g., per second, per minute)
  • Identifier: Key used to track rate (e.g., user ID, IP address, API key)
  • Quota: Total allowance over a longer period
  • Burst: Temporary spike in traffic above the normal rate

Common Response Patterns

When a rate limit is exceeded, systems typically:

  • Return HTTP 429 (Too Many Requests)
  • Include headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset
  • Optionally include Retry-After header

Algorithms

This repository covers the following rate limiting algorithms:

1. Token Bucket

2. Leaky Bucket

3. Fixed Window Counter

4. Sliding Window Log

5. Sliding Window Counter

6. Concurrent Requests Limiter

Implementation Examples

Code examples are provided in multiple languages:

  • Typescript: Express middleware, Redis-based
  • Python: Using Redis, in-memory implementations

Each implementation includes:

  • Complete working code
  • Unit tests
  • Performance benchmarks
  • Configuration options

Best Practices

Design Principles

  1. Choose the right algorithm for your use case
  2. Communicate limits clearly through headers
  3. Implement graceful degradation
  4. Monitor and alert on rate limit hits
  5. Make limits configurable without code changes
  6. Consider different dimensions: per-user, per-IP, per-endpoint

Production Considerations

  • Use distributed rate limiting for multi-instance deployments
  • Implement circuit breakers alongside rate limits
  • Cache rate limit decisions when possible
  • Log rate limit violations for analysis
  • Provide webhooks or notifications for quota exhaustion

Distributed Rate Limiting

When running multiple application instances, you need distributed rate limiting:

Solutions

  1. Redis-based: Centralized counter storage
  2. Memcached: Simple distributed caching
  3. Dedicated Services: Kong, Envoy, API Gateway
  4. Custom Solutions: Consistent hashing, gossip protocols

See Distributed Rate Limiting Guide for details.

Testing

Testing Strategies

  • Load testing to verify limits
  • Unit tests for algorithm correctness
  • Integration tests for distributed scenarios
  • Chaos testing for failure scenarios

See Testing Guide for examples.

Directory Structure

.
β”œβ”€β”€ README.md
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ algorithms/
β”‚   β”‚   β”œβ”€β”€ token-bucket.md           ⭐ Complete token bucket guide
β”‚   β”‚   β”œβ”€β”€ leaky-bucket.md           ⭐ Complete leaky bucket guide
β”‚   β”‚   β”œβ”€β”€ fixed-window.md           ⭐ Complete fixed window guide
β”‚   β”‚   β”œβ”€β”€ sliding-window-log.md     ⭐ Complete sliding window log guide
β”‚   β”‚   β”œβ”€β”€ sliding-window-counter.md ⭐ Complete sliding window counter guide
β”‚   β”‚   └── concurrent-requests.md    ⭐ Concurrent requests limiter guide
β”‚   β”œβ”€β”€ algorithm-comparison.md       ⭐ Detailed algorithm comparison
β”‚   β”œβ”€β”€ getting-started.md            ⭐ Quick start guide
β”‚   β”œβ”€β”€ distributed.md                ⭐ Distributed rate limiting guide
β”‚   β”œβ”€β”€ best-practices.md             ⭐ Production best practices
β”‚   └── testing.md                    ⭐ Comprehensive testing guide
β”œβ”€β”€ implementations/
β”‚   β”œβ”€β”€ typescript/
β”‚   β”‚   β”œβ”€β”€ rate-limiter.ts           ⭐ All 6 algorithms in TypeScript
β”‚   β”‚   β”œβ”€β”€ express-middleware.ts     ⭐ Express.js middleware
β”‚   β”‚   β”œβ”€β”€ redis-adapter.ts          ⭐ Redis distributed limiter
β”‚   β”‚   β”œβ”€β”€ examples.ts               ⭐ 12+ usage examples
β”‚   β”‚   β”œβ”€β”€ rate-limiter.test.ts      ⭐ Complete test suite
β”‚   β”‚   β”œβ”€β”€ package.json              ⭐ NPM configuration
β”‚   β”‚   └── tsconfig.json             ⭐ TypeScript config
β”‚   └── python/
β”‚       β”œβ”€β”€ rate_limiter.py           ⭐ All algorithms in Python
β”‚       └── redis_rate_limiter.py     ⭐ Redis implementation
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ api-gateway/
β”‚   β”‚   └── server.ts                 ⭐ Complete API gateway example
β”‚   β”œβ”€β”€ microservices/
β”‚   β”‚   └── services.ts               ⭐ Microservices architecture example
β”‚   └── web-application/
β”‚       └── app.ts                    ⭐ Web application example
└── benchmarks/
    └── index.ts                      ⭐ Performance benchmarks

Resources

Articles & Papers

Tools & Libraries

Contributing

Contributions are welcome (Need more Python implementations)

License

MIT License - see LICENSE file for details.

⭐ Star this repo if you find it helpful!

About

A complete guide to rate limiting algorithms, implementations, and best practices for building robust APIs and distributed systems.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors