← Workflows

阅读中文版本

API Rate Limit Implementation Workflow

Implement rate limiting on API endpoints using a token bucket algorithm with Redis for distributed enforcement.

intermediate~60 minapirate-limitworkflow

Objective

Add distributed rate limiting to protect API endpoints from abuse and ensure fair usage.

Environment

  • An API server with Redis access
  • A rate limiting library
  • Understanding of token bucket algorithm

Steps

1. Choose limits

Define rate limits per endpoint based on usage patterns and sensitivity.

Expected state: A rate limit policy is documented per endpoint.

2. Implement middleware

Add rate limiting middleware to the API using Redis as the shared store.

Expected state: Rate limiting is active on all configured endpoints.

3. Add headers

Include X-RateLimit-Limit, X-RateLimit-Remaining, and Retry-After headers in responses.

Expected state: Rate limit information is visible in response headers.

4. Test limits

Send requests exceeding the limit and verify 429 responses with correct headers.

Expected state: The API returns 429 when the limit is exceeded.

5. Monitor usage

Set up dashboards to monitor rate limit hits and adjust limits as needed.

Expected state: Rate limit metrics are visible in monitoring.

Decision points

Per-IP or per-user limits?

  • Per-IP for public APIs
  • Per-user for authenticated APIs

Use per-user limits for authenticated requests; per-IP for unauthenticated endpoints.

Expected outcome

All API endpoints have rate limiting with Redis-backed distributed enforcement and informative headers.

Verification

Send requests above the limit and confirm 429 responses; verify the same limit applies across multiple server instances.

Cleanup

Remove rate limiting middleware if it causes issues; investigate false positives before re-enabling.

Rate limiting protects both the server and fair users

Without rate limiting, a single client can degrade service for everyone. The token bucket algorithm allows bursts while enforcing a steady-state limit.

const limiter = rateLimit({
  store: new RedisStore({ client: redis }),
  max: 100,
  windowMs: 60000,
});
app.use(limiter);

Implementation Notes

This workflow provides a structured approach to ensure consistent results across team members and environments.

Common Pitfalls

Skipping verification steps or rushing through the process can lead to incomplete results. Always follow each step in order and verify before proceeding.