Rate limiting helps control how many times a user or system can make requests in a short time. It stops overload and keeps apps running smoothly.
Rate limiting in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
use Illuminate\Support\Facades\RateLimiter; use Illuminate\Cache\RateLimiting\Limit; RateLimiter::for('key', function (\Illuminate\Http\Request $request) { return Limit::perMinute(10); });
Use RateLimiter::for to define limits by a key name.
Limit::perMinute(10) means max 10 requests per minute.
use Illuminate\Http\Request; use Illuminate\Cache\RateLimiting\Limit; RateLimiter::for('login', function (Request $request) { return Limit::perMinute(5)->by($request->ip()); });
use Illuminate\Http\Request; use Illuminate\Cache\RateLimiting\Limit; RateLimiter::for('api', function (Request $request) { return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); });
use Illuminate\Cache\RateLimiting\Limit; RateLimiter::for('global', function () { return Limit::perMinute(100); });
This example sets a rate limit of 5 login attempts per minute per IP address. If a user tries more, Laravel will block extra requests automatically.
The route uses the throttle:login middleware to apply the limit.
<?php namespace App\Providers; use Illuminate\Support\Facades\RateLimiter; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; use Illuminate\Http\Request; use Illuminate\Cache\RateLimiting\Limit; class RouteServiceProvider extends ServiceProvider { public function boot() { RateLimiter::for('login', function (Request $request) { return Limit::perMinute(5)->by($request->ip()); }); } } // In routes/web.php use Illuminate\Support\Facades\Route; Route::middleware(['throttle:login'])->group(function () { Route::post('/login', function () { return 'Login attempt accepted'; }); });
Laravel automatically sends a 429 status code if the limit is exceeded.
You can customize the response by handling exceptions or using custom middleware.
Use meaningful keys for limits to organize different rate rules.
Rate limiting controls how often users can make requests to keep apps stable.
Laravel uses RateLimiter::for to define limits by keys.
Apply limits with middleware like throttle:key on routes.
Practice
Solution
Step 1: Understand the concept of rate limiting
Rate limiting is used to limit the number of requests a user can make in a given time to prevent overload.Step 2: Identify the purpose in Laravel context
Laravel uses rate limiting to keep the app stable by controlling request frequency.Final Answer:
To control how often users can make requests to keep the app stable -> Option DQuick Check:
Rate limiting = control request frequency [OK]
- Confusing rate limiting with database optimization
- Thinking it encrypts data
- Assuming it generates tokens
RateLimiter::for?Solution
Step 1: Recall Laravel rate limiter syntax
Laravel uses RateLimiter::for with a closure returning a Limit object.Step 2: Match the correct syntax
RateLimiter::for('api', fn (Request $request) => Limit::perMinute(60)); correctly uses RateLimiter::for with a closure and Limit::perMinute(60).Final Answer:
RateLimiter::for('api', fn (Request $request) => Limit::perMinute(60)); -> Option BQuick Check:
Correct syntax uses RateLimiter::for + Limit::perMinute [OK]
- Using non-existent methods like set or create
- Passing number directly without Limit::perMinute
- Missing the closure function
Route::middleware('throttle:api')->get('/data', function () { return 'OK'; });If the rate limiter 'api' allows 2 requests per minute, what will happen on the 3rd request within the same minute?
Solution
Step 1: Understand the throttle middleware behavior
The throttle middleware limits requests based on the named limiter, here 'api'.Step 2: Apply the limit of 2 requests per minute
After 2 requests, the 3rd request exceeds the limit and is blocked with a 429 error.Final Answer:
The request will be blocked with a 429 Too Many Requests response -> Option AQuick Check:
Exceed limit = 429 error [OK]
- Assuming requests always succeed
- Thinking server error 500 occurs
- Believing requests get queued automatically
RateLimiter::for('login', function (Request $request) { return Limit::perMinute(5)->by($request->ip()); });What is wrong if users report they can make unlimited login attempts?
Solution
Step 1: Check if limiter is applied on route
Defining a limiter alone does not enforce it; middleware must use 'throttle:login'.Step 2: Identify missing middleware usage
If middleware is missing on login route, rate limiting won't work, allowing unlimited attempts.Final Answer:
The limiter is not applied on the login route middleware -> Option CQuick Check:
Limiter defined but not applied = no limit [OK]
- Thinking perSecond is required instead of perMinute
- Assuming by() must use user ID always
- Believing RateLimiter::for is deprecated
Solution
Step 1: Check conditional logic for admin and normal users
Admins get 100 requests/min, others get 10 requests/min, keyed by user ID.Step 2: Verify correct use of ternary and by() method
RateLimiter::for('custom', function (Request $request) { return $request->user()?->isAdmin() ? Limit::perMinute(100)->by($request->user()->id) : Limit::perMinute(10)->by($request->user()->id); }); uses ternary to select limit and keys by user ID, handling null user safely.Final Answer:
RateLimiter::for('custom', function (Request $request) { return $request->user()?->isAdmin() ? Limit::perMinute(100)->by($request->user()->id) : Limit::perMinute(10)->by($request->user()->id); }); -> Option AQuick Check:
Conditional limits with by(user ID) = RateLimiter::for('custom', function (Request $request) { return $request->user()?->isAdmin() ? Limit::perMinute(100)->by($request->user()->id) : Limit::perMinute(10)->by($request->user()->id); }); [OK]
- Swapping admin and user limits
- Not keying limits by user ID
- Using wrong ternary logic or missing by()
