Urgent.News

What's breaking now, across thousands of outlets.

Tech

API Rate Limiting: What Actually Breaks When You Get It Wrong

Most teams add rate limiting to their API as an afterthought — usually right after something has already gone wrong. A scraper hammers an endpoint, a client integration goes into a retry loop, or a single misbehaving user takes down a shared resource for everyone else. By then, you're not designing a rate limiter, you're firefighting. This post walks through the rate limiting mistakes that show…

Many developers add rate limiting to their APIs after problems have already arisen. Scraper tools, faulty client integrations, or a single misbehaving user can overwhelm shared resources. This article explores common rate limiting pitfalls in production systems, why they occur, and presents a more resilient approach. Simply setting a single request limit (e.g., X per minute per API key) is insufficient because all requests don't have equal costs.

A cached read and a resource-intensive operation are treated equally under a flat limit. Additionally, bursts of traffic are normal, not exceptions. Setting a limit that resets at fixed intervals creates edge-of-window spikes, where 100 requests at 0:59 and 100 more at 1:01 technically comply with the rules but overwhelm the system.

Better approaches include using sliding window or token bucket algorithms that allow small bursts while enforcing an average usage rate. Weighting endpoints based on cost rather than just counting requests prevents a few expensive calls from causing more damage than numerous cheap ones. Differentiating between authenticated and unauthenticated traffic is also essential; anonymous traffic should have stricter limits than identified clients.

Clear signals such as a 429 status code with Retry-After headers and informative rate limit metrics (limit, remaining, reset time) help clients manage retries effectively. A significant mistake occurs when rate limiting isn't uniformly applied across multiple servers. If each instance maintains its own in-memory counter, a client could multiply their effective limit by the number of instances, overwhelming the system.

Centralizing the counter, typically using Redis, ensures all instances check and decrement against the same source of truth. While this adds a small latency per request, it's crucial for a rate limiter to actually limit traffic. For existing APIs, a practical starting point is to first log and monitor traffic patterns before implementing real limits.

Limit per authenticated client, not IP, when identities are available. Opt for token bucket or sliding window algorithms over fixed reset windows. Centralize limit counters if running multiple instances. Provide clear headers and Retry-After values on every 429 response. Alerts on clients nearing their limits can indicate bugs instead of malicious intent.

Proper rate limiting is invisible when functioning correctly and becomes obvious when it fails. The article is based on real-world experiences in building and scaling backend systems. For further insights on API architecture decisions, refer to Web Matrix Lab.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

At the edge, the number that matters is memory - not throughput (specially in Ramageddon)

We rebuilt LF Edge eKuiper in Rust and ran it against eKuiper, Telegraf and Redpanda Connect on five real MQTT workloads — one core, 1 GB of memory, and output checked message-by-message.

  • Memory usage, not throughput, is key metric in resource-constrained environments
  • Rekuiper, Rust-based engine, maintained memory usage between 5 and 10 MB
  • Design choices include bounded queues, incremental window aggregation, offline sink cache

More from Sunday 13 September →