Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Awks's avatar
Level 3

RateLimiter::attempts using Redis

Hello everyone,

I'm working on a personal project using Laravel 9. Some of my routes are throttled by the throttle middleware, configured to work with Redis on the $routeMiddleware from the Kernel.

While working with the "normal" ThrottleRequests::class, my code is working as expected. But as soon as I'm switching for ThrottleRequestsWithRedis::class, the RateLimiter::attempts($key) doesn't seem to retrieve the values from Redis and return 0 instead of the actual count.

Did someone already find a way to correctly match the key and/or fetch it from Redis? After 2 hours of reading the middleware classes, I couldn't figure out :(

To give you some context, below, are the configurations while running with Redis:

// Kernel.php
// ...
protected $routeMiddleware = [
        // ...
        // 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequestsWithRedis::class,
       // ...
];
// RouteServiceProvider.php
protected function configureRateLimiting()
{
        // ...

        RateLimiter::for('auth_registration', function (Request $request) {
                return Limit::perMinute(10)->by($request->ip());
        });
}		
// somewhere on my project

// ...
$key = md5('auth_registration'.$request->ip());
RateLimiter::attempts($key); // always return 0 with Redis

// ...

RateLimiter::clear($key);

Thank you for your help!

0 likes
2 replies
orpheusohms's avatar

@awks you've got wrong keys. One has MD5 while the other doesn't.

the $request->IP() on this line is a $key

return Limit::perMinute(10)->by($request->IP());

Please or to participate in this conversation.