tezvyn:

Rate Limiting: Your API's Bouncer

AI-drafted, machine-checkedSource: Wikipedia: Rate limitingintermediate

Rate limiting acts as a bouncer for your API, controlling traffic to protect your service. It's used on public APIs to prevent abuse, ensure fair usage, and defend against denial-of-service attacks.

WHY IT EXISTS: Any public-facing service has finite resources like CPU, memory, and bandwidth. Without controls, a single malicious or poorly-coded client could consume all resources, making the service unavailable for everyone else. Rate limiting was created to ensure stability, security, and fair access.

THE MENTAL MODEL: Think of a rate limiter as a bouncer at a club door. The bouncer has a simple rule: only let in a certain number of people per minute. If a large group arrives all at once, they have to wait. If they try to rush the door, they get rejected. The bouncer doesn't care who you are, just how many are trying to get in right now from your group, which is identified by your IP address or API key.

HOW IT WORKS: Rate limiting is typically implemented using algorithms like token bucket, leaky bucket, or fixed/sliding window counters. A server tracks request counts per client in a fast data store like Redis. For example, using a token bucket, each client has a bucket refilled with 'tokens' at a steady rate. Each request consumes one token. If the bucket is empty, the server rejects the request with an HTTP 429 'Too Many Requests' status code until a new token is added.

WHEN TO USE IT: Use rate limiting for any public-facing API to prevent abuse and ensure availability. It's critical for several scenarios: first, on login endpoints to slow down brute-force password attacks; second, on expensive computational endpoints to prevent resource exhaustion; and third, on any API offered as a product to enforce usage tiers and prevent web scraping.

WHEN NOT TO USE IT: Avoid applying overly strict rate limits on critical, low-cost internal service-to-service communication where high throughput is expected and trust is high. Also, be cautious with global rate limits that don't distinguish between users, as one bad actor could block the entire service for everyone. The key is to apply limits granularly per-user or per-IP.

ONE CANONICAL EXAMPLE: A social media API might allow 150 read requests per user per 15-minute window. A developer building a client must respect this limit. If their app tries to fetch 200 updates at once, the first 150 will succeed, and the next 50 will receive an HTTP 429 error. The client is then expected to wait and retry after the window resets.

Read the original → en.wikipedia.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.