Skip to content
All notes
30API TestingAPI TestingPerformance

What is API Rate Limiting and how do you test it?

Rate Limiting is a technique used to control the number of requests a client can make to an API within a specific time period, preventing abuse and ensuring fair usage.

Why Rate Limiting is Important

  • Prevents API abuse and DDoS attacks
  • Ensures fair resource distribution
  • Protects server from overload
  • Manages costs for cloud-based APIs
  • Maintains quality of service for all users

Common Rate Limiting Strategies

  • Fixed Window: X requests per minute/hour
  • Sliding Window: Rolling time window
  • Token Bucket: Tokens replenish over time
  • Leaky Bucket: Requests processed at constant rate

Rate Limit Response Headers

X-RateLimit-Limit: 100
X-RateLimit-Remaining: 75
X-RateLimit-Reset: 1640995200
Retry-After: 60

Testing Rate Limiting

  1. Test Within Limits: Verify API works normally
  2. Test Exceeding Limits: Send requests beyond limit
  3. Verify Status Code: Should return 429 (Too Many Requests)
  4. Check Headers: Validate rate limit headers
  5. Test Reset: Verify limits reset after time window
  6. Test Different Users: Ensure limits are per-user/per-key

Example Test Scenario

// Send 100 requests (within limit)
for (let i = 0; i < 100; i++) {
    // Should return 200 OK
}

// Send 101st request (exceeds limit)
// Should return 429 Too Many Requests
// Response should include Retry-After header