stackbook.io
🔍
traffic-managementestablished · low operational burden

Rate Limiting

Also known as: rate-limit, throttling

Intent

Control the rate of requests to a service or API to prevent abuse, ensure fair usage, and protect against denial-of-service attacks.

Problem

Without rate limiting, malicious or accidental high-volume traffic can overwhelm services, cause cascading failures, and degrade availability for all users. Uncontrolled requests consume resources and increase costs.

Forces

  • •Legitimate users need reasonable access to services
  • •Attackers may attempt to flood services with requests
  • •Different endpoints have different risk profiles
  • •Rate limiting must not degrade user experience for valid traffic
  • •Operational overhead of managing rate limits should be minimal

Solution

Rate Limiting

Control the rate of requests to a service or API to prevent abuse, ensure fair usage, and protect against denial-of-service attacks.

Core Algorithms

1. Token Bucket

Tokens are added to a bucket at a steady rate. Each request consumes one token. If the bucket is empty, the request is rejected. Allows brief bursts up to the bucket capacity. Example: 100 tokens per minute, bucket size 20.

2. Sliding Window

A fixed-size window slides over time. Counts requests within the current window. The window slides forward, old requests drop off. More precise than fixed window. Example: 100 requests per 60-second sliding window.

3. Fixed Window

Counter resets at fixed intervals (e.g., per minute). Simple to implement and understand. Can be gapped at window boundaries. Example: 100 requests per minute, counter resets at :00.

Implementation Notes

  • Storage: Use Redis for distributed counter coordination
  • Algorithm choice: Sliding window or token bucket for better accuracy
  • Client classification: IP-based + user-based limiting combinations
  • HTTP response: Return 429 with Retry-After header
  • Monitoring: Log rate limit events for observability

When to Use

  • Any public API or service exposed to external clients
  • Protecting against DoS attacks and abuse
  • Ensuring fair usage among multiple tenants
  • When upstream dependencies need protection from cascading failures

When Not to Use

  • Internal APIs with trusted clients and known load patterns
  • Real-time systems where rate limiting introduces unacceptable latency
  • Very short-lived internal services with controlled deployment

Cost Profile

| Category | Assessment | |----------|-----------| | Infrastructure | Low — Redis or in-memory counter | | Operational | Low — once configured, self-regulating | | Cognitive | Medium — choosing and tuning the right algorithm |

Real-World Examples

  • Twitter API rate limiting: Official rate limit tiers and headers
  • GitHub API rate limiting: Per-endpoint limits and authentication-based limits

Alternatives

✓ When to Use

  • •Any public API or service exposed to external clients
  • •Protecting against DoS attacks and abuse
  • •Ensuring fair usage among multiple tenants
  • •When upstream dependencies need protection from cascading failures

✗ When Not to Use

  • •Internal APIs with trusted clients and known load patterns
  • •Real-time systems where rate limiting introduces unacceptable latency
  • •Very short-lived internal services with controlled deployment

Pros

  • +Protects service availability under load
  • +Fair resource allocation among clients
  • +Well-understood pattern with standard implementations
  • +Enables graceful degradation via HTTP 429

Cons

  • −Can reject legitimate traffic if thresholds are too aggressive
  • −Requires distributed counter coordination
  • −Implementation complexity for accurate windowing
  • −User experience impact if not handled gracefully

Cost Profile

Infrastructure

Low — Redis or in-memory counter

Operational

Low — once configured, self-regulating

Cognitive

Medium — choosing and tuning the right algorithm

Failure Modes

  • Counter overflow: Metrics become inaccurate

  • Single point of failure: Local counters in single-instance deployments

  • Incorrect window boundaries: Gapping at interval boundaries

  • Bypassed limits: Clients rotating IPs or using proxies

Real-World Examples

Alternatives

  • api-gateway
  • circuit-breaker
  • quota-management

Related Patterns

    Competency Domains

    distribution communicationreliability ops