iluwatar / java-design-patterns

Design patterns implemented in Java
https://java-design-patterns.com
Other
88.22k stars 26.18k forks source link

Rate Limiting pattern #2973

Open iluwatar opened 1 month ago

iluwatar commented 1 month ago

Description: The Rate Limiting design pattern is crucial in a microservices architecture to prevent abuse and ensure fair usage of resources. This pattern restricts the number of requests a service can handle within a specified timeframe, thereby enhancing reliability and performance by preventing overloading. This implementation will involve creating a mechanism to count and limit the number of incoming requests to a service, and take appropriate actions when the limit is exceeded.

Main Elements of the Pattern:

  1. Rate Limiter: A component responsible for tracking the number of requests and enforcing the rate limit.
  2. Policy Configuration: Defines the rate limit rules, such as the number of requests allowed per time unit and what actions to take when the limit is exceeded.
  3. Client Identification: Mechanism to identify clients (e.g., IP address, API key) to apply rate limits individually.
  4. Throttling: Delaying or rejecting requests when the rate limit is exceeded.
  5. Monitoring and Logging: Tools to monitor and log rate-limiting activities for analysis and tuning.

References:

  1. Rate Limiting Pattern - Microservices.io
  2. API Rate Limiting - Amazon API Gateway
  3. Rate Limiting Strategies and Techniques
  4. Project Contribution Guidelines

Acceptance Criteria:

  1. Implement a rate limiter that can be configured with different policies.
  2. Provide configuration options to define rate limit rules (e.g., requests per second).
  3. Ensure the rate limiter can identify different clients and apply rate limits accordingly.
  4. Implement mechanisms to throttle or reject requests exceeding the limit.
  5. Include logging to capture rate-limiting events for monitoring purposes.
  6. Provide comprehensive documentation and examples demonstrating how to use the rate limiter.
  7. Write unit and integration tests to ensure the rate limiter works as expected under various scenarios.