jhodgson4
jhodgson4

Reputation: 1656

Rate limit laravel queue with Redis

I am trying to rate limit one of my laravel queues using the below:

Redis::throttle('key')->allow(10)->every(60)->then(function () {
    // Job logic...
}, function () {
    // Could not obtain lock...

    return $this->release(10);
});

I don't quite understand what the 'key' is used for, does this need to be unique per job or something that identifies the jobs as a group ( not unique )?

Also, I'm assuming this code is to go into the handle method of the job?

Upvotes: 2

Views: 1435

Answers (1)

Alex Blex
Alex Blex

Reputation: 37048

'key' is a limiter name - the hash stored in redis that holds number of acquired locks. It is KEYS[1] in the lua script that actually implement the rate limiting.

To put it simple, if you have multiple invocations of Redis::throttle with the same argument, all of them will contribute to the quota. It is not required to be unique, but using it with different decays may result with unexpected behaviour, as it seems to be used in reset() function only. I imagine there are usecases where you may need to use the same hash name for multiple jobs, but it must be quite rare.

Upvotes: 1

Related Questions