Limitador de Tasa
Throttling · Token bucket
Escrito por Darjus Kazakevic · revisado el
A budget per caller, and a 429 that says when to come back.
A rate limiter checks each request against the caller's budget before any work happens. The caller is identified by a key: an API key, a user ID or an IP address. Requests over the budget get HTTP 429 Too Many Requests. GitHub, for example, allows 5,000 API requests per hour for a signed-in user.
The limit stops one noisy client from using shared capacity, and slows abuse such as password guessing. Where it runs matters. At the proxy or gateway it protects every service; inside a service it can know about tenants and plans. Many systems use both.
En esencia — Give each caller its own budget of requests per time window, and refuse the excess with 429 and a Retry-After header.
Cuándo recurrir a él
Recurre a él cuando
- A public API where one client can use up capacity all clients share
- Endpoints that attract abuse, such as login, signup or search
- Paid plans that promise each customer a fixed request quota
Reconsidéralo cuando
- Protecting yourself from a slow dependency: use a circuit breaker
- Total load is the problem, not one caller: shed load instead
- Internal batch jobs that should wait in a queue, not be refused
Conceptos clave
- Token bucket
- Each key has a bucket of tokens that refills at a fixed rate up to a maximum size. A request takes one token or is refused. The rate sets the average; the size sets the largest burst.
- Fixed window
- Count requests per key in a fixed window, such as each minute, and reset at the boundary. It is one counter, so it is cheap. But a client can send the full limit at 12:00:59 and again at 12:01:00.
- Sliding window
- Counts requests over the last N seconds at any moment. A sliding log stores every timestamp: exact, but heavy on memory. A sliding counter weights the previous window's count: close enough, and cheap.
- Shared counters
- With many servers, the counter must live in one shared place, usually Redis. Update it in one atomic step, INCR or a Lua script, so two servers cannot both spend the last token.
Reglas de diseño
- Limit per key, never globally
- One shared counter lets the noisiest client decide when everyone else is blocked. Give each API key, user or IP its own budget, and use a global limit only as a last guard.
- Always say when to come back
- Return 429 with Retry-After. A bare 429 makes clients guess, and their retries arrive together as a second spike. Good clients also add random jitter.
- Keep the counter shared and atomic
- With several servers, keep counters in Redis and update them in one step: INCR, or a Lua script for a token bucket. A read-then-write lets two servers spend the same token.
- Decide what happens when the limiter fails
- If Redis is down, failing open keeps the API up but unprotected; failing closed protects it but blocks everyone. Choose on purpose, per endpoint, before the outage.
Errores habituales
- Keying by IP alone. Thousands of users behind one office or mobile network share an IP, share a budget and get blocked together.
- Counting in each server's memory. With N servers the real limit becomes N times the configured one, and it changes when you scale.
- Returning 429 without Retry-After. Clients retry at once, and the limited traffic doubles instead of slowing down.
- A fixed window with a hard reset. Clients learn the boundary and send double the limit within two seconds.