Rate Limit Exceeded (429)
Too many requests were sent in a given amount of time.
Explanation
Rate limit exceeded occurs when a client sends more requests than the server allows within a specified time window. Most APIs and web servers implement rate limiting to prevent abuse, ensure fair usage, and protect against DDoS attacks. The HTTP 429 status code indicates the rate limit has been exceeded. The server typically includes Retry-After and X-RateLimit-* headers indicating when the client can resume requests. Common scenarios include scraping, brute force attacks, and misbehaving clients.
Common Causes
- Too many requests in short time
- Missing rate limit handling
- No caching of API responses
- Aggressive polling or scraping
- Misconfigured client retry logic
Solution
Implement exponential backoff when retrying after rate limits. Respect the Retry-After header returned by the server. Use request queuing or batching to reduce the number of individual requests. Cache API responses to avoid redundant requests. Check API documentation for rate limit information and adjust request frequency. Implement client-side rate limiting to prevent hitting server limits. Use API keys for higher rate limit tiers when available. For Laravel, use the built-in RateLimiter facade for custom rate limiting.
Code Example
// Laravel rate limiting middleware
Route::middleware('throttle:60,1')->group(function () {
Route::get('/api/data', [DataController::class, 'index']);
});
// Custom rate limit response
protected function respondToRateLimited(Request $request)
{
return response()->json([
'message' => 'Too many requests. Please try again later.',
], 429)->header('Retry-After', 60);
}
// Client-side exponential backoff
async function fetchWithRetry(url, options = {}, retries = 3) {
for (let i = 0; i < retries; i++) {
const response = await fetch(url, options);
if (response.status === 429) {
const retryAfter = response.headers.get('Retry-After') || 60;
await new Promise(r => setTimeout(r, retryAfter * 1000));
continue;
}
return response;
}
throw new Error('Rate limit exceeded after retries');
}
Error Information
Language
php
Framework
laravel
Difficulty
Intermediate
Views
210
Related Errors
PHP
Parse Error: Syntax Error
PHP detected a syntax error in the source code that prevents parsing.
Laravel
QueryException (SQL Errors)
A database query failed due to invalid SQL, constraint violations, or connection issues.
Laravel
SerializationException
Failed to serialize or unserialize data for caching, queues, or sessions.
422
Laravel
ValidationException (422)
Request data failed validation rules defined in the controller or form request.