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
18
Related Errors
419
Laravel
TokenMismatchException (CSRF)
The CSRF token validation failed during a POST, PUT, or DELETE request.
Database
Database Connection Refused or Timeout
The database server is not reachable or took too long to respond.
Database
Table Doesn't Exist
The SQL query references a table that doesn't exist in the database.
500
Laravel
Laravel 500 Internal Server Error
A generic server-side error indicating something went wrong during request processing.