JDA provides sophisticated rate limit handling to ensure your bot respects Discord’s API rate limits. This guide covers advanced rate limit configuration and custom implementation.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/discord-jda/JDA/llms.txt
Use this file to discover all available pages before exploring further.
Understanding Rate Limits
Discord uses several types of rate limits:- Per-route rate limits: Apply to specific API endpoints
- Global rate limits: Apply to the entire bot token
- Cloudflare rate limits: Apply to the IP address
Rate Limit Headers
JDA reads the following headers from Discord’s API responses:Total time (in seconds) until the current rate limit bucket resets. Can have decimals for millisecond precision.
Epoch time (seconds since January 1, 1970 UTC) at which the rate limit resets.
The number of requests that can be made to this endpoint.
The number of remaining requests that can be made.
A unique string denoting the rate limit being encountered (non-inclusive of top-level resources in the path).
Returned only on HTTP 429 responses. Value can be
user, global, or shared.Returned only on HTTP 429 responses if the rate limit encountered is the global rate limit.
The number of seconds to wait before submitting another request.
Custom Rate Limiter
You can implement a custom rate limiter by implementing theRestRateLimiter interface:
Rate Limit Configuration
TheRateLimitConfig class provides configuration options for the rate limiter:
The scheduler used to schedule rate-limit tasks and delayed executions.
The elastic pool used to execute blocking HTTP requests. Can scale up and down based on demand.
The global rate-limit store, which can be shared between multiple JDA instances.
Whether to use
X-RateLimit-Reset-After (relative) instead of X-RateLimit-Reset (absolute). Recommended to avoid NTP sync issues.Example Configuration
Shared Global Rate Limits
When running multiple bot instances (sharding), you can share global rate limit information:Priority Requests
TheWork interface includes priority detection:
Best Practices
- Use relative timing: Set
isRelativetotrueto avoid clock synchronization issues - Share global limits: When sharding, share the
GlobalRateLimitinstance across all shards - Respect retry headers: Always honor the
Retry-Afterheader on 429 responses - Monitor bucket hashes: Use the
X-RateLimit-Bucketheader to properly identify unique rate limit buckets - Don’t cancel priority requests: Requests marked as priority should never be cancelled
Troubleshooting
Hitting Rate Limits Frequently
If you’re consistently hitting rate limits:- Review your request patterns for inefficient API usage
- Implement caching to reduce redundant requests
- Use bulk operations where possible (e.g., bulk delete messages)
- Consider implementing request queuing with backoff
Clock Synchronization Issues
If you experience issues with absolute timestamps:- Enable relative timing in your
RateLimitConfig - Ensure your server’s clock is synchronized with NTP
- Use
X-RateLimit-Reset-Afterinstead ofX-RateLimit-Reset