Search code examples
phplaravellaravel-5.8throttlingrate-limiting

Laravel throttle rate limiter limites access too early


I'm working with Laravel 5.8 and I wanted to apply a Rate Limiter that limits the sending request to 500 per minute.

So I tried adding this throttle to the route group:

Route::middleware('throttle:500,1')->group(function () {
    ...
});

So this means that limits access to the routes after 500 requests in 1 minute.

Now the problem is I get 429 | Too Many Requests too soon!

I mean, it does not seem to be sending 500 requests in a minute but it limits the access somehow.

So what's going wrong here? Why I get Too Many Requests message too early?


Solution

  • The throttle middleware requests are calculated together if you have another throttle group, these calculated together and may that group consume your limits

    Route::get('example1', function () {
        return 'ans1';
    
    })->middleware('throttle:5,1');
    
    Route::get('example2', function () {
        return 'ans2';
    
    })->middleware('throttle:5,1');
    

    In above case, you have 2 route but if you send 2 request to example1 and 3 request to example2, your rate limit will finish for both of them and you got 429 | Too Many Requests error.