Перейти к содержимому
Skip to content
← All articles

Rate Limiting Strategies for Web APIs and Applications

Rate limiting controls how many requests a client can make to your API документацию or application within a given time period. It protects your infrastructure from abuse, ensures fair usage, and prevents a single user from monopolizing resources.

Why Rate Limiting Matters

Rate Limiting Algorithms

Fixed Window

Count requests in fixed time windows (e.g., per minute). Reset the counter at the start of each window.

Sliding Window Log

Store a timestamp for every request. Count requests within the last N seconds. Precise but memory-intensive.

Sliding Window Counter

Hybrid approach: use fixed windows but weight the previous window proportionally. If you're 30% into the current window, count 70% of the previous window's requests plus 100% of the current.

Token Bucket

Imagine a bucket that fills with tokens at a steady rate. Each request consumes a token. If the bucket is empty, the request is rejected. Allows controlled bursts (bucket can accumulate tokens).

Leaky Bucket

Requests enter a queue (bucket) and are processed at a fixed rate. If the queue is full, new requests are dropped. Enforces a perfectly smooth output rate.

Implementation with Redis

-- Sliding window counter in Redis (Lua script)
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local now = tonumber(ARGV[3])

-- Remove expired entries
redis.call('ZREMRANGEBYSCORE', key, 0, now - window)

-- Count current requests
local count = redis.call('ZCARD', key)

if count < limit then
    redis.call('ZADD', key, now, now .. math.random())
    redis.call('EXPIRE', key, window)
    return 0  -- allowed
else
    return 1  -- rate limited
end

HTTP Response Headers

Communicate rate limit status to clients:

HTTP/1.1 200 OK
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 42
X-RateLimit-Reset: 1679529600
Retry-After: 30

When rate limited, return 429 Too Many Requests:

HTTP/1.1 429 Too Many Requests
Retry-After: 30
Content-Type: application/json

{"error": "Rate limit exceeded. Try again in 30 seconds."}

What to Rate Limit By

Best Practices

Conclusion

Rate limiting is essential infrastructure for any web API. Start with the sliding window counter (best balance of simplicity and accuracy), implement per-key/per-user limits, and always communicate limits through headers. Well-implemented rate limiting protects your service while maintaining a good developer experience.

Check your website right now

Check now →
More articles: Security
Security
Security Headers: CSP, HSTS, X-Frame-Options and More
10.03.2025 · 16 views
Security
Security Headers: The Complete Guide
14.03.2026 · 18 views
Security
HSTS and Preload List: Complete Implementation Guide
16.03.2026 · 14 views
Security
API Security: Best Practices for Protection
11.03.2026 · 13 views