API Rate Limiting & Throttling
Learn how rate limiting protects APIs from abuse, brute force, and denial of service, and how to implement token-bucket and sliding-window strategies.
API Rate Limiting & Throttling is a free Secure Coding & OWASP Top 10 for Backend lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Secure Coding & OWASP Top 10 for Backend learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Rate Limiting?
Rate limiting caps how many requests a client can make in a time window. It protects APIs from brute-force attacks, scraping, accidental loops, and denial-of-service.
It is a key control listed under API security best practices.
Throttling vs Limiting
Rate limiting rejects requests over a hard cap; throttling slows them down (queuing or delaying) instead of rejecting outright. Both manage load and abuse, often used together.
What to Limit On
Choose a key to count requests against:
- API key or user ID for authenticated traffic
- IP address for anonymous traffic
- Endpoint sensitivity (stricter limits on login)
Combining keys gives finer control and resists simple bypasses.
Fixed Window
The simplest approach counts requests in a fixed time window, resetting the counter each period. It is easy but allows bursts at window edges (twice the limit across a boundary).
import time
window = {}
LIMIT = 5
PERIOD = 60
def allow(key):
now = int(time.time() // PERIOD)
count = window.get((key, now), 0)
if count >= LIMIT:
return False
window[(key, now)] = count + 1
return TrueToken Bucket
The token bucket refills tokens at a steady rate up to a capacity. Each request consumes a token; an empty bucket means the request is rejected. It allows controlled bursts while enforcing an average rate.
import time
class TokenBucket:
def __init__(self, rate, capacity):
self.rate = rate
self.capacity = capacity
self.tokens = capacity
self.last = time.time()
def allow(self):
now = time.time()
self.tokens = min(self.capacity, self.tokens + (now - self.last) * self.rate)
self.last = now
if self.tokens >= 1:
self.tokens -= 1
return True
return FalseSliding Window
The sliding window tracks timestamps of recent requests and counts only those within the last N seconds. It avoids the burst problem of fixed windows at the cost of more bookkeeping.
Distributed Rate Limiting
With multiple servers, counters must be shared. A central store like Redis holds the counters so limits apply across the whole cluster, not per instance. Use atomic operations to avoid race conditions.
Communicating Limits
Tell clients about their limits with response headers so well-behaved clients can back off.
headers = {
'X-RateLimit-Limit': '100',
'X-RateLimit-Remaining': '42',
'X-RateLimit-Reset': '1717000000',
'Retry-After': '30',
}
for k, v in headers.items():
print(k + ': ' + v)Status Codes
Return 429 Too Many Requests when a client exceeds the limit, ideally with a Retry-After header. This is the standard signal clients and SDKs expect.
Protecting Sensitive Endpoints
Apply stricter limits to high-risk endpoints like login, password reset, and OTP verification. Tight limits here directly blunt brute-force and credential-stuffing attacks.
- Login: a few attempts per minute
- Password reset: a few per hour
- General reads: generous limits
Avoiding Pitfalls
Watch for bypasses: rotating IPs, missing limits on some routes, and limits that reset on server restart. Place rate limiting at the gateway or middleware layer so every route is covered consistently.
Quick Check
Test your understanding of rate limiting.
Recap
You learned why APIs need rate limiting, how to choose a limiting key, and the trade-offs of fixed-window, token-bucket, and sliding-window strategies. You also saw distributed limiting with Redis, the 429 response, and stricter limits for sensitive endpoints.
Frequently asked questions
Is the “API Rate Limiting & Throttling” lesson free?
Yes — the full text of “API Rate Limiting & Throttling” is free to read here on the web, and the Secure Coding & OWASP Top 10 for Backend course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Secure Coding & OWASP Top 10 for Backend course, upgrade to CoddyKit PRO.
What will I learn in “API Rate Limiting & Throttling”?
Learn how rate limiting protects APIs from abuse, brute force, and denial of service, and how to implement token-bucket and sliding-window strategies. You practise Secure Coding & OWASP Top 10 for Backend with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Secure Coding & OWASP Top 10 for Backend?
No prior experience is required. Secure Coding & OWASP Top 10 for Backend on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “API Rate Limiting & Throttling” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Secure Coding & OWASP Top 10 for Backend lesson?
Yes. Every Secure Coding & OWASP Top 10 for Backend lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Designing Secure RESTful APIs
- GraphQL API Security
- Preventing SSRF Attacks
- API Rate Limiting & Throttling