Course contentsShow
← Security
Lesson 1013 of 3,05221. Web Security - API SecurityFree lesson

Rate Limiting Fundamentals and Algorithms

Understand rate limiting principles, token bucket, leaky bucket, and fixed/sliding window algorithms for APIs.

Rate Limiting Fundamentals and Algorithms

What you'll learn: How rate limiting protects APIs from abuse and the core algorithms that control request flow.

What is Rate Limiting?

Rate limiting controls how many requests a client can make to your API within a time window. Without it, attackers can overwhelm your servers with brute-force attacks, credential stuffing, or denial-of-service attempts. Even legitimate users can accidentally drain resources with buggy clients.

Think of rate limiting like a bouncer at a club: they control how many people enter per hour to prevent overcrowding.

Core Rate Limiting Algorithms

Token Bucket

Imagine a bucket that holds tokens. Each token represents permission for one request. Tokens are added at a steady rate (e.g., 10 per second). When a request arrives, it consumes a token. If the bucket is empty, the request is rejected. This allows short bursts of traffic while maintaining average limits.

Example: 100 tokens capacity, refill at 10/second. You can burst 100 requests immediately, then sustain 10 per second.

Leaky Bucket

Requests enter a queue (the bucket) and are processed at a constant rate—like water dripping from a leaky bucket. Incoming requests that exceed the bucket's capacity overflow and are rejected. This smooths out traffic spikes into a steady stream.

Example: Process exactly 5 requests per second, regardless of incoming rate. Excess requests are dropped.

Fixed Window

Count requests within fixed time periods (e.g., 0-60 seconds). Reset the counter when the window expires. Simple but allows bursts at window boundaries (99 requests at 59s, 100 more at 1s = 199 in 2 seconds).

Sliding Window

Track requests using a rolling time window. More accurate than fixed windows—examines the exact past N seconds, preventing boundary exploits.

Key Takeaway: Rate limiting is essential for API security—token bucket allows bursts with average limits, leaky bucket enforces steady processing, while fixed and sliding windows track requests over time periods with varying accuracy.