0
0
Laravelframework~8 mins

Rate limiting in Laravel - Performance & Optimization

Choose your learning style9 modes available
Performance: Rate limiting
HIGH IMPACT
Rate limiting controls how often users can make requests, impacting server load and response times.
Preventing excessive API requests from users
Laravel
use Illuminate\Cache\RateLimiter;

public function handle(Request $request, Closure $next) {
  $key = $request->ip();
  $maxAttempts = 60;
  $decayMinutes = 1;

  if (app(RateLimiter::class)->tooManyAttempts($key, $maxAttempts)) {
    return response('Too many requests', 429);
  }

  app(RateLimiter::class)->hit($key, $decayMinutes * 60);
  return $next($request);
}
Limits requests per user, reducing server load and keeping response times fast.
📈 Performance GainPrevents server overload; maintains low latency and stable INP
Preventing excessive API requests from users
Laravel
public function handle(Request $request, Closure $next) {
  // No rate limiting
  return $next($request);
}
No limits cause server overload under high traffic, slowing responses and risking downtime.
📉 Performance CostBlocks rendering for many users during overload; increases server CPU and memory usage
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No rate limitingN/AN/AN/A[X] Bad
Laravel built-in rate limiting middlewareN/AN/AN/A[OK] Good
Rendering Pipeline
Rate limiting happens before request processing, reducing server work and avoiding slow responses that delay browser rendering.
Server Request Handling
Response Time
⚠️ BottleneckServer CPU and memory under high request volume
Core Web Vital Affected
INP
Rate limiting controls how often users can make requests, impacting server load and response times.
Optimization Tips
1Always apply rate limiting early in request handling to reduce server load.
2Use Laravel's built-in rate limiter for efficient and easy implementation.
3Monitor 429 responses in DevTools Network tab to verify rate limiting is active.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using rate limiting in Laravel?
AIt prevents server overload and keeps response times fast
BIt reduces the size of CSS files
CIt improves browser rendering speed directly
DIt caches all user requests
DevTools: Network
How to check: Open DevTools, go to Network tab, make rapid repeated requests to API endpoint, observe response status codes.
What to look for: Look for 429 status codes indicating rate limiting is active; absence means no limits.