Rate limiting and API protection
Without limits, an API gets hammered: login brute-force, scraping, unintended abuse from a buggy client, or a cost spike on your LLM calls. Here's how to limit cleanly, without hurting real users.
Without limits, an API gets hammered: login brute-force, scraping, unintended abuse from a buggy client, or a cost spike on your LLM calls. Here's how to limit cleanly, without hurting real users.
On login, an attacker tries thousands of passwords. Limiting attempts breaks the attack.
A bot scrapes your data, or a buggy client loops. The limit protects your resources.
An endpoint calling an LLM without a limit is an open bill. A per-user cap bounds it.
One heavy consumer shouldn’t degrade everyone else’s service. The limit keeps sharing fair.
The simplest and most common algorithm: each key (IP or user) has a counter that fills over a time window. Beyond the limit, return a 429. Redis is perfect for it — atomic and shared across all your instances.
Limit per logged-in user when you can, not just per IP: behind a corporate NAT, hundreds of users share the same IP.
Watching 429s is observability: I cover logs and alerts in monitoring: logs, alerts, uptime.
Rate limiting is one of the best effort-to-payoff protections: a few lines with Redis, and you close the door to brute-force, scraping and cost overruns. Tight where it matters, generous elsewhere, and always return a polite 429.
MDN — HTTP 429 ↗