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
211
Related Errors
403
Laravel
AuthorizationException (403)
The authenticated user lacks permission to perform the requested action.
Database
Data Truncation
The data being inserted or updated is too long for the column's defined size.
PHP
Maximum Execution Time Exceeded
A script ran longer than the allowed maximum execution time limit.
PHP
Deprecated Function Usage
The code uses a function that has been deprecated and may be removed in future PHP versions.