DEV Community

丁久
丁久

Posted on • Originally published at dingjiu1989-hue.github.io

API Rate Limiting Implementation

This article was originally published on AI Study Room. For the full version with working code examples and related articles, visit the original post.

API Rate Limiting Implementation

API Rate Limiting Implementation

API Rate Limiting Implementation

API Rate Limiting Implementation

API Rate Limiting Implementation

API Rate Limiting Implementation

API Rate Limiting Implementation

API Rate Limiting Implementation

API Rate Limiting Implementation

API Rate Limiting Implementation

Why Rate Limiting Matters

Rate limiting protects APIs from abuse, DoS attacks, and unintentional overload. It ensures fair usage and maintains service quality for all consumers.

Rate Limiting Algorithms

Token Bucket

The most popular algorithm, allowing bursts while maintaining average limits:

import time

import threading

class TokenBucket:

def init(self, rate, capacity):

self.rate = rate # Tokens per second

self.capacity = capacity

self.tokens = capacity

self.last_refill = time.monotonic()

self.lock = threading.Lock()

def consume(self, tokens=1):

with self.lock:

self._refill()

if self.tokens >= tokens:

self.tokens -= tokens

return True

return False

def _refill(self):

now = time.monotonic()

elapsed = now - self.last_refill

self.tokens = min(self.capacity,

self.tokens + elapsed * self.rate)

self.last_refill = now

Usage

bucket = TokenBucket(rate=10, capacity=20) # 10 req/s, burst 20

if bucket.consume():

process_request()

else:

return "429 Too Many Requests"

Sliding Window Log

More precise but memory-intensive:

from collections import deque

import time

class SlidingWindowLog:

def init(self, window_size=60, max_requests=100):

self.window_size = window_size

self.max_requests = max_requests

self.log = deque()

def allow_request(self):

now = time.time()


Read the full article on AI Study Room for complete code examples, comparison tables, and related resources.

Found this useful? Check out more developer guides and tool comparisons on AI Study Room.

Top comments (0)