Lesson 7 of 9 · 27m
Rate Limits, Queues, and Backpressure
Honor Discord's dynamic rate-limit contract, queue outbound actions, and prevent user bursts or moderation incidents from becoming API brownouts.
The Discord API does not owe your bot a fixed number of requests per second. It owes you feedback. Rate limits are dynamic, scoped, and sometimes global; the production mistake is treating a 429 as an exception instead of a normal control signal. If you build outbound actions as fire-and-forget API calls, the first raid or bulk purge will turn your bot into its own denial-of-service attack.
Read the bucket contract
Discord rate limiting is generally organized by route and bucket, with headers describing current state:
X-RateLimit-Bucket: 8f1d3d...
X-RateLimit-Limit: 5
X-RateLimit-Remaining: 0
X-RateLimit-Reset-After: 2.431
X-RateLimit-Scope: userThe bucket, not just the path, is the identity of the limit. Some routes share limits across resources; some include major parameters such as guild or channel in bucket calculation. A library should handle this centrally, but you still need to know when your design is queueing thousands of actions behind one bucket.
Never hardcode the famous numbers
You will hear numbers such as fifty requests per second. They are folklore at bot scale. Discord can adjust global limits, invalid request thresholds, and route behavior. Build around returned headers, retry-after values, and concurrency caps. A safe executor looks conceptually like this:
const result = await limiter.schedule(bucketKey, async () => {
const response = await discordRequest();
if (response.status === 429) throw new RetryAfter(response.retryAfterSeconds, response.isGlobal);
return response;
});Queue work by consequence
Not all actions deserve the same queue. During a raid, deleting messages, timing out members, updating incident status, and replying to moderators compete for capacity. Separate them:
interactive responses low latency, small concurrency
safety enforcement high priority, bounded concurrency
bulk cleanup slow lane, cancellable
analytics/export best effort, preemptibleInvalid requests are a different danger
400 validation error inspect payload; do not blind retry
401/403 authz failure token incident, permission, or hierarchy changed
404 unknown resource target disappeared; usually terminal
429 rate limited wait according to response
5xx platform failure bounded exponential backoffIdempotency matters here because a timeout does not tell you whether Discord applied the mutation. Check state before repeating irreversible-looking operations.
Design bulk operations as campaigns
Purging 10,000 messages or timeouting 500 raid accounts is a campaign, not a loop someone pasted into a command handler. Give it a durable ID, progress state, cancellation, actor attribution, rate-limit-aware executor, and an audit trail. Then a deploy or process restart is inconvenient rather than destructive.
Field notes
- A global 429 affects more than the route that triggered it. Treat it as fleet-level backpressure.
- Parallel shards do not magically create independent global budgets. Coordinates matter across processes.
- Test with queued synthetic workloads and induced retry-after responses; one quiet guild proves almost nothing.
Was this lesson helpful?